Skip to content

Conversation

arouel
Copy link
Contributor

@arouel arouel commented Jul 28, 2025

Description

It is currently not 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 in a modular way to extend Gson's functionality within Helidon.

More details how to use it can be found in issue #10435.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Jul 28, 2025
@arouel
Copy link
Contributor Author

arouel commented Jul 28, 2025

@eiselems maybe this is of interest to you.

@eiselems
Copy link
Contributor

eiselems commented Aug 4, 2025

@tomas-langer sounds like something that I missed, maybe you can also have a look

@barchetta barchetta requested a review from Verdent August 13, 2025 22:05
@arouel
Copy link
Contributor Author

arouel commented Sep 18, 2025

@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.

GsonBuilder gsonBuilder = new GsonBuilder();
// Enable the registering of custom type adapters by using service providers for TypeAdapterFactory.
for (var factory : ServiceLoader.load(TypeAdapterFactory.class)) {
Copy link
Member

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.TypeAdapterFactory statement in module-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 @Weight to order providers
  • the gson instance you create is lost

How to do this to align with current implementation:

  • add the following method to GsonSupportConfigBlueprint to 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 the target builder to the GsonBuilder instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants