diff --git a/docs/generators/jaxrs-spec.md b/docs/generators/jaxrs-spec.md index 36656ae9e00..559e9aca5b8 100644 --- a/docs/generators/jaxrs-spec.md +++ b/docs/generators/jaxrs-spec.md @@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|
**false**
No changes to the enum's are made, this is the default option.
**true**
With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.
|false| |generateBuilders|Whether to generate builders for models| |false| |generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false| +|generateJsonCreator|Whether to generate @JsonCreator constructor for required properties.| |true| |generatePom|Whether to generate pom.xml if the file does not already exist.| |true| |groupId|groupId in generated pom.xml| |org.openapitools| |hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSCXFCDIServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSCXFCDIServerCodegen.java index 7da886e8743..d4b5b2c21cb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSCXFCDIServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSCXFCDIServerCodegen.java @@ -58,6 +58,8 @@ public class JavaJAXRSCXFCDIServerCodegen extends JavaJAXRSSpecServerCodegen imp // Updated template directory embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "cxf-cdi"; + + removeOption(JavaJAXRSSpecServerCodegen.GENERATE_JSON_CREATOR); } @Override diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java index 0127f17688a..a0092f6ad9d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java @@ -42,6 +42,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen { public static final String USE_MICROPROFILE_OPENAPI_ANNOTATIONS = "useMicroProfileOpenAPIAnnotations"; public static final String USE_MUTINY = "useMutiny"; public static final String OPEN_API_SPEC_FILE_LOCATION = "openApiSpecFileLocation"; + public static final String GENERATE_JSON_CREATOR = "generateJsonCreator"; public static final String QUARKUS_LIBRARY = "quarkus"; public static final String THORNTAIL_LIBRARY = "thorntail"; @@ -56,6 +57,9 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen { private boolean useMicroProfileOpenAPIAnnotations = false; private boolean useMutiny = false; + @Getter @Setter + protected boolean generateJsonCreator = true; + @Setter protected boolean useGzipFeature = false; /** @@ -129,6 +133,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen { cliOptions.add(CliOption.newString(OPEN_API_SPEC_FILE_LOCATION, "Location where the file containing the spec will be generated in the output folder. No file generated when set to null or empty string.")); cliOptions.add(CliOption.newBoolean(SUPPORT_ASYNC, "Wrap responses in CompletionStage type, allowing asynchronous computation (requires JAX-RS 2.1).", supportAsync)); cliOptions.add(CliOption.newBoolean(USE_MUTINY, "Whether to use Smallrye Mutiny instead of CompletionStage for asynchronous computation. Only valid when library is set to quarkus.", useMutiny)); + cliOptions.add(CliOption.newBoolean(GENERATE_JSON_CREATOR, "Whether to generate @JsonCreator constructor for required properties.", generateJsonCreator)); } @Override @@ -155,6 +160,8 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen { convertPropertyToBooleanAndWriteBack(USE_MUTINY, value -> useMutiny = value); } + convertPropertyToBooleanAndWriteBack(GENERATE_JSON_CREATOR, this::setGenerateJsonCreator); + if (additionalProperties.containsKey(OPEN_API_SPEC_FILE_LOCATION)) { openApiSpecFileLocation = additionalProperties.get(OPEN_API_SPEC_FILE_LOCATION).toString(); } else if(QUARKUS_LIBRARY.equals(library) || THORNTAIL_LIBRARY.equals(library) || HELIDON_LIBRARY.equals(library) || KUMULUZEE_LIBRARY.equals(library)) { diff --git a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache index d97de41fda3..fec3a42b1bb 100644 --- a/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache +++ b/modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache @@ -51,6 +51,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens public {{classname}}() { } + {{#generateJsonCreator}} {{#hasRequired}} @JsonCreator public {{classname}}( @@ -73,6 +74,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens } {{/hasRequired}} + {{/generateJsonCreator}} {{#vars}} /** {{#description}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java index 3463f836759..3c3dead1194 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java @@ -27,8 +27,7 @@ import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; -import static org.openapitools.codegen.TestUtils.assertFileContains; -import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles; +import static org.openapitools.codegen.TestUtils.*; import static org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen.*; import static org.openapitools.codegen.languages.features.GzipFeatures.USE_GZIP_FEATURE; import static org.testng.Assert.assertTrue; @@ -1069,4 +1068,27 @@ public class JavaJAXRSSpecServerCodegenTest extends JavaJaxrsBaseTest { .assertMethod("fromValue").bodyContainsLines("throw new IllegalArgumentException(\"Unexpected value '\" + value + \"'\");"); } + + @Test + public void disableGenerateJsonCreator() throws Exception { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/3_0/required-properties.yaml", null, new ParseOptions()).getOpenAPI(); + + codegen.setOutputDir(output.getAbsolutePath()); + ((JavaJAXRSSpecServerCodegen) codegen).setGenerateJsonCreator(false); + + ClientOptInput input = new ClientOptInput() + .openAPI(openAPI) + .config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + assertFileNotContains(files.get("RequiredProperties.java").toPath(), "@JsonCreator"); + } + } diff --git a/modules/openapi-generator/src/test/resources/3_0/required-properties.yaml b/modules/openapi-generator/src/test/resources/3_0/required-properties.yaml new file mode 100644 index 00000000000..de996c8ebec --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/required-properties.yaml @@ -0,0 +1,32 @@ +openapi: 3.0.3 +info: + title: Title + description: Title + version: 1.0.0 +servers: + - url: 'https' +paths: + '/user': + get: + responses: + 200: + description: "success" + content: + application/json: + schema: + $ref: '#/components/schemas/RequiredProperties' + +components: + schemas: + RequiredProperties: + type: object + required: + - a + - b + properties: + a: + type: string + b: + type: string + c: + type: string