Skip to content

Add constructor with required parameter for Spring #14822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public List<CodegenProperty> readOnlyVars = new ArrayList<>(); // a list of read-only properties
public List<CodegenProperty> readWriteVars = new ArrayList<>(); // a list of properties for read, write
public List<CodegenProperty> parentVars = new ArrayList<>();
public List<CodegenProperty> parentRequiredVars = new ArrayList<>();
public List<CodegenProperty> nonNullableVars = new ArrayList<>(); // a list of non-nullable properties
public Map<String, Object> allowableValues;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,10 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
codegenModel.getImports().add(imp);
}
}

if (property.required) {
codegenModel.parentRequiredVars.add(parentVar.clone());
}
}
}
parentCodegenModel = parentCodegenModel.getParentModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ public class {{classname}}{{#parent}} extends {{{parent}}}{{/parent}}{{^parent}}
{{/openApiNullable}}
{{/isContainer}}
{{/vars}}
{{#hasRequired}}

/**
* Default constructor
* @deprecated Use {@link {{classname}}#{{classname}}({{#requiredVars}}{{{datatypeWithEnum}}}{{^-last}}, {{/-last}}{{/requiredVars}})}
*/
@Deprecated
public {{classname}}() {
super();
}

/**
* Constructor with only required parameters
*/
public {{classname}}({{#requiredVars}}{{{datatypeWithEnum}}} {{name}}{{^-last}}, {{/-last}}{{/requiredVars}}) {
{{#parent}}
super({{#parentRequiredVars}}{{name}}{{^-last}}, {{/-last}}{{/parentRequiredVars}});
{{/parent}}
{{#vars}}
{{#required}}
{{#openApiNullable}}
this.{{name}} = {{#isNullable}}JsonNullable.of({{name}}){{/isNullable}}{{^isNullable}}{{name}}{{/isNullable}};
{{/openApiNullable}}
{{^openApiNullable}}
this.{{name}} = {{name}};
{{/openApiNullable}}
{{/required}}
{{/vars}}
}
{{/hasRequired}}
{{#vars}}

{{! begin feature: fluent setter methods }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public ConstructorAssert assertConstructor(final String... paramTypes) {
return new ConstructorAssert(this, constructorDeclaration.get());
}

public JavaFileAssert assertNoConstructor(final String... paramTypes) {
Optional<ConstructorDeclaration> constructorDeclaration = actual.getType(0).getConstructorByParameterTypes(paramTypes);
Assertions.assertThat(constructorDeclaration)
.withFailMessage("Found constructor with parameter(s) %s", Arrays.toString(paramTypes))
.isEmpty();

return this;
}

public PropertyAssert hasProperty(final String propertyName) {
Optional<FieldDeclaration> fieldOptional = actual.getType(0).getMembers().stream()
.filter(FieldDeclaration.class::isInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

import com.github.javaparser.ast.nodeTypes.NodeWithName;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
Expand All @@ -45,8 +44,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.validation.Valid;

import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.CliOption;
Expand Down Expand Up @@ -2041,6 +2038,25 @@ public void shouldUseTheSameTagNameForTheInterfaceAndTheMethod_issue11570() thro
assertFileContains(output.get("PersonApi.java").toPath(), interfaceTag, methodTag);
}

@Test
public void shouldGenerateConstructorWithOnlyRequiredParameters() throws IOException {
final Map<String, File> output = generateFromContract("src/test/resources/3_0/spring/issue_9789.yml", SPRING_BOOT);

JavaFileAssert.assertThat(output.get("ObjectWithNoRequiredParameter.java")).assertNoConstructor("String");

JavaFileAssert.assertThat(output.get("ObjectWithRequiredParameter.java")).assertConstructor();
JavaFileAssert.assertThat(output.get("ObjectWithRequiredParameter.java")).assertConstructor("String", "String")
.hasParameter("param2").toConstructor()
.hasParameter("param3");

JavaFileAssert.assertThat(output.get("ObjectWithInheritedRequiredParameter.java")).assertConstructor();
JavaFileAssert.assertThat(output.get("ObjectWithInheritedRequiredParameter.java")).assertConstructor("Integer", "String", "String")
.hasParameter("param2").toConstructor()
.hasParameter("param3").toConstructor()
.hasParameter("param6").toConstructor()
.bodyContainsLines("super(param2, param3)", "this.param6 = param6");
}

private Map<String, File> generateFromContract(String url, String library) throws IOException {
return generateFromContract(url, library, new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
openapi: 3.0.1
info:
title: Test Issue #9789
version: v1
paths:
/test:
get:
responses:
'200':
description: default response
content:
'*/*':
schema:
$ref: '#/components/schemas/ObjectWithNoRequiredParameter'
components:
schemas:
ObjectWithNoRequiredParameter:
type: object
properties:
param1:
type: string
ObjectWithRequiredParameter:
type: object
properties:
param1:
type: string
param2:
type: string
param3:
type: string
nullable: true
param4:
type: string
required:
- param2
- param3
discriminator:
propertyName: param1
ObjectWithInheritedRequiredParameter:
allOf:
- $ref: '#/components/schemas/ObjectWithRequiredParameter'
- type: object
properties:
param5:
type: boolean
param6:
type: integer
param7:
type: number
required:
- param6

Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ public static StatusEnum fromValue(String value) {
@JsonProperty("status")
private StatusEnum status;

/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String, List<String>)}
*/
@Deprecated
public Pet() {
super();
}

/**
* Constructor with only required parameters
*/
public Pet(String name, List<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}

public Pet id(Long id) {
this.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public class Pet {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfBirth = LocalDate.parse("2021-01-01");

/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String)}
*/
@Deprecated
public Pet() {
super();
}

/**
* Constructor with only required parameters
*/
public Pet(String atType) {
this.atType = atType;
}

public Pet atType(String atType) {
this.atType = atType;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ public static StatusEnum fromValue(String value) {
@JsonProperty("status")
private StatusEnum status;

/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String, List<String>)}
*/
@Deprecated
public Pet() {
super();
}

/**
* Constructor with only required parameters
*/
public Pet(String name, List<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}

public Pet id(Long id) {
this.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ public static StatusEnum fromValue(String value) {
@JsonProperty("status")
private StatusEnum status;

/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String, List<String>)}
*/
@Deprecated
public Pet() {
super();
}

/**
* Constructor with only required parameters
*/
public Pet(String name, List<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}

public Pet id(Long id) {
this.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ public static StatusEnum fromValue(String value) {
@JsonProperty("status")
private StatusEnum status;

/**
* Default constructor
* @deprecated Use {@link Pet#Pet(String, List<String>)}
*/
@Deprecated
public Pet() {
super();
}

/**
* Constructor with only required parameters
*/
public Pet(String name, List<String> photoUrls) {
this.name = name;
this.photoUrls = photoUrls;
}

public Pet id(Long id) {
this.id = id;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ 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 @@ -67,6 +67,22 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ 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;
Expand Down
Loading