From d36dabaf776cde4fcfd74918b1eee71a531fe337 Mon Sep 17 00:00:00 2001 From: Oleh Kurpiak Date: Sun, 3 Jul 2022 12:54:55 +0300 Subject: [PATCH] [Java][microprofile] fix constructor creation (#12627) --- .../Java/libraries/microprofile/pojo.mustache | 2 +- .../codegen/java/JavaClientCodegenTest.java | 28 ++++++++++ .../src/test/resources/bugs/issue_12622.json | 53 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_12622.json diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache index 84c08fbb26cf..ddec54aae100 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache @@ -72,7 +72,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}}{{#vendorExtensi @JsonbCreator public {{classname}}( {{#readOnlyVars}} - @JsonbProperty("{{baseName}}") {{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}} + @JsonbProperty(value = "{{baseName}}"{{^required}}, nillable = true{{/required}}) {{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}} {{/readOnlyVars}} ) { {{#readOnlyVars}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index 71ba4734ddb7..7b1c4215cc20 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -1513,6 +1513,34 @@ public void testMicroprofileRestClientVersion_3_0() throws Exception { output.deleteOnExit(); } + @Test + public void testMicroprofileGenerateCorrectJsonbCreator_issue12622() throws Exception { + Map properties = new HashMap<>(); + properties.put(JavaClientCodegen.MICROPROFILE_REST_CLIENT_VERSION, "3.0"); + + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setAdditionalProperties(properties) + .setGeneratorName("java") + .setLibrary(JavaClientCodegen.MICROPROFILE) + .setInputSpec("src/test/resources/bugs/issue_12622.json") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + Map files = generator.opts(clientOptInput).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert.assertThat(files.get("Foo.java")) + .printFileContent() + .fileContains( + "@JsonbProperty(value = \"b\", nillable = true) String b", + "@JsonbProperty(value = \"c\") Integer c" + ); + } + public void testExtraAnnotations(String library) throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); output.deleteOnExit(); diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_12622.json b/modules/openapi-generator/src/test/resources/bugs/issue_12622.json new file mode 100644 index 000000000000..33d760899a47 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_12622.json @@ -0,0 +1,53 @@ +{ + "openapi": "3.0.1", + "info": { + "title": "Bug Report", + "version": "1.0.0" + }, + "paths": { + "/foo": { + "get": { + "operationId": "getFoo", + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Foo" + } + } + }, + "description": "get foo" + } + } + } + } + }, + "components": { + "schemas": { + "Foo": { + "type": "object", + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "string", + "readOnly": true + }, + "c": { + "type": "integer", + "readOnly": true + }, + "d:": { + "type": "boolean" + } + }, + "required": [ + "c", + "d" + ] + } + } + } +} \ No newline at end of file