This is an automated email from the ASF dual-hosted git repository. aldettinger pushed a commit to branch CAMEL-QUARKUS-3382 in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit b81bb7695f96078bfda9dd7eb178cc3563044198 Author: aldettinger <[email protected]> AuthorDate: Mon Dec 13 16:23:22 2021 +0100 protobuf: Missing method "getName" when using contentTypeFormat=json in native mode #3382 --- .../ROOT/pages/reference/extensions/protobuf.adoc | 20 ++++++++++++++-- .../runtime/src/main/doc/configuration.adoc | 20 ++++++++++++++-- .../component/protobuf/it/ProtobufResource.java | 22 +++++++++++++++++- .../component/protobuf/it/ProtobufRoute.java | 14 +++++++++++ .../component/protobuf/it/ProtobufTest.java | 27 ++++++++++++++++++++++ 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/protobuf.adoc b/docs/modules/ROOT/pages/reference/extensions/protobuf.adoc index eba2417..7bd6910 100644 --- a/docs/modules/ROOT/pages/reference/extensions/protobuf.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/protobuf.adoc @@ -64,6 +64,22 @@ service and message definitions stored in the `src/main/proto` directory: </build> ---- -You may want to check the https://github.com/apache/camel-quarkus/tree/main/integration-tests/protobuf[integration test] -in our source tree as an example. +Moreover, please note that some additional configurations might be needed when using `contentTypeFormat=json`. +Indeed, in such a case, the generated `Builder` class needs to be registered for reflection. + +For instance, let's examine the `ProtobufDataFormat` below: + +[source,java] +---- +ProtobufDataFormat protobufJsonDataFormat = new ProtobufDataFormat(Person.getDefaultInstance(), ProtobufDataFormat.CONTENT_TYPE_FORMAT_JSON); +---- + +In such a case, the `Person.Builder` class should be xref:user-guide/native-mode.adoc#reflection[registered for reflection], for instance as below: + +[source,java] +---- +@RegisterForReflection(targets = { org.apache.camel.quarkus.component.protobuf.it.model.AddressBookProtos.Person.Builder.class }) +---- + +The https://github.com/apache/camel-quarkus/tree/main/integration-tests/protobuf[camel-quarkus-protobuf integration test] is a good way to learn more. diff --git a/extensions/protobuf/runtime/src/main/doc/configuration.adoc b/extensions/protobuf/runtime/src/main/doc/configuration.adoc index 068cfbe..e33d89a 100644 --- a/extensions/protobuf/runtime/src/main/doc/configuration.adoc +++ b/extensions/protobuf/runtime/src/main/doc/configuration.adoc @@ -21,5 +21,21 @@ service and message definitions stored in the `src/main/proto` directory: </build> ---- -You may want to check the https://github.com/apache/camel-quarkus/tree/main/integration-tests/protobuf[integration test] -in our source tree as an example. +Moreover, please note that some additional configurations might be needed when using `contentTypeFormat=json`. +Indeed, in such a case, the generated `Builder` class needs to be registered for reflection. + +For instance, let's examine the `ProtobufDataFormat` below: + +[source,java] +---- +ProtobufDataFormat protobufJsonDataFormat = new ProtobufDataFormat(Person.getDefaultInstance(), ProtobufDataFormat.CONTENT_TYPE_FORMAT_JSON); +---- + +In such a case, the `Person.Builder` class should be xref:user-guide/native-mode.adoc#reflection[registered for reflection], for instance as below: + +[source,java] +---- +@RegisterForReflection(targets = { org.apache.camel.quarkus.component.protobuf.it.model.AddressBookProtos.Person.Builder.class }) +---- + +The https://github.com/apache/camel-quarkus/tree/main/integration-tests/protobuf[camel-quarkus-protobuf integration test] is a good way to learn more. diff --git a/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufResource.java b/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufResource.java index 3cc02a5..1b7d92d 100644 --- a/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufResource.java +++ b/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufResource.java @@ -39,7 +39,7 @@ public class ProtobufResource { @Path("/marshal") @GET @Produces(MediaType.APPLICATION_OCTET_STREAM) - public byte[] xstreamXmlMarshal(@QueryParam("id") int id, @QueryParam("name") String name) { + public byte[] marshal(@QueryParam("id") int id, @QueryParam("name") String name) { final Person person = Person.newBuilder() .setId(id) .setName(name) @@ -56,4 +56,24 @@ public class ProtobufResource { return "{\"name\": \"" + person.getName() + "\",\"id\": " + person.getId() + "}"; } + @Path("/marshal-json") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String marshalJson(@QueryParam("id") int id, @QueryParam("name") String name) { + final Person person = Person.newBuilder() + .setId(id) + .setName(name) + .build(); + return producerTemplate.requestBody("direct:protobuf-marshal-json", person, String.class); + } + + @Path("/unmarshal-json") + @POST + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.TEXT_PLAIN) + public String unmarshalJson(String body) { + final Person person = producerTemplate.requestBody("direct:protobuf-unmarshal-json", body, Person.class); + return person.getName() + " - " + person.getId(); + } + } diff --git a/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufRoute.java b/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufRoute.java index f74d60e..9722977 100644 --- a/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufRoute.java +++ b/integration-tests/protobuf/src/main/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufRoute.java @@ -16,9 +16,13 @@ */ package org.apache.camel.quarkus.component.protobuf.it; +import io.quarkus.runtime.annotations.RegisterForReflection; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.dataformat.protobuf.ProtobufDataFormat; import org.apache.camel.quarkus.component.protobuf.it.model.AddressBookProtos.Person; +@RegisterForReflection(targets = { + org.apache.camel.quarkus.component.protobuf.it.model.AddressBookProtos.Person.Builder.class }) public class ProtobufRoute extends RouteBuilder { @Override @@ -31,5 +35,15 @@ public class ProtobufRoute extends RouteBuilder { .unmarshal() .protobuf(Person.class.getName()); + ProtobufDataFormat protobufJsonDataFormat = new ProtobufDataFormat(Person.getDefaultInstance(), + ProtobufDataFormat.CONTENT_TYPE_FORMAT_JSON); + + from("direct:protobuf-marshal-json") + .marshal(protobufJsonDataFormat) + .end(); + + from("direct:protobuf-unmarshal-json") + .unmarshal(protobufJsonDataFormat) + .end(); } } diff --git a/integration-tests/protobuf/src/test/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufTest.java b/integration-tests/protobuf/src/test/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufTest.java index 8a019b6..9a1f7d3 100644 --- a/integration-tests/protobuf/src/test/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufTest.java +++ b/integration-tests/protobuf/src/test/java/org/apache/camel/quarkus/component/protobuf/it/ProtobufTest.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; @QuarkusTest class ProtobufTest { @@ -64,4 +65,30 @@ class ProtobufTest { .body(equalTo(json)); } + + @Test + void marshalJson() { + + RestAssured.given() + .queryParam("name", name) + .queryParam("id", id) + .get("/protobuf/marshal-json") + .then() + .statusCode(200) + .body("name", equalTo(name)) + .body("id", equalTo(id)); + + } + + @Test + void unmarshalJson() { + + RestAssured.given() + .body(json) + .post("/protobuf/unmarshal-json") + .then() + .statusCode(200) + .body(is("Joe - 2345")); + + } }
