Skip to content

Commit

Permalink
[Java][Spring] option whether to generate required args constructor (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
borsch authored Mar 14, 2023
1 parent 3d12510 commit bda2501
Show file tree
Hide file tree
Showing 28 changed files with 16 additions and 378 deletions.
1 change: 1 addition & 0 deletions bin/configs/spring-cloud-oas3-fakeapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ additionalProperties:
interfaceOnly: "true"
singleContentTypes: "true"
hideGenerationTimestamp: "true"
generatedConstructorWithRequiredArgs: "false"
1 change: 1 addition & 0 deletions bin/configs/spring-http-interface.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ additionalProperties:
snapshotVersion: "true"
hideGenerationTimestamp: "true"
modelNameSuffix: 'Dto'
generatedConstructorWithRequiredArgs: "false"
1 change: 1 addition & 0 deletions docs/generators/java-camel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -157,6 +158,7 @@ public String getDescription() {
protected boolean useSpringController = false;
protected boolean useSwaggerUI = true;
protected boolean useSpringBoot3 = false;
protected boolean generatedConstructorWithRequiredArgs = true;
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;

public SpringCodegen() {
Expand Down Expand Up @@ -248,6 +250,9 @@ public SpringCodegen() {
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,
Expand Down Expand Up @@ -457,6 +462,11 @@ public void processOpts() {
}
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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/openApiNullable}}
{{/isContainer}}
{{/vars}}
{{#generatedConstructorWithRequiredArgs}}
{{#hasRequired}}

/**
Expand Down Expand Up @@ -110,6 +111,7 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/vars}}
}
{{/hasRequired}}
{{/generatedConstructorWithRequiredArgs}}
{{#vars}}

{{! begin feature: fluent setter methods }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ public static KindEnum fromValue(String value) {
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,6 @@ public static EnumNumberEnum fromValue(Double value) {
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ public static StatusEnum fromValue(String value) {
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ public static KindEnum fromValue(String value) {
@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;
Expand Down
Loading

0 comments on commit bda2501

Please sign in to comment.