-
Notifications
You must be signed in to change notification settings - Fork 583
Add custom Gson type adapter factories during runtime
#10437
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
base: main
Are you sure you want to change the base?
Conversation
|
@eiselems maybe this is of interest to you. |
|
@tomas-langer sounds like something that I missed, maybe you can also have a look |
|
@Verdent @tomas-langer do you have feedback about this change request? I would appreciate if this get merged soon. |
It is not currently possible to add custom Gson type adapters in a modular way at runtime. Adding them requires manually instantiating GsonSupport, which undermines the modular approach of extending Helidon with media type implementations. To resolve this, I have added the ability to add custom TypeAdapterFactory instances as service providers. These providers are loaded during the initialization of GsonSupport by the GsonMediaSupportProvider using Java's ServiceLoader API. This allows for a more flexible and modular way to extend Gson's functionality within Helidon.
eadc816 to
6d4b3fe
Compare
|
|
||
| GsonBuilder gsonBuilder = new GsonBuilder(); | ||
| // Enable the registering of custom type adapters by using service providers for TypeAdapterFactory. | ||
| for (var factory : ServiceLoader.load(TypeAdapterFactory.class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- there must be a
uses com.google.gson.TypeAdapterFactorystatement inmodule-info.java, otherwise this would fail on module path - we never use Java service loader directly, please use
io.helidon.common.HelidonServiceLoader#create(java.lang.Class<T>)- this supports@Weightto order providers - the
gsoninstance you create is lost
How to do this to align with current implementation:
- add the following method to
GsonSupportConfigBlueprintto allow users to register them programmatically:
/**
* Additional type adapter factories.
*
* @returns type adapter factories
*/
@Option.Singular("typeAdapterFactory")
List<TypeAdapterFactory> typeAdapterFactories();
- add the discovered factories to the builder created on line 85 (in the original code)
- update method
io.helidon.http.media.gson.GsonSupport.Decorator#decorate- add the factories from thetargetbuilder to theGsonBuilderinstance.
Description
It is currently not possible to add custom
Gsontype adapters in a modular way at runtime. Adding them requires manually instantiatingGsonSupport, which undermines the modular approach of extending Helidon with media type implementations.To resolve this, I have added the ability to add custom
TypeAdapterFactoryinstances as service providers. These providers are loaded during the initialization ofGsonSupportby theGsonMediaSupportProviderusing Java'sServiceLoaderAPI. This allows in a modular way to extend Gson's functionality within Helidon.More details how to use it can be found in issue #10435.