Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Map;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.function.Consumer;

import io.helidon.builder.api.Prototype;
Expand All @@ -34,6 +35,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapterFactory;

import static io.helidon.http.HeaderValues.CONTENT_TYPE_JSON;

Expand Down Expand Up @@ -82,6 +84,12 @@ public static MediaSupport create(Config config, String name) {
Objects.requireNonNull(config, "Config must not be null");
Objects.requireNonNull(name, "Name must not be null");

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.

gsonBuilder.registerTypeAdapterFactory(factory);
}
Gson gson = gsonBuilder.create();
return builder()
.name(name)
.config(config)
Expand Down
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));
}
}
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;
}
}
4 changes: 4 additions & 0 deletions http/media/gson/src/test/java/my/pkg/Book.java
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 http/media/gson/src/test/java/my/pkg/BookTypeAdapterFactory.java
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.helidon.http.media.gson.GsonSupportTestBookTypeAdapterFactory