-
Notifications
You must be signed in to change notification settings - Fork 584
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
Open
arouel
wants to merge
1
commit into
helidon-io:main
Choose a base branch
from
arouel:gson-register
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
http/media/gson/src/test/java/io/helidon/http/media/gson/GsonSupportTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright (c) 2025 Oracle and/or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.helidon.http.media.gson; | ||
|
|
||
| import io.helidon.common.GenericType; | ||
| import io.helidon.common.config.Config; | ||
| import io.helidon.http.WritableHeaders; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| class GsonSupportTest { | ||
|
|
||
| record Book(String title, int pages) { | ||
| } | ||
|
|
||
| @Test | ||
| void test() { | ||
| var support = GsonSupport.create(Config.empty(), "gson"); | ||
| var headers = WritableHeaders.create(); | ||
| var type = GenericType.create(Book.class); | ||
| var outputStream = new ByteArrayOutputStream(); | ||
| var instance = new Book("some-title", 123); | ||
|
|
||
| support.writer(type, headers) | ||
| .supplier() | ||
| .get() | ||
| .write(type, instance, outputStream, headers); | ||
|
|
||
| assertThat(GsonSupportTestBookTypeAdapterFactory.writeCount.get(), is(1)); | ||
|
|
||
| Book sanity = support.reader(type, headers) | ||
| .supplier() | ||
| .get() | ||
| .read(type, new ByteArrayInputStream(outputStream.toByteArray()), headers); | ||
|
|
||
| assertThat(GsonSupportTestBookTypeAdapterFactory.readCount.get(), is(1)); | ||
|
|
||
| assertThat(sanity.title(), is("some-title")); | ||
| assertThat(sanity.pages(), is(123)); | ||
| assertThat(sanity, is(instance)); | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
.../gson/src/test/java/io/helidon/http/media/gson/GsonSupportTestBookTypeAdapterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright (c) 2025 Oracle and/or its affiliates. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.helidon.http.media.gson; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.TypeAdapterFactory; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| public class GsonSupportTestBookTypeAdapterFactory implements TypeAdapterFactory { | ||
|
|
||
| static final AtomicInteger readCount = new AtomicInteger(0); | ||
| static final AtomicInteger writeCount = new AtomicInteger(0); | ||
|
|
||
| private static final TypeAdapter instance = new TypeAdapter<GsonSupportTest.Book>() { | ||
| @Override | ||
| public void write(JsonWriter writer, GsonSupportTest.Book book) throws IOException { | ||
| writer.beginObject(); | ||
| writer.name("title"); | ||
| writer.value(book.title()); | ||
| writer.name("pages"); | ||
| writer.value(book.pages()); | ||
| writer.endObject(); | ||
| writeCount.incrementAndGet(); | ||
| } | ||
|
|
||
| @Override | ||
| public GsonSupportTest.Book read(JsonReader reader) throws IOException { | ||
| reader.beginObject(); | ||
| reader.nextName(); | ||
| var title = reader.nextString(); | ||
| reader.nextName(); | ||
| var pages = reader.nextInt(); | ||
| reader.endObject(); | ||
| readCount.incrementAndGet(); | ||
| return new GsonSupportTest.Book(title, pages); | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { | ||
| if (typeToken.getRawType().isAssignableFrom(GsonSupportTest.Book.class)) { | ||
| return instance; | ||
| } | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package my.pkg; | ||
|
|
||
| public record Book(String title, int pages) { | ||
| } |
49 changes: 49 additions & 0 deletions
49
http/media/gson/src/test/java/my/pkg/BookTypeAdapterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package my.pkg; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.TypeAdapterFactory; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class BookTypeAdapterFactory implements TypeAdapterFactory { | ||
|
|
||
| private static final TypeAdapter<Book> instance = new TypeAdapter<>() { | ||
| @Override | ||
| public void write(JsonWriter writer, Book book) throws IOException { | ||
| writer.beginObject(); | ||
| writer.name("title"); | ||
| writer.value(book.title()); | ||
| writer.name("pages"); | ||
| writer.value(book.pages()); | ||
| writer.endObject(); | ||
| } | ||
|
|
||
| @Override | ||
| public Book read(JsonReader reader) throws IOException { | ||
| reader.beginObject(); | ||
| String title = null; | ||
| int pages = 0; | ||
| while (reader.hasNext()) { | ||
| switch (reader.nextName()) { | ||
| case "title" -> title = reader.nextString(); | ||
| case "pages" -> pages = reader.nextInt(); | ||
| default -> reader.skipValue(); | ||
| } | ||
| } | ||
| reader.endObject(); | ||
| return new Book(title, pages); | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { | ||
| if (typeToken.getRawType().isAssignableFrom(Book.class)) { | ||
| return (TypeAdapter<T>) instance; | ||
| } | ||
| return null; | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
http/media/gson/src/test/resources/META-INF/services/com.google.gson.TypeAdapterFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| io.helidon.http.media.gson.GsonSupportTestBookTypeAdapterFactory |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
uses com.google.gson.TypeAdapterFactorystatement inmodule-info.java, otherwise this would fail on module pathio.helidon.common.HelidonServiceLoader#create(java.lang.Class<T>)- this supports@Weightto order providersgsoninstance you create is lostHow to do this to align with current implementation:
GsonSupportConfigBlueprintto allow users to register them programmatically:io.helidon.http.media.gson.GsonSupport.Decorator#decorate- add the factories from thetargetbuilder to theGsonBuilderinstance.