diff --git a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallType.java b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallType.java deleted file mode 100644 index 721a677e3295..000000000000 --- a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallType.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2022 LINE Corporation - * - * LINE Corporation licenses this file to you 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: - * - * https://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 com.linecorp.armeria.common.grpc; - -/** - * Represents the type of {@link GrpcJsonMarshaller}. - */ -public enum GrpcJsonMarshallType { - /** - * Use protobuf-jackson - * for more efficient json marshalling. - */ - PROTOBUF_JACKSON, - /** - * Use upstream google's implementation for json marshalling which also partially supports {@code proto2}. - */ - UPSTREAM, - /** - * The best of both worlds. Use {@link GrpcJsonMarshallType#PROTOBUF_JACKSON} by default, - * and try to use {@link GrpcJsonMarshallType#UPSTREAM} if the message type is {@code proto2}. - */ - DEFAULT, -} diff --git a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshaller.java b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshaller.java index 275924f412a8..9c78ff2b1b0c 100644 --- a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshaller.java +++ b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 LINE Corporation + * Copyright 2022 LINE Corporation * * LINE Corporation licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance @@ -42,16 +42,46 @@ public interface GrpcJsonMarshaller { * served by the {@linkplain ServiceDescriptor service}. */ static GrpcJsonMarshaller of(ServiceDescriptor serviceDescriptor) { - return builder().build(serviceDescriptor); + return ofJackson(serviceDescriptor); } /** * Returns a new {@link GrpcJsonMarshallerBuilder}. */ static GrpcJsonMarshallerBuilder builder() { + return builderForJackson(); + } + + /** + * Returns a newly-created {@link GrpcJsonMarshaller} which serializes and deserializes a {@link Message} + * served by the {@linkplain ServiceDescriptor service}. + */ + static GrpcJsonMarshaller ofJackson(ServiceDescriptor serviceDescriptor) { + return builderForJackson().build(serviceDescriptor); + } + + /** + * Returns a new {@link GrpcJsonMarshallerBuilder}. + */ + static GrpcJsonMarshallerBuilder builderForJackson() { return new GrpcJsonMarshallerBuilder(); } + /** + * Returns a newly-created {@link GrpcJsonMarshaller} which serializes and deserializes a {@link Message} + * to and from {@code json}. + */ + static GrpcJsonMarshaller ofGson() { + return builderForGson().build(); + } + + /** + * Returns a new {@link GrpcJsonMarshallerBuilder}. + */ + static GsonGrpcJsonMarshallerBuilder builderForGson() { + return new GsonGrpcJsonMarshallerBuilder(); + } + /** * Serializes a gRPC message into JSON. */ diff --git a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallerBuilder.java b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallerBuilder.java index 32de33b8d88c..80707d0ac59d 100644 --- a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallerBuilder.java +++ b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonMarshallerBuilder.java @@ -23,12 +23,11 @@ import org.curioswitch.common.protobuf.json.MessageMarshaller; import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder; +import com.google.common.collect.ImmutableList; import com.google.protobuf.Message; import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.internal.common.grpc.DefaultJsonMarshaller; -import com.linecorp.armeria.internal.common.grpc.GrpcJsonUtil; -import com.linecorp.armeria.internal.common.grpc.UpstreamJsonMarshaller; import io.grpc.ServiceDescriptor; @@ -41,8 +40,6 @@ public final class GrpcJsonMarshallerBuilder { @Nullable private Consumer jsonMarshallerCustomizer; - private GrpcJsonMarshallType grpcJsonMarshallType = GrpcJsonMarshallType.DEFAULT; - GrpcJsonMarshallerBuilder() {} /** @@ -64,27 +61,13 @@ public GrpcJsonMarshallerBuilder jsonMarshallerCustomizer( return this; } - /** - * Specifies the {@link GrpcJsonMarshallType} that should be created. - */ - public GrpcJsonMarshallerBuilder grpcJsonMarshallType(GrpcJsonMarshallType grpcJsonMarshallType) { - this.grpcJsonMarshallType = requireNonNull(grpcJsonMarshallType); - return this; - } - /** * Returns a newly-created {@link GrpcJsonMarshaller} with the specified {@link ServiceDescriptor}. */ public GrpcJsonMarshaller build(ServiceDescriptor serviceDescriptor) { requireNonNull(serviceDescriptor, "serviceDescriptor"); - if (grpcJsonMarshallType == GrpcJsonMarshallType.DEFAULT) { - return new DefaultJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer); - } else if (grpcJsonMarshallType == GrpcJsonMarshallType.PROTOBUF_JACKSON) { - return GrpcJsonUtil.protobufJacksonJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer); - } else if (grpcJsonMarshallType == GrpcJsonMarshallType.UPSTREAM) { - return UpstreamJsonMarshaller.INSTANCE; - } else { - throw new Error(); - } + return new DefaultJsonMarshaller( + GrpcJsonUtil.jsonMarshaller(ImmutableList.copyOf(serviceDescriptor.getMethods()), + jsonMarshallerCustomizer)); } } diff --git a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonUtil.java b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonUtil.java similarity index 78% rename from grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonUtil.java rename to grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonUtil.java index 36b0b796fbad..e7e915757ab6 100644 --- a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonUtil.java +++ b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GrpcJsonUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 LINE Corporation + * Copyright 2017 LINE Corporation * * LINE Corporation licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance @@ -14,15 +14,13 @@ * under the License. */ -package com.linecorp.armeria.internal.common.grpc; +package com.linecorp.armeria.common.grpc; import java.util.List; import java.util.function.Consumer; import org.curioswitch.common.protobuf.json.MessageMarshaller; -import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder; -import com.google.common.collect.ImmutableList; import com.google.protobuf.Message; import com.linecorp.armeria.common.annotation.Nullable; @@ -30,18 +28,17 @@ import io.grpc.MethodDescriptor; import io.grpc.MethodDescriptor.Marshaller; import io.grpc.MethodDescriptor.PrototypeMarshaller; -import io.grpc.ServiceDescriptor; /** * Utilities for dealing with JSON marshalling in server/client. */ -public final class GrpcJsonUtil { +final class GrpcJsonUtil { /** * Returns a {@link MessageMarshaller} with the request/response {@link Message}s of all the {@code methods} * registered. */ - private static MessageMarshaller jsonMarshaller( + public static MessageMarshaller jsonMarshaller( List> methods, @Nullable Consumer jsonMarshallerCustomizer) { final MessageMarshaller.Builder builder = MessageMarshaller.builder() @@ -76,12 +73,5 @@ private static Message marshallerPrototype(Marshaller marshaller) { return null; } - public static ProtobufJacksonJsonMarshaller protobufJacksonJsonMarshaller( - ServiceDescriptor serviceDescriptor, @Nullable Consumer jsonMarshallerCustomizer) { - return new ProtobufJacksonJsonMarshaller( - jsonMarshaller(ImmutableList.copyOf(serviceDescriptor.getMethods()), - jsonMarshallerCustomizer)); - } - private GrpcJsonUtil() {} } diff --git a/grpc/src/main/java/com/linecorp/armeria/common/grpc/GsonGrpcJsonMarshallerBuilder.java b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GsonGrpcJsonMarshallerBuilder.java new file mode 100644 index 000000000000..8504a43774a3 --- /dev/null +++ b/grpc/src/main/java/com/linecorp/armeria/common/grpc/GsonGrpcJsonMarshallerBuilder.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 LINE Corporation + * + * LINE Corporation licenses this file to you 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: + * + * https://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 com.linecorp.armeria.common.grpc; + +import static java.util.Objects.requireNonNull; + +import java.util.function.Consumer; + +import com.google.protobuf.Message; +import com.google.protobuf.util.JsonFormat; + +import com.linecorp.armeria.common.annotation.Nullable; +import com.linecorp.armeria.internal.common.grpc.GsonGrpcJsonMarshaller; + +/** + * A builder for creating a new {@link GrpcJsonMarshaller} that serializes and deserializes a {@link Message} + * to and from JSON. + */ +public final class GsonGrpcJsonMarshallerBuilder { + + @Nullable + private Consumer jsonParserCustomizer; + + @Nullable + private Consumer jsonPrinterCustomizer; + + GsonGrpcJsonMarshallerBuilder() {} + + /** + * TBU. + */ + public GsonGrpcJsonMarshallerBuilder jsonParserCustomizer( + Consumer jsonParserCustomizer) { + requireNonNull(jsonParserCustomizer, "jsonParserCustomizer"); + if (this.jsonParserCustomizer == null) { + @SuppressWarnings("unchecked") + final Consumer cast = (Consumer) jsonParserCustomizer; + this.jsonParserCustomizer = cast; + } else { + this.jsonParserCustomizer = this.jsonParserCustomizer.andThen(jsonParserCustomizer); + } + return this; + } + + /** + * TBU. + */ + public GsonGrpcJsonMarshallerBuilder jsonPrinterCustomizer( + Consumer jsonPrinterCustomizer) { + requireNonNull(jsonPrinterCustomizer, "jsonPrinterCustomizer"); + if (this.jsonPrinterCustomizer == null) { + @SuppressWarnings("unchecked") + final Consumer cast = (Consumer) jsonPrinterCustomizer; + this.jsonPrinterCustomizer = cast; + } else { + this.jsonPrinterCustomizer = this.jsonPrinterCustomizer.andThen(jsonPrinterCustomizer); + } + return this; + } + + /** + * Returns a newly-created {@link GsonGrpcJsonMarshaller}. + */ + public GsonGrpcJsonMarshaller build() { + final JsonFormat.Printer printer = JsonFormat.printer().omittingInsignificantWhitespace(); + if (jsonPrinterCustomizer != null) { + jsonPrinterCustomizer.accept(printer); + } + + final JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields(); + if (jsonParserCustomizer != null) { + jsonParserCustomizer.accept(parser); + } + return new GsonGrpcJsonMarshaller(printer, parser); + } +} diff --git a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/DefaultJsonMarshaller.java b/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/DefaultJsonMarshaller.java index e291598801cf..a148aa186f52 100644 --- a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/DefaultJsonMarshaller.java +++ b/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/DefaultJsonMarshaller.java @@ -19,62 +19,37 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.function.Consumer; -import org.curioswitch.common.protobuf.json.MessageMarshaller.Builder; +import org.curioswitch.common.protobuf.json.MessageMarshaller; -import com.google.common.annotations.VisibleForTesting; -import com.google.protobuf.Descriptors.FileDescriptor; -import com.google.protobuf.Descriptors.FileDescriptor.Syntax; +import com.google.protobuf.Message; -import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.common.grpc.GrpcJsonMarshaller; import io.grpc.MethodDescriptor.Marshaller; -import io.grpc.ServiceDescriptor; -import io.grpc.protobuf.ProtoFileDescriptorSupplier; +import io.grpc.MethodDescriptor.PrototypeMarshaller; public final class DefaultJsonMarshaller implements GrpcJsonMarshaller { - /** - * Recursively goes through dependencies and look if any files are defined with proto2. - * It seems like protobuf doesn't allow circular imports for now. - */ - private static boolean hasProto2(FileDescriptor fileDescriptor) { - if (fileDescriptor.getSyntax() == Syntax.PROTO2) { - return true; - } - return fileDescriptor.getDependencies().stream().anyMatch(DefaultJsonMarshaller::hasProto2); - } - - private final GrpcJsonMarshaller delegate; + private final MessageMarshaller delegate; - public DefaultJsonMarshaller(ServiceDescriptor serviceDescriptor, - @Nullable Consumer jsonMarshallerCustomizer) { - final Object schemaDescriptor = serviceDescriptor.getSchemaDescriptor(); - if (schemaDescriptor instanceof ProtoFileDescriptorSupplier) { - final FileDescriptor fileDescriptor = - ((ProtoFileDescriptorSupplier) schemaDescriptor).getFileDescriptor(); - if (hasProto2(fileDescriptor)) { - delegate = UpstreamJsonMarshaller.INSTANCE; - return; - } - } - delegate = GrpcJsonUtil.protobufJacksonJsonMarshaller(serviceDescriptor, jsonMarshallerCustomizer); + public DefaultJsonMarshaller(MessageMarshaller delegate) { + this.delegate = delegate; } @Override public void serializeMessage(Marshaller marshaller, T message, OutputStream os) throws IOException { - delegate.serializeMessage(marshaller, message, os); + delegate.writeValue((Message) message, os); } @Override public T deserializeMessage(Marshaller marshaller, InputStream is) throws IOException { - return delegate.deserializeMessage(marshaller, is); - } - - @VisibleForTesting - GrpcJsonMarshaller delegate() { - return delegate; + final PrototypeMarshaller prototypeMarshaller = (PrototypeMarshaller) marshaller; + final Message prototype = (Message) prototypeMarshaller.getMessagePrototype(); + final Message.Builder builder = prototype.newBuilderForType(); + delegate.mergeValue(is, builder); + @SuppressWarnings("unchecked") + final T cast = (T) builder.build(); + return cast; } } diff --git a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/UpstreamJsonMarshaller.java b/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GsonGrpcJsonMarshaller.java similarity index 80% rename from grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/UpstreamJsonMarshaller.java rename to grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GsonGrpcJsonMarshaller.java index 961bed4052f7..fbe3c768dfc4 100644 --- a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/UpstreamJsonMarshaller.java +++ b/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/GsonGrpcJsonMarshaller.java @@ -36,16 +36,19 @@ * A {@link GrpcJsonMarshaller} which serializes and deserializes a {@link Message} * to and from JSON using the upstream {@link JsonParser} and {@link JsonFormat} utilities. */ -public final class UpstreamJsonMarshaller implements GrpcJsonMarshaller { +public final class GsonGrpcJsonMarshaller implements GrpcJsonMarshaller { - private UpstreamJsonMarshaller() {} + final JsonFormat.Printer printer; + final JsonFormat.Parser parser; - public static final UpstreamJsonMarshaller INSTANCE = new UpstreamJsonMarshaller(); + public GsonGrpcJsonMarshaller(JsonFormat.Printer printer, JsonFormat.Parser parser) { + this.printer = printer; + this.parser = parser; + } @Override public void serializeMessage(Marshaller marshaller, T message, OutputStream os) throws IOException { - os.write(JsonFormat.printer().omittingInsignificantWhitespace() - .print((MessageOrBuilder) message).getBytes(StandardCharsets.UTF_8)); + os.write(printer.print((MessageOrBuilder) message).getBytes(StandardCharsets.UTF_8)); } @Override @@ -54,8 +57,7 @@ public T deserializeMessage(Marshaller marshaller, InputStream is) throws final Message prototype = (Message) prototypeMarshaller.getMessagePrototype(); assert prototype != null; final Message.Builder builder = prototype.newBuilderForType(); - JsonFormat.parser().ignoringUnknownFields() - .merge(new InputStreamReader(is, StandardCharsets.UTF_8), builder); + parser.merge(new InputStreamReader(is, StandardCharsets.UTF_8), builder); @SuppressWarnings("unchecked") final T cast = (T) builder.build(); return cast; diff --git a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/ProtobufJacksonJsonMarshaller.java b/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/ProtobufJacksonJsonMarshaller.java deleted file mode 100644 index c507e06891f0..000000000000 --- a/grpc/src/main/java/com/linecorp/armeria/internal/common/grpc/ProtobufJacksonJsonMarshaller.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2022 LINE Corporation - * - * LINE Corporation licenses this file to you 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: - * - * https://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 com.linecorp.armeria.internal.common.grpc; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -import org.curioswitch.common.protobuf.json.MessageMarshaller; - -import com.google.protobuf.Message; - -import com.linecorp.armeria.common.grpc.GrpcJsonMarshaller; - -import io.grpc.MethodDescriptor.Marshaller; -import io.grpc.MethodDescriptor.PrototypeMarshaller; - -public final class ProtobufJacksonJsonMarshaller implements GrpcJsonMarshaller { - - private final MessageMarshaller delegate; - - public ProtobufJacksonJsonMarshaller(MessageMarshaller delegate) { - this.delegate = delegate; - } - - @Override - public void serializeMessage(Marshaller marshaller, T message, OutputStream os) throws IOException { - delegate.writeValue((Message) message, os); - } - - @Override - public T deserializeMessage(Marshaller marshaller, InputStream is) throws IOException { - final PrototypeMarshaller prototypeMarshaller = (PrototypeMarshaller) marshaller; - final Message prototype = (Message) prototypeMarshaller.getMessagePrototype(); - final Message.Builder builder = prototype.newBuilderForType(); - delegate.mergeValue(is, builder); - @SuppressWarnings("unchecked") - final T cast = (T) builder.build(); - return cast; - } -} diff --git a/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonMarshallerTest.java b/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonMarshallerTest.java deleted file mode 100644 index 232a2f90cbdc..000000000000 --- a/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcJsonMarshallerTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2022 LINE Corporation - * - * LINE Corporation licenses this file to you 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: - * - * https://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 com.linecorp.armeria.internal.common.grpc; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -import com.linecorp.armeria.common.grpc.GrpcJsonMarshallType; -import com.linecorp.armeria.common.grpc.GrpcJsonMarshaller; - -import example.armeria.grpc.Proto2ServiceGrpc; -import example.armeria.grpc.Proto3ServiceGrpc; -import example.armeria.grpc.Proto3WithProto2ServiceGrpc; - -class GrpcJsonMarshallerTest { - - @Test - void testDefaultMarshallerDelegate() { - // only proto2 - GrpcJsonMarshaller grpcJsonMarshaller = - GrpcJsonMarshaller.of(Proto2ServiceGrpc.getServiceDescriptor()); - assertThat(grpcJsonMarshaller).isInstanceOf(DefaultJsonMarshaller.class); - DefaultJsonMarshaller defaultJsonMarshaller = (DefaultJsonMarshaller) grpcJsonMarshaller; - assertThat(defaultJsonMarshaller.delegate()).isInstanceOf(UpstreamJsonMarshaller.class); - - // only proto3 - grpcJsonMarshaller = GrpcJsonMarshaller.of(Proto3ServiceGrpc.getServiceDescriptor()); - assertThat(grpcJsonMarshaller).isInstanceOf(DefaultJsonMarshaller.class); - defaultJsonMarshaller = (DefaultJsonMarshaller) grpcJsonMarshaller; - assertThat(defaultJsonMarshaller.delegate()).isInstanceOf(ProtobufJacksonJsonMarshaller.class); - - // proto3 including proto2 - grpcJsonMarshaller = GrpcJsonMarshaller.of(Proto3WithProto2ServiceGrpc.getServiceDescriptor()); - assertThat(grpcJsonMarshaller).isInstanceOf(DefaultJsonMarshaller.class); - defaultJsonMarshaller = (DefaultJsonMarshaller) grpcJsonMarshaller; - assertThat(defaultJsonMarshaller.delegate()).isInstanceOf(UpstreamJsonMarshaller.class); - } - - @Test - void testUpstreamMarshaller() { - final GrpcJsonMarshaller grpcJsonMarshaller = - GrpcJsonMarshaller.builder() - .grpcJsonMarshallType(GrpcJsonMarshallType.UPSTREAM) - .build(Proto3ServiceGrpc.getServiceDescriptor()); - assertThat(grpcJsonMarshaller).isInstanceOf(UpstreamJsonMarshaller.class); - } - - @Test - void testProtobufJacksonMarshaller() { - final GrpcJsonMarshaller grpcJsonMarshaller = - GrpcJsonMarshaller.builder() - .grpcJsonMarshallType(GrpcJsonMarshallType.PROTOBUF_JACKSON) - .build(Proto3ServiceGrpc.getServiceDescriptor()); - assertThat(grpcJsonMarshaller).isInstanceOf(ProtobufJacksonJsonMarshaller.class); - } -} diff --git a/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcMessageMarshallerTest.java b/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcMessageMarshallerTest.java index 7b8dde0daeee..ee89b7c4cfe4 100644 --- a/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcMessageMarshallerTest.java +++ b/grpc/src/test/java/com/linecorp/armeria/internal/common/grpc/GrpcMessageMarshallerTest.java @@ -40,13 +40,12 @@ class GrpcMessageMarshallerTest { private static Stream grpcJsonMarshallerStream() { - final ProtobufJacksonJsonMarshaller protobufJacksonJsonMarshaller = - new ProtobufJacksonJsonMarshaller(MessageMarshaller.builder() + final DefaultJsonMarshaller protobufJacksonJsonMarshaller = + new DefaultJsonMarshaller(MessageMarshaller.builder() .register(SimpleRequest.getDefaultInstance()) .register(SimpleResponse.getDefaultInstance()) .build()); - return Stream.of(protobufJacksonJsonMarshaller, UpstreamJsonMarshaller.INSTANCE, - new DefaultJsonMarshaller(TestServiceGrpc.getServiceDescriptor(), null)); + return Stream.of(protobufJacksonJsonMarshaller, GrpcJsonMarshaller.ofGson()); } private static Stream jsonMarshallerArgs() { diff --git a/grpc/src/test/java/com/linecorp/armeria/it/grpc/GrpcProtoVersionTest.java b/grpc/src/test/java/com/linecorp/armeria/it/grpc/GrpcProtoVersionTest.java index c71d3419b01b..6526255661ba 100644 --- a/grpc/src/test/java/com/linecorp/armeria/it/grpc/GrpcProtoVersionTest.java +++ b/grpc/src/test/java/com/linecorp/armeria/it/grpc/GrpcProtoVersionTest.java @@ -24,6 +24,8 @@ import org.junit.jupiter.api.extension.RegisterExtension; import com.linecorp.armeria.client.Clients; +import com.linecorp.armeria.client.grpc.GrpcClients; +import com.linecorp.armeria.common.grpc.GrpcJsonMarshaller; import com.linecorp.armeria.common.grpc.GrpcSerializationFormats; import com.linecorp.armeria.server.ServerBuilder; import com.linecorp.armeria.server.grpc.GrpcService; @@ -55,7 +57,7 @@ public void echo(Proto2Message request, responseObserver.onNext(request); responseObserver.onCompleted(); } - }).build(); + }).jsonMarshallerFactory(sd -> GrpcJsonMarshaller.ofGson()).build(); final GrpcService proto3Service = GrpcService.builder().addService(new Proto3ServiceImplBase() { @Override @@ -73,7 +75,7 @@ public void echo(Proto2Message request, responseObserver.onNext(request); responseObserver.onCompleted(); } - }).build(); + }).jsonMarshallerFactory(sd -> GrpcJsonMarshaller.ofGson()).build(); sb.idleTimeout(Duration.ZERO) .requestTimeout(Duration.ZERO) .service(proto2Service) @@ -96,10 +98,11 @@ void testProto3() { @Test void testProto2() { final Proto2ServiceBlockingStub proto2Service = - Clients.builder(server.httpUri(GrpcSerializationFormats.JSON)) - .writeTimeout(Duration.ZERO) - .responseTimeout(Duration.ZERO) - .build(Proto2ServiceBlockingStub.class); + GrpcClients.builder(server.httpUri(GrpcSerializationFormats.JSON)) + .jsonMarshallerFactory(sd -> GrpcJsonMarshaller.ofGson()) + .writeTimeout(Duration.ZERO) + .responseTimeout(Duration.ZERO) + .build(Proto2ServiceBlockingStub.class); final Proto2Message message = Proto2Message.newBuilder().setFoo(Foo2.B2).build(); assertThat(proto2Service.echo(message)).isEqualTo(message); } @@ -107,10 +110,11 @@ void testProto2() { @Test void testProto3WithProto2() { final Proto3WithProto2ServiceBlockingStub proto2Service = - Clients.builder(server.httpUri(GrpcSerializationFormats.JSON)) - .writeTimeout(Duration.ZERO) - .responseTimeout(Duration.ZERO) - .build(Proto3WithProto2ServiceBlockingStub.class); + GrpcClients.builder(server.httpUri(GrpcSerializationFormats.JSON)) + .jsonMarshallerFactory(sd -> GrpcJsonMarshaller.ofGson()) + .writeTimeout(Duration.ZERO) + .responseTimeout(Duration.ZERO) + .build(Proto3WithProto2ServiceBlockingStub.class); final Proto2Message message = Proto2Message.newBuilder().setFoo(Foo2.B2).build(); assertThat(proto2Service.echo(message)).isEqualTo(message); }