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

Avoid having TypeConverter defined as beans #10315

Merged
merged 7 commits into from
Jan 4, 2024
Merged

Avoid having TypeConverter defined as beans #10315

merged 7 commits into from
Jan 4, 2024

Conversation

dstepanov
Copy link
Contributor

No description provided.

@dstepanov dstepanov added the type: improvement A minor improvement to an existing feature label Jan 2, 2024
@dstepanov dstepanov added this to the 4.3.0 milestone Jan 2, 2024
@dstepanov
Copy link
Contributor Author

Strange, what is defining it to be initiated at the runtime:

 Error: Classes that should be initialized at run time got initialized during image building:
 io.micronaut.http.server.cors.CorsOriginConverter was unintentionally initialized at build time. To see why io.micronaut.http.server.cors.CorsOriginConverter got initialized use --trace-class-initialization=io.micronaut.http.server.cors.CorsOriginConverter

Copy link
Contributor

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the motivation for this change?

But more importantly, we need to update the documentation. We currently say:

You can register additional converters for types not supported by Micronaut by defining beans that implement the TypeConverter interface

However, this PR indicates the recommended approach is to:
Create a class, not a bean, implementing TypeConveterRegistrar.
Register the converter in that class.
List the TypeConverterRegistrar implementation in src/main/resources/META-INF/services/io.micronaut.core.convert.TypeConverterRegistrar so that it is loaded later via Java SPI.

Moreover, removing the beans SameSiteConverter and CorsOriginConveter and making them @Internal could be considered a breaking change.

@dstepanov
Copy link
Contributor Author

Registering the converter using the SPI versus finding beans should be faster for the startup; having fewer beans is also a plus.

The only reason to have converters as beans is if you need to inject something external. I'm not sure what the point of having those public.

Now I tried to disable CorsOriginConverter, and it looks like it's not needed anyway and probably added in v1 because the configuration beans were not flexible enough.

@sdelamo
Copy link
Contributor

sdelamo commented Jan 3, 2024

Now I tried to disable CorsOriginConverter, and it looks like it's not needed anyway and probably added in v1 because the configuration beans were not flexible enough.

Or maybe we lack test coverage.

@sdelamo
Copy link
Contributor

sdelamo commented Jan 3, 2024

The only reason to have converters as beans is if you need to inject something external

What do you mean by something external?

@dstepanov
Copy link
Contributor Author

The only reason to have converters as beans is if you need to inject something external

What do you mean by something external?

Like the bean context or something from it.

@dstepanov
Copy link
Contributor Author

Now I tried to disable CorsOriginConverter, and it looks like it's not needed anyway and probably added in v1 because the configuration beans were not flexible enough.

Or maybe we lack test coverage.

I think it's used in the multiple CORS configuration and we should have tests for it.

@sdelamo
Copy link
Contributor

sdelamo commented Jan 3, 2024

Now I tried to disable CorsOriginConverter, and it looks like it's not needed anyway and probably added in v1 because the configuration beans were not flexible enough.

Or maybe we lack test coverage.

I think it's used in the multiple CORS configuration and we should have tests for it.

A TCK test fails:

JUnit Platform Suite:HTTP Server TCK for Netty:JUnit Jupiter:CorsSimpleRequestTest:corsSimpleRequestForLocalhostCanBeAllowedViaRegexConfiguration()

@sdelamo
Copy link
Contributor

sdelamo commented Jan 3, 2024

@dstepanov can you please update the documentation

we need to update the documentation. We currently say:

You can register additional converters for types not supported by Micronaut by defining beans that implement the TypeConverter interface

However, this PR indicates the recommended approach is to: Create a class, not a bean, implementing TypeConveterRegistrar. Register the converter in that class. List the TypeConverterRegistrar implementation in src/main/resources/META-INF/services/io.micronaut.core.convert.TypeConverterRegistrar so that it is loaded later via Java SPI.

@dstepanov
Copy link
Contributor Author

It's strange that it would fail only in native

@dstepanov
Copy link
Contributor Author

@sdelamo I have corrected docs

Copy link

sonarcloud bot commented Jan 3, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

68.8% Coverage on New Code (required ≥ 70%)

See analysis details on SonarCloud

@sdelamo sdelamo merged commit cd72cbf into 4.3.x Jan 4, 2024
14 of 15 checks passed
@sdelamo sdelamo deleted the httpco branch January 4, 2024 17:06
sdelamo added a commit to micronaut-projects/micronaut-security that referenced this pull request Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants