[Java][Spring] option whether to generate required args constructor (#14941) (fix #14934)

This commit is contained in:
Oleh Kurpiak
2023-03-14 10:39:58 +02:00
committed by GitHub
parent 3d12510e1f
commit bda2501455
28 changed files with 16 additions and 378 deletions

View File

@@ -10,3 +10,4 @@ additionalProperties:
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
generatedConstructorWithRequiredArgs: "false"

View File

@@ -8,3 +8,4 @@ additionalProperties:
snapshotVersion: "true"
hideGenerationTimestamp: "true"
modelNameSuffix: 'Dto'
generatedConstructorWithRequiredArgs: "false"

View File

@@ -56,6 +56,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|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|
|fullJavaUtil|whether to use fully qualified name for classes under java.util. This option only works for Java API client| |false|
|generatedConstructorWithRequiredArgs|Whether to generate constructors with required args for models| |true|
|groupId|groupId in generated pom.xml| |org.openapitools|
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |false|

View File

@@ -49,6 +49,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|ensureUniqueParams|Whether to ensure parameter names are unique in an operation (rename parameters that are not).| |true|
|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|
|fullJavaUtil|whether to use fully qualified name for classes under java.util. This option only works for Java API client| |false|
|generatedConstructorWithRequiredArgs|Whether to generate constructors with required args for models| |true|
|groupId|groupId in generated pom.xml| |org.openapitools|
|hateoas|Use Spring HATEOAS library to allow adding HATEOAS links| |false|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |false|

View File

@@ -96,6 +96,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String SINGLE_CONTENT_TYPES = "singleContentTypes";
public static final String VIRTUAL_SERVICE = "virtualService";
public static final String SKIP_DEFAULT_INTERFACE = "skipDefaultInterface";
public static final String GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS = "generatedConstructorWithRequiredArgs";
public static final String ASYNC = "async";
public static final String REACTIVE = "reactive";
@@ -157,6 +158,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean useSpringController = false;
protected boolean useSwaggerUI = true;
protected boolean useSpringBoot3 = false;
protected boolean generatedConstructorWithRequiredArgs = true;
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
public SpringCodegen() {
@@ -248,6 +250,9 @@ public class SpringCodegen extends AbstractJavaCodegen
cliOptions.add(CliOption.newBoolean(USE_SPRING_BOOT3,
"Generate code and provide dependencies for use with Spring Boot 3.x. (Use jakarta instead of javax in imports). Enabling this option will also enable `useJakartaEe`.",
useSpringBoot3));
cliOptions.add(CliOption.newBoolean(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS,
"Whether to generate constructors with required args for models",
generatedConstructorWithRequiredArgs));
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
@@ -457,6 +462,11 @@ public class SpringCodegen extends AbstractJavaCodegen
}
writePropertyBack(SPRING_CONTROLLER, useSpringController);
if (additionalProperties.containsKey(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS)) {
this.generatedConstructorWithRequiredArgs = convertPropertyToBoolean(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS);
}
writePropertyBack(GENERATE_CONSTRUCTOR_WITH_REQUIRED_ARGS, generatedConstructorWithRequiredArgs);
if (additionalProperties.containsKey(RETURN_SUCCESS_CODE)) {
this.setReturnSuccessCode(Boolean.parseBoolean(additionalProperties.get(RETURN_SUCCESS_CODE).toString()));
}

View File

@@ -80,6 +80,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/openApiNullable}}
{{/isContainer}}
{{/vars}}
{{#generatedConstructorWithRequiredArgs}}
{{#hasRequired}}
/**
@@ -110,6 +111,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/vars}}
}
{{/hasRequired}}
{{/generatedConstructorWithRequiredArgs}}
{{#vars}}
{{! begin feature: fluent setter methods }}

View File

@@ -43,22 +43,6 @@ public class AnimalDto {
@JsonProperty("color")
private String color = "red";
/**
* Default constructor
* @deprecated Use {@link AnimalDto#AnimalDto(String)}
*/
@Deprecated
public AnimalDto() {
super();
}
/**
* Constructor with only required parameters
*/
public AnimalDto(String className) {
this.className = className;
}
public AnimalDto className(String className) {
this.className = className;
return this;

View File

@@ -69,22 +69,6 @@ public class BigCatDto extends CatDto {
@JsonProperty("kind")
private KindEnum kind;
/**
* Default constructor
* @deprecated Use {@link BigCatDto#BigCatDto(String)}
*/
@Deprecated
public BigCatDto() {
super();
}
/**
* Constructor with only required parameters
*/
public BigCatDto(String className) {
super(className);
}
public BigCatDto kind(KindEnum kind) {
this.kind = kind;
return this;

View File

@@ -37,22 +37,6 @@ public class CatDto extends AnimalDto {
@JsonProperty("declawed")
private Boolean declawed;
/**
* Default constructor
* @deprecated Use {@link CatDto#CatDto(String)}
*/
@Deprecated
public CatDto() {
super();
}
/**
* Constructor with only required parameters
*/
public CatDto(String className) {
super(className);
}
public CatDto declawed(Boolean declawed) {
this.declawed = declawed;
return this;

View File

@@ -27,22 +27,6 @@ public class CategoryDto {
@JsonProperty("name")
private String name = "default-name";
/**
* Default constructor
* @deprecated Use {@link CategoryDto#CategoryDto(String)}
*/
@Deprecated
public CategoryDto() {
super();
}
/**
* Constructor with only required parameters
*/
public CategoryDto(String name) {
this.name = name;
}
public CategoryDto id(Long id) {
this.id = id;
return this;

View File

@@ -29,22 +29,6 @@ public class DogDto extends AnimalDto {
@JsonProperty("breed")
private String breed;
/**
* Default constructor
* @deprecated Use {@link DogDto#DogDto(String)}
*/
@Deprecated
public DogDto() {
super();
}
/**
* Constructor with only required parameters
*/
public DogDto(String className) {
super(className);
}
public DogDto breed(String breed) {
this.breed = breed;
return this;

View File

@@ -182,22 +182,6 @@ public class EnumTestDto {
@JsonProperty("outerEnum")
private OuterEnumDto outerEnum;
/**
* Default constructor
* @deprecated Use {@link EnumTestDto#EnumTestDto(EnumStringRequiredEnum)}
*/
@Deprecated
public EnumTestDto() {
super();
}
/**
* Constructor with only required parameters
*/
public EnumTestDto(EnumStringRequiredEnum enumStringRequired) {
this.enumStringRequired = enumStringRequired;
}
public EnumTestDto enumString(EnumStringEnum enumString) {
this.enumString = enumString;
return this;

View File

@@ -71,25 +71,6 @@ public class FormatTestDto {
@JsonProperty("BigDecimal")
private BigDecimal bigDecimal;
/**
* Default constructor
* @deprecated Use {@link FormatTestDto#FormatTestDto(BigDecimal, byte[], LocalDate, String)}
*/
@Deprecated
public FormatTestDto() {
super();
}
/**
* Constructor with only required parameters
*/
public FormatTestDto(BigDecimal number, byte[] _byte, LocalDate date, String password) {
this.number = number;
this._byte = _byte;
this.date = date;
this.password = password;
}
public FormatTestDto integer(Integer integer) {
this.integer = integer;
return this;

View File

@@ -33,22 +33,6 @@ public class NameDto {
@JsonProperty("123Number")
private Integer _123number;
/**
* Default constructor
* @deprecated Use {@link NameDto#NameDto(Integer)}
*/
@Deprecated
public NameDto() {
super();
}
/**
* Constructor with only required parameters
*/
public NameDto(Integer name) {
this.name = name;
}
public NameDto name(Integer name) {
this.name = name;
return this;

View File

@@ -86,23 +86,6 @@ public class PetDto {
@JsonProperty("status")
private StatusEnum status;
/**
* Default constructor
* @deprecated Use {@link PetDto#PetDto(String, Set<String>)}
*/
@Deprecated
public PetDto() {
super();
}
/**
* Constructor with only required parameters
*/
public PetDto(String name, Set<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}
public PetDto id(Long id) {
this.id = id;
return this;

View File

@@ -40,26 +40,6 @@ public class TypeHolderDefaultDto {
private List<Integer> arrayItem = new ArrayList<>();
/**
* Default constructor
* @deprecated Use {@link TypeHolderDefaultDto#TypeHolderDefaultDto(String, BigDecimal, Integer, Boolean, List<Integer>)}
*/
@Deprecated
public TypeHolderDefaultDto() {
super();
}
/**
* Constructor with only required parameters
*/
public TypeHolderDefaultDto(String stringItem, BigDecimal numberItem, Integer integerItem, Boolean boolItem, List<Integer> arrayItem) {
this.stringItem = stringItem;
this.numberItem = numberItem;
this.integerItem = integerItem;
this.boolItem = boolItem;
this.arrayItem = arrayItem;
}
public TypeHolderDefaultDto stringItem(String stringItem) {
this.stringItem = stringItem;
return this;

View File

@@ -43,27 +43,6 @@ public class TypeHolderExampleDto {
private List<Integer> arrayItem = new ArrayList<>();
/**
* Default constructor
* @deprecated Use {@link TypeHolderExampleDto#TypeHolderExampleDto(String, BigDecimal, Float, Integer, Boolean, List<Integer>)}
*/
@Deprecated
public TypeHolderExampleDto() {
super();
}
/**
* Constructor with only required parameters
*/
public TypeHolderExampleDto(String stringItem, BigDecimal numberItem, Float floatItem, Integer integerItem, Boolean boolItem, List<Integer> arrayItem) {
this.stringItem = stringItem;
this.numberItem = numberItem;
this.floatItem = floatItem;
this.integerItem = integerItem;
this.boolItem = boolItem;
this.arrayItem = arrayItem;
}
public TypeHolderExampleDto stringItem(String stringItem) {
this.stringItem = stringItem;
return this;

View File

@@ -44,22 +44,6 @@ public class Animal {
@JsonProperty("color")
private String color = "red";
/**
* Default constructor
* @deprecated Use {@link Animal#Animal(String)}
*/
@Deprecated
public Animal() {
super();
}
/**
* Constructor with only required parameters
*/
public Animal(String className) {
this.className = className;
}
public Animal className(String className) {
this.className = className;
return this;

View File

@@ -69,22 +69,6 @@ public class BigCat extends Cat {
@JsonProperty("kind")
private KindEnum kind;
/**
* Default constructor
* @deprecated Use {@link BigCat#BigCat(String)}
*/
@Deprecated
public BigCat() {
super();
}
/**
* Constructor with only required parameters
*/
public BigCat(String className) {
super(className);
}
public BigCat kind(KindEnum kind) {
this.kind = kind;
return this;

View File

@@ -38,22 +38,6 @@ public class Cat extends Animal {
@JsonProperty("declawed")
private Boolean declawed;
/**
* Default constructor
* @deprecated Use {@link Cat#Cat(String)}
*/
@Deprecated
public Cat() {
super();
}
/**
* Constructor with only required parameters
*/
public Cat(String className) {
super(className);
}
public Cat declawed(Boolean declawed) {
this.declawed = declawed;
return this;

View File

@@ -27,22 +27,6 @@ public class Category {
@JsonProperty("name")
private String name = "default-name";
/**
* Default constructor
* @deprecated Use {@link Category#Category(String)}
*/
@Deprecated
public Category() {
super();
}
/**
* Constructor with only required parameters
*/
public Category(String name) {
this.name = name;
}
public Category id(Long id) {
this.id = id;
return this;

View File

@@ -29,22 +29,6 @@ public class Dog extends Animal {
@JsonProperty("breed")
private String breed;
/**
* Default constructor
* @deprecated Use {@link Dog#Dog(String)}
*/
@Deprecated
public Dog() {
super();
}
/**
* Constructor with only required parameters
*/
public Dog(String className) {
super(className);
}
public Dog breed(String breed) {
this.breed = breed;
return this;

View File

@@ -184,22 +184,6 @@ public class EnumTest {
@JsonProperty("outerEnum")
private OuterEnum outerEnum;
/**
* Default constructor
* @deprecated Use {@link EnumTest#EnumTest(EnumStringRequiredEnum)}
*/
@Deprecated
public EnumTest() {
super();
}
/**
* Constructor with only required parameters
*/
public EnumTest(EnumStringRequiredEnum enumStringRequired) {
this.enumStringRequired = enumStringRequired;
}
public EnumTest enumString(EnumStringEnum enumString) {
this.enumString = enumString;
return this;

View File

@@ -73,25 +73,6 @@ public class FormatTest {
@JsonProperty("BigDecimal")
private BigDecimal bigDecimal;
/**
* Default constructor
* @deprecated Use {@link FormatTest#FormatTest(BigDecimal, byte[], LocalDate, String)}
*/
@Deprecated
public FormatTest() {
super();
}
/**
* Constructor with only required parameters
*/
public FormatTest(BigDecimal number, byte[] _byte, LocalDate date, String password) {
this.number = number;
this._byte = _byte;
this.date = date;
this.password = password;
}
public FormatTest integer(Integer integer) {
this.integer = integer;
return this;

View File

@@ -34,22 +34,6 @@ public class Name {
@JsonProperty("123Number")
private Integer _123number;
/**
* Default constructor
* @deprecated Use {@link Name#Name(Integer)}
*/
@Deprecated
public Name() {
super();
}
/**
* Constructor with only required parameters
*/
public Name(Integer name) {
this.name = name;
}
public Name name(Integer name) {
this.name = name;
return this;

View File

@@ -86,23 +86,6 @@ public class Pet {
@JsonProperty("status")
private StatusEnum status;
/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String, Set<String>)}
*/
@Deprecated
public Pet() {
super();
}
/**
* Constructor with only required parameters
*/
public Pet(String name, Set<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}
public Pet id(Long id) {
this.id = id;
return this;

View File

@@ -40,26 +40,6 @@ public class TypeHolderDefault {
@Valid
private List<Integer> arrayItem = new ArrayList<>();
/**
* Default constructor
* @deprecated Use {@link TypeHolderDefault#TypeHolderDefault(String, BigDecimal, Integer, Boolean, List<Integer>)}
*/
@Deprecated
public TypeHolderDefault() {
super();
}
/**
* Constructor with only required parameters
*/
public TypeHolderDefault(String stringItem, BigDecimal numberItem, Integer integerItem, Boolean boolItem, List<Integer> arrayItem) {
this.stringItem = stringItem;
this.numberItem = numberItem;
this.integerItem = integerItem;
this.boolItem = boolItem;
this.arrayItem = arrayItem;
}
public TypeHolderDefault stringItem(String stringItem) {
this.stringItem = stringItem;
return this;

View File

@@ -43,27 +43,6 @@ public class TypeHolderExample {
@Valid
private List<Integer> arrayItem = new ArrayList<>();
/**
* Default constructor
* @deprecated Use {@link TypeHolderExample#TypeHolderExample(String, BigDecimal, Float, Integer, Boolean, List<Integer>)}
*/
@Deprecated
public TypeHolderExample() {
super();
}
/**
* Constructor with only required parameters
*/
public TypeHolderExample(String stringItem, BigDecimal numberItem, Float floatItem, Integer integerItem, Boolean boolItem, List<Integer> arrayItem) {
this.stringItem = stringItem;
this.numberItem = numberItem;
this.floatItem = floatItem;
this.integerItem = integerItem;
this.boolItem = boolItem;
this.arrayItem = arrayItem;
}
public TypeHolderExample stringItem(String stringItem) {
this.stringItem = stringItem;
return this;