Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use beanName as default name of @ServiceConnection #36064

Closed
wants to merge 1 commit into from

Conversation

quaff
Copy link
Contributor

@quaff quaff commented Jun 25, 2023

Before this commit, ConnectionDetailsNotFoundException will be raised if unnamed @ServiceConnection is annotated on @Bean method and containerType is not accepted by ContainerConnectionSource.

For example:

	@Bean
	@ServiceConnection(name = "redis")
	GenericContainer<?> redis() {
		return new GenericContainer<>("redis").withExposedPorts(6379);
	}

After this commit, name of @ServiceConnection could be omitted.

@wilkinsona
Copy link
Member

Thanks for the proposal, @quaff. @snicoll suggested this a few weeks ago but I wasn't in favor as it feels a bit too magical. It also won't work for containers with / in their name such as openzipkin/zipkin. Let's see what the rest of the team thinks.

@wilkinsona wilkinsona added for: team-meeting An issue we'd like to discuss as a team to make progress for: team-attention An issue we'd like other members of the team to review and removed for: team-meeting An issue we'd like to discuss as a team to make progress labels Jun 25, 2023
@quaff
Copy link
Contributor Author

quaff commented Jun 25, 2023

Thanks for the proposal, @quaff. @snicoll suggested this a few weeks ago but I wasn't in favor as it feels a bit too magical. It also won't work for containers with / in their name such as openzipkin/zipkin. Let's see what the rest of the team thinks.

What about bean name as openzipkin_zipkin or openzipkinZipkin?

@eddumelendez
Copy link
Contributor

IIRC, this was introduced in order to make sure the ConnectionFactory to be used. This is due there is no specific Testcontainers implementation. Redis and Zipkin fall into this because both use GenericContainer but the issue comes with custom images ("my-custom-kv-store") which will always requires @ServiceName(name = "redis"). The existing approach is good, instead of making it optional and rely on the bean's name and increase cognitive complexity

@quaff
Copy link
Contributor Author

quaff commented Jun 25, 2023

If people migrate

@ServiceConnection
GenericContainer<?> redis = return new GenericContainer<>("redis").withExposedPorts(6379);

to

@Bean
@ServiceConnection
GenericContainer<?> redis() {
	return new GenericContainer<>("redis").withExposedPorts(6379);
}

They will encounter unexpected ConnectionDetailsNotFoundException .

@wilkinsona
Copy link
Member

That's not because the field is called redis but because the GenericContainer instance is available, allowing us to call getDockerImageName() on it:

By default Container.getDockerImageName() is used to obtain the name used to find connection details. If you are using a custom docker image, you can use the name attribute of @ServiceConnection to override it.

When you're using @Bean, the container instance isn't available without calling the method which we can't do as it would trigger eager initialization. As a result, all we have available is the return type of the @Bean method. We should improve the documentation for this as it's not explained at the moment and I think it should be. I've opened #36071.

@quaff
Copy link
Contributor Author

quaff commented Jun 26, 2023

That's not because the field is called redis but because the GenericContainer instance is available, allowing us to call getDockerImageName() on it:

By default Container.getDockerImageName() is used to obtain the name used to find connection details. If you are using a custom docker image, you can use the name attribute of @ServiceConnection to override it.

When you're using @Bean, the container instance isn't available without calling the method which we can't do as it would trigger eager initialization. As a result, all we have available is the return type of the @Bean method. We should improve the documentation for this as it's not explained at the moment and I think it should be. I've opened #36071.

I know that because I read sources before preparing this PR, but most people doesn't read sources even documents, they want intuitional programming experience.

@quaff quaff changed the title Use beanName as fallback of containerImageName Use beanName as default name of @ServiceConnection Jun 26, 2023
@quaff
Copy link
Contributor Author

quaff commented Jun 26, 2023

Using beanName as default name of @ServiceConnection is better choice, I've updated the commit.

Before this commit, ConnectionDetailsNotFoundException will be raised if unnamed @Serviceconnection is annotated on @bean method and containerType is not accepted by ContainerConnectionSource.
@wilkinsona
Copy link
Member

We've discussed this today and decided that we don't want to make any changes here. We think there's too much magic in using the bean name. Thanks anyway for the suggestion, @quaff. We do think the docs should be improved though. #36071 is tracking that.

@wilkinsona wilkinsona closed this Jun 28, 2023
@wilkinsona wilkinsona added status: declined A suggestion or change that we don't feel we should currently apply and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants