Skip to content
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

[jaxrs-spec] fix default values for array #13076

Merged
merged 2 commits into from
Aug 4, 2022
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,46 @@ import com.fasterxml.jackson.annotation.JsonTypeName;
{{{vendorExtensions.x-class-extra-annotation}}}
{{/vendorExtensions.x-class-extra-annotation}}
public class {{classname}} {{#parent}}extends {{{.}}}{{/parent}} {{#vendorExtensions.x-implements}}{{#-first}}implements {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
{{#vars}}{{#isEnum}}{{^isContainer}}

{{>enumClass}}{{/isContainer}}{{#isContainer}}{{#mostInnerItems}}

{{>enumClass}}{{/mostInnerItems}}{{/isContainer}}{{/isEnum}}
{{#vars}}
{{#isEnum}}
{{^isContainer}}
{{>enumClass}}
{{/isContainer}}
{{#isContainer}}
{{#mostInnerItems}}
{{>enumClass}}
{{/mostInnerItems}}
{{/isContainer}}
{{/isEnum}}
{{#vendorExtensions.x-field-extra-annotation}}
{{{vendorExtensions.x-field-extra-annotation}}}
{{/vendorExtensions.x-field-extra-annotation}}
private {{#useBeanValidation}}@Valid {{/useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};{{/vars}}{{#generateBuilders}}{{^additionalProperties}}
{{#isContainer}}
private {{#useBeanValidation}}@Valid {{/useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{#required}}{{^isNullable}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isNullable}}{{#isNullable}} = null{{/isNullable}}{{/required}}{{^required}} = null{{/required}};
{{/isContainer}}
{{^isContainer}}
private {{#useBeanValidation}}@Valid {{/useBeanValidation}}{{{datatypeWithEnum}}} {{name}}{{#defaultValue}} = {{{.}}}{{/defaultValue}};
{{/isContainer}}
{{/vars}}
{{#generateBuilders}}
{{^additionalProperties}}

protected {{classname}}({{classname}}Builder<?, ?> b) {
{{#parent}}super(b);
{{/parent}}{{#vars}}this.{{name}} = b.{{name}};{{/vars}}
{{#parent}}
super(b);
{{/parent}}
{{#vars}}
this.{{name}} = b.{{name}};
{{/vars}}
}

public {{classname}}() { }{{/additionalProperties}}{{/generateBuilders}}
public {{classname}}() {
}
{{/additionalProperties}}
{{/generateBuilders}}

{{#vars}}/**
{{#vars}}
/**
{{#description}}
* {{.}}
{{/description}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,43 @@ public void testHandleDefaultValue_issue8535() throws Exception {
.assertParameterAnnotations()
.containsWithNameAndAttributes("DefaultValue", ImmutableMap.of("value", "\"true\""));
}

@Test
public void arrayNullableDefaultValueTests() throws Exception {
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

final OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_13025.yaml", null, new ParseOptions()).getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(SUPPORT_ASYNC, true); //Given support async is enabled
codegen.additionalProperties().put(INTERFACE_ONLY, true); //And only interfaces are generated

final ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen); //Using JavaJAXRSSpecServerCodegen

final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(input).generate(); //When generating files

//Then the java files are compilable
validateJavaSourceFiles(files);

//And the generated model contains correct default value for array properties (optional)
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/model/Body.java");
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/model/Body.java"),
"\nprivate @Valid List<String> arrayThatIsNull = null;\n");

//And the generated model contains correct default value for array properties (required, nullable)
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/model/BodyWithRequiredNullalble.java");
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/model/BodyWithRequiredNullalble.java"),
"\nprivate @Valid List<String> arrayThatIsNull = null;\n");

//And the generated model contains correct default value for array properties (required)
TestUtils.ensureContainsFile(files, output, "src/gen/java/org/openapitools/model/BodyWithRequired.java");
assertFileContains(output.toPath().resolve("src/gen/java/org/openapitools/model/BodyWithRequired.java"),
"\nprivate @Valid List<String> arrayThatIsNotNull = new ArrayList<>();\n");

}
}
44 changes: 44 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_13025.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
openapi: "3.0.3"
info:
version: 1.0.0
title: Array Not Null
paths:
/array/not/null:
post:
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Body"
responses:
"200":
description: Success
components:
schemas:
Body:
type: object
properties:
arrayThatIsNull:
type: array
nullable: true
items:
type: string
BodyWithRequiredNullalble:
type: object
required:
- arrayThatIsNull
properties:
arrayThatIsNull:
type: array
nullable: true
items:
type: string
BodyWithRequired:
type: object
required:
- arrayThatIsNotNull
properties:
arrayThatIsNotNull:
type: array
items:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@JsonTypeName("AdditionalPropertiesAnyType")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesAnyType extends HashMap<String, Object> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@JsonTypeName("AdditionalPropertiesArray")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesArray extends HashMap<String, List> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@JsonTypeName("AdditionalPropertiesBoolean")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesBoolean extends HashMap<String, Boolean> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
@JsonTypeName("AdditionalPropertiesClass")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesClass implements Serializable {

private @Valid Map<String, String> mapString = new HashMap<>();
private @Valid Map<String, BigDecimal> mapNumber = new HashMap<>();
private @Valid Map<String, Integer> mapInteger = new HashMap<>();
private @Valid Map<String, Boolean> mapBoolean = new HashMap<>();
private @Valid Map<String, List<Integer>> mapArrayInteger = new HashMap<>();
private @Valid Map<String, List<Object>> mapArrayAnytype = new HashMap<>();
private @Valid Map<String, Map<String, String>> mapMapString = new HashMap<>();
private @Valid Map<String, Map<String, Object>> mapMapAnytype = new HashMap<>();
private @Valid Map<String, String> mapString = null;
private @Valid Map<String, BigDecimal> mapNumber = null;
private @Valid Map<String, Integer> mapInteger = null;
private @Valid Map<String, Boolean> mapBoolean = null;
private @Valid Map<String, List<Integer>> mapArrayInteger = null;
private @Valid Map<String, List<Object>> mapArrayAnytype = null;
private @Valid Map<String, Map<String, String>> mapMapString = null;
private @Valid Map<String, Map<String, Object>> mapMapAnytype = null;
private @Valid Object anytype1;
private @Valid Object anytype2;
private @Valid Object anytype3;
Expand Down Expand Up @@ -70,7 +69,7 @@ public AdditionalPropertiesClass removeMapStringItem(String mapStringItem) {

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapNumber(Map<String, BigDecimal> mapNumber) {
this.mapNumber = mapNumber;
Expand Down Expand Up @@ -105,7 +104,7 @@ public AdditionalPropertiesClass removeMapNumberItem(BigDecimal mapNumberItem) {

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapInteger(Map<String, Integer> mapInteger) {
this.mapInteger = mapInteger;
Expand Down Expand Up @@ -140,7 +139,7 @@ public AdditionalPropertiesClass removeMapIntegerItem(Integer mapIntegerItem) {

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapBoolean(Map<String, Boolean> mapBoolean) {
this.mapBoolean = mapBoolean;
Expand Down Expand Up @@ -175,7 +174,7 @@ public AdditionalPropertiesClass removeMapBooleanItem(Boolean mapBooleanItem) {

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapArrayInteger(Map<String, List<Integer>> mapArrayInteger) {
this.mapArrayInteger = mapArrayInteger;
Expand Down Expand Up @@ -210,7 +209,7 @@ public AdditionalPropertiesClass removeMapArrayIntegerItem(List<Integer> mapArra

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapArrayAnytype(Map<String, List<Object>> mapArrayAnytype) {
this.mapArrayAnytype = mapArrayAnytype;
Expand Down Expand Up @@ -245,7 +244,7 @@ public AdditionalPropertiesClass removeMapArrayAnytypeItem(List<Object> mapArray

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapMapString(Map<String, Map<String, String>> mapMapString) {
this.mapMapString = mapMapString;
Expand Down Expand Up @@ -280,7 +279,7 @@ public AdditionalPropertiesClass removeMapMapStringItem(Map<String, String> mapM

return this;
}
/**
/**
**/
public AdditionalPropertiesClass mapMapAnytype(Map<String, Map<String, Object>> mapMapAnytype) {
this.mapMapAnytype = mapMapAnytype;
Expand Down Expand Up @@ -315,7 +314,7 @@ public AdditionalPropertiesClass removeMapMapAnytypeItem(Map<String, Object> map

return this;
}
/**
/**
**/
public AdditionalPropertiesClass anytype1(Object anytype1) {
this.anytype1 = anytype1;
Expand All @@ -334,7 +333,7 @@ public void setAnytype1(Object anytype1) {
this.anytype1 = anytype1;
}

/**
/**
**/
public AdditionalPropertiesClass anytype2(Object anytype2) {
this.anytype2 = anytype2;
Expand All @@ -353,7 +352,7 @@ public void setAnytype2(Object anytype2) {
this.anytype2 = anytype2;
}

/**
/**
**/
public AdditionalPropertiesClass anytype3(Object anytype3) {
this.anytype3 = anytype3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@JsonTypeName("AdditionalPropertiesInteger")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesInteger extends HashMap<String, Integer> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
@JsonTypeName("AdditionalPropertiesNumber")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesNumber extends HashMap<String, BigDecimal> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@JsonTypeName("AdditionalPropertiesObject")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesObject extends HashMap<String, Map> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@JsonTypeName("AdditionalPropertiesString")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class AdditionalPropertiesString extends HashMap<String, String> implements Serializable {

private @Valid String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
@JsonTypeName("Animal")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class Animal implements Serializable {

private @Valid String className;
private @Valid String color = "red";

Expand All @@ -51,7 +50,7 @@ public void setClassName(String className) {
this.className = className;
}

/**
/**
**/
public Animal color(String color) {
this.color = color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
@JsonTypeName("ArrayOfArrayOfNumberOnly")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class ArrayOfArrayOfNumberOnly implements Serializable {

private @Valid List<List<BigDecimal>> arrayArrayNumber = new ArrayList<>();
private @Valid List<List<BigDecimal>> arrayArrayNumber = null;

/**
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
@JsonTypeName("ArrayOfNumberOnly")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class ArrayOfNumberOnly implements Serializable {

private @Valid List<BigDecimal> arrayNumber = new ArrayList<>();
private @Valid List<BigDecimal> arrayNumber = null;

/**
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
@JsonTypeName("ArrayTest")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class ArrayTest implements Serializable {

private @Valid List<String> arrayOfString = new ArrayList<>();
private @Valid List<List<Long>> arrayArrayOfInteger = new ArrayList<>();
private @Valid List<List<ReadOnlyFirst>> arrayArrayOfModel = new ArrayList<>();
private @Valid List<String> arrayOfString = null;
private @Valid List<List<Long>> arrayArrayOfInteger = null;
private @Valid List<List<ReadOnlyFirst>> arrayArrayOfModel = null;

/**
**/
Expand Down Expand Up @@ -61,7 +60,7 @@ public ArrayTest removeArrayOfStringItem(String arrayOfStringItem) {

return this;
}
/**
/**
**/
public ArrayTest arrayArrayOfInteger(List<List<Long>> arrayArrayOfInteger) {
this.arrayArrayOfInteger = arrayArrayOfInteger;
Expand Down Expand Up @@ -96,7 +95,7 @@ public ArrayTest removeArrayArrayOfIntegerItem(List<Long> arrayArrayOfIntegerIte

return this;
}
/**
/**
**/
public ArrayTest arrayArrayOfModel(List<List<ReadOnlyFirst>> arrayArrayOfModel) {
this.arrayArrayOfModel = arrayArrayOfModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
@JsonTypeName("BigCat")
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen")
public class BigCat extends Cat implements Serializable {


public enum KindEnum {
public enum KindEnum {

LIONS(String.valueOf("lions")), TIGERS(String.valueOf("tigers")), LEOPARDS(String.valueOf("leopards")), JAGUARS(String.valueOf("jaguars"));

Expand Down
Loading