Add config option to disable generation of @JsonCreator constructor (#20570)

This commit is contained in:
Jimmy Praet 2025-02-05 09:00:03 +01:00 committed by GitHub
parent 248a78b894
commit 9a9c1087be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 68 additions and 2 deletions

View File

@ -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.|<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|
|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|

View File

@ -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

View File

@ -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)) {

View File

@ -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}}

View File

@ -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<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));
assertFileNotContains(files.get("RequiredProperties.java").toPath(), "@JsonCreator");
}
}

View File

@ -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