Skip to content

Commit 9a9c108

Browse files
authored
Add config option to disable generation of @JsonCreator constructor (#20570)
1 parent 248a78b commit 9a9c108

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

docs/generators/jaxrs-spec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
4444
|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.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>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.</dd></dl>|false|
4545
|generateBuilders|Whether to generate builders for models| |false|
4646
|generateConstructorWithAllArgs|whether to generate a constructor for all arguments| |false|
47+
|generateJsonCreator|Whether to generate @JsonCreator constructor for required properties.| |true|
4748
|generatePom|Whether to generate pom.xml if the file does not already exist.| |true|
4849
|groupId|groupId in generated pom.xml| |org.openapitools|
4950
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSCXFCDIServerCodegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public JavaJAXRSCXFCDIServerCodegen() {
5858

5959
// Updated template directory
6060
embeddedTemplateDir = templateDir = JAXRS_TEMPLATE_DIRECTORY_NAME + File.separator + "cxf-cdi";
61+
62+
removeOption(JavaJAXRSSpecServerCodegen.GENERATE_JSON_CREATOR);
6163
}
6264

6365
@Override

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
4242
public static final String USE_MICROPROFILE_OPENAPI_ANNOTATIONS = "useMicroProfileOpenAPIAnnotations";
4343
public static final String USE_MUTINY = "useMutiny";
4444
public static final String OPEN_API_SPEC_FILE_LOCATION = "openApiSpecFileLocation";
45+
public static final String GENERATE_JSON_CREATOR = "generateJsonCreator";
4546

4647
public static final String QUARKUS_LIBRARY = "quarkus";
4748
public static final String THORNTAIL_LIBRARY = "thorntail";
@@ -56,6 +57,9 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen {
5657
private boolean useMicroProfileOpenAPIAnnotations = false;
5758
private boolean useMutiny = false;
5859

60+
@Getter @Setter
61+
protected boolean generateJsonCreator = true;
62+
5963
@Setter
6064
protected boolean useGzipFeature = false;
6165
/**
@@ -129,6 +133,7 @@ public JavaJAXRSSpecServerCodegen() {
129133
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."));
130134
cliOptions.add(CliOption.newBoolean(SUPPORT_ASYNC, "Wrap responses in CompletionStage type, allowing asynchronous computation (requires JAX-RS 2.1).", supportAsync));
131135
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));
136+
cliOptions.add(CliOption.newBoolean(GENERATE_JSON_CREATOR, "Whether to generate @JsonCreator constructor for required properties.", generateJsonCreator));
132137
}
133138

134139
@Override
@@ -155,6 +160,8 @@ public void processOpts() {
155160
convertPropertyToBooleanAndWriteBack(USE_MUTINY, value -> useMutiny = value);
156161
}
157162

163+
convertPropertyToBooleanAndWriteBack(GENERATE_JSON_CREATOR, this::setGenerateJsonCreator);
164+
158165
if (additionalProperties.containsKey(OPEN_API_SPEC_FILE_LOCATION)) {
159166
openApiSpecFileLocation = additionalProperties.get(OPEN_API_SPEC_FILE_LOCATION).toString();
160167
} else if(QUARKUS_LIBRARY.equals(library) || THORNTAIL_LIBRARY.equals(library) || HELIDON_LIBRARY.equals(library) || KUMULUZEE_LIBRARY.equals(library)) {

modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
5151
public {{classname}}() {
5252
}
5353

54+
{{#generateJsonCreator}}
5455
{{#hasRequired}}
5556
@JsonCreator
5657
public {{classname}}(
@@ -73,6 +74,7 @@ public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtens
7374
}
7475

7576
{{/hasRequired}}
77+
{{/generateJsonCreator}}
7678
{{#vars}}
7779
/**
7880
{{#description}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import java.util.function.Function;
2828
import java.util.stream.Collectors;
2929

30-
import static org.openapitools.codegen.TestUtils.assertFileContains;
31-
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
30+
import static org.openapitools.codegen.TestUtils.*;
3231
import static org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen.*;
3332
import static org.openapitools.codegen.languages.features.GzipFeatures.USE_GZIP_FEATURE;
3433
import static org.testng.Assert.assertTrue;
@@ -1069,4 +1068,27 @@ public void testEnumUnknownDefaultCaseDeserializationNotSet_issue13444() throws
10691068
.assertMethod("fromValue").bodyContainsLines("throw new IllegalArgumentException(\"Unexpected value '\" + value + \"'\");");
10701069

10711070
}
1071+
1072+
@Test
1073+
public void disableGenerateJsonCreator() throws Exception {
1074+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
1075+
output.deleteOnExit();
1076+
1077+
OpenAPI openAPI = new OpenAPIParser()
1078+
.readLocation("src/test/resources/3_0/required-properties.yaml", null, new ParseOptions()).getOpenAPI();
1079+
1080+
codegen.setOutputDir(output.getAbsolutePath());
1081+
((JavaJAXRSSpecServerCodegen) codegen).setGenerateJsonCreator(false);
1082+
1083+
ClientOptInput input = new ClientOptInput()
1084+
.openAPI(openAPI)
1085+
.config(codegen);
1086+
1087+
DefaultGenerator generator = new DefaultGenerator();
1088+
Map<String, File> files = generator.opts(input).generate().stream()
1089+
.collect(Collectors.toMap(File::getName, Function.identity()));
1090+
1091+
assertFileNotContains(files.get("RequiredProperties.java").toPath(), "@JsonCreator");
1092+
}
1093+
10721094
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Title
4+
description: Title
5+
version: 1.0.0
6+
servers:
7+
- url: 'https'
8+
paths:
9+
'/user':
10+
get:
11+
responses:
12+
200:
13+
description: "success"
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/RequiredProperties'
18+
19+
components:
20+
schemas:
21+
RequiredProperties:
22+
type: object
23+
required:
24+
- a
25+
- b
26+
properties:
27+
a:
28+
type: string
29+
b:
30+
type: string
31+
c:
32+
type: string

0 commit comments

Comments
 (0)