[Java] better default value handling (#14130)

* add test for array default value

* update null return

* minor fixes

* move default value tests to echo api spec

* add new files

* remove unused files

* fix enum array default, add tests

* better array init

* Update modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Co-authored-by: Leonard Brünings <lord_damokles@gmx.net>

* Update modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Co-authored-by: Leonard Brünings <lord_damokles@gmx.net>

* revert the fix

* improve default value handling

* update native samples, add tests

* update samples

* fix tests

* use conditional test for timezone

* add tests to apache http client echo api

* add option to default container to null

* fix map default value

* minor refactoring

* update samples

* fix javadoc

* fix pom.xml

* add tests in java native echo client

* add java apache client echo tests

* fix test

* fix test

---------

Co-authored-by: Leonard Brünings <lord_damokles@gmx.net>
This commit is contained in:
William Cheng
2023-01-30 20:19:00 +08:00
committed by GitHub
parent b3527348f3
commit fd45b74128
290 changed files with 3687 additions and 650 deletions

View File

@@ -0,0 +1,24 @@
# ArrayDefaultValue
## Properties
| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**id** | **Long** | id | [optional] |
|**outcomes** | [**List&lt;OutcomesEnum&gt;**](#List&lt;OutcomesEnum&gt;) | | [optional] |
## Enum: List&lt;OutcomesEnum&gt;
| Name | Value |
|---- | -----|
| SUCCESS | &quot;SUCCESS&quot; |
| FAILURE | &quot;FAILURE&quot; |
| SKIPPED | &quot;SKIPPED&quot; |

View File

@@ -50,10 +50,10 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class AdditionalPropertiesClass {
public static final String JSON_PROPERTY_MAP_PROPERTY = "map_property";
private Map<String, String> mapProperty = null;
private Map<String, String> mapProperty = new HashMap<>();
public static final String JSON_PROPERTY_MAP_OF_MAP_PROPERTY = "map_of_map_property";
private Map<String, Map<String, String>> mapOfMapProperty = null;
private Map<String, Map<String, String>> mapOfMapProperty = new HashMap<>();
public static final String JSON_PROPERTY_ANYTYPE1 = "anytype_1";
private JsonNullable<Object> anytype1 = JsonNullable.<Object>of(null);
@@ -65,13 +65,13 @@ public class AdditionalPropertiesClass {
private Object mapWithUndeclaredPropertiesAnytype2;
public static final String JSON_PROPERTY_MAP_WITH_UNDECLARED_PROPERTIES_ANYTYPE3 = "map_with_undeclared_properties_anytype_3";
private Map<String, Object> mapWithUndeclaredPropertiesAnytype3 = null;
private Map<String, Object> mapWithUndeclaredPropertiesAnytype3 = new HashMap<>();
public static final String JSON_PROPERTY_EMPTY_MAP = "empty_map";
private Object emptyMap;
public static final String JSON_PROPERTY_MAP_WITH_UNDECLARED_PROPERTIES_STRING = "map_with_undeclared_properties_string";
private Map<String, String> mapWithUndeclaredPropertiesString = null;
private Map<String, String> mapWithUndeclaredPropertiesString = new HashMap<>();
public AdditionalPropertiesClass() {
}

View File

@@ -0,0 +1,186 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.model;
import java.util.Objects;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* ArrayDefaultValue
*/
@JsonPropertyOrder({
ArrayDefaultValue.JSON_PROPERTY_ID,
ArrayDefaultValue.JSON_PROPERTY_OUTCOMES
})
@javax.annotation.processing.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ArrayDefaultValue {
public static final String JSON_PROPERTY_ID = "id";
private Long id;
/**
* Gets or Sets outcomes
*/
public enum OutcomesEnum {
SUCCESS("SUCCESS"),
FAILURE("FAILURE"),
SKIPPED("SKIPPED");
private String value;
OutcomesEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return String.valueOf(value);
}
@JsonCreator
public static OutcomesEnum fromValue(String value) {
for (OutcomesEnum b : OutcomesEnum.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
public static final String JSON_PROPERTY_OUTCOMES = "outcomes";
private List<OutcomesEnum> outcomes = null;
public ArrayDefaultValue() {
}
public ArrayDefaultValue id(Long id) {
this.id = id;
return this;
}
/**
* id
* @return id
**/
@javax.annotation.Nullable
@JsonProperty(JSON_PROPERTY_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public Long getId() {
return id;
}
@JsonProperty(JSON_PROPERTY_ID)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public void setId(Long id) {
this.id = id;
}
public ArrayDefaultValue outcomes(List<OutcomesEnum> outcomes) {
this.outcomes = outcomes;
return this;
}
public ArrayDefaultValue addOutcomesItem(OutcomesEnum outcomesItem) {
if (this.outcomes == null) {
this.outcomes = new ArrayList<>();
}
this.outcomes.add(outcomesItem);
return this;
}
/**
* Get outcomes
* @return outcomes
**/
@javax.annotation.Nullable
@JsonProperty(JSON_PROPERTY_OUTCOMES)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public List<OutcomesEnum> getOutcomes() {
return outcomes;
}
@JsonProperty(JSON_PROPERTY_OUTCOMES)
@JsonInclude(value = JsonInclude.Include.USE_DEFAULTS)
public void setOutcomes(List<OutcomesEnum> outcomes) {
this.outcomes = outcomes;
}
/**
* Return true if this ArrayDefaultValue object is equal to o.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ArrayDefaultValue arrayDefaultValue = (ArrayDefaultValue) o;
return Objects.equals(this.id, arrayDefaultValue.id) &&
Objects.equals(this.outcomes, arrayDefaultValue.outcomes);
}
@Override
public int hashCode() {
return Objects.hash(id, outcomes);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class ArrayDefaultValue {\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" outcomes: ").append(toIndentedString(outcomes)).append("\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}

View File

@@ -40,7 +40,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ArrayOfArrayOfNumberOnly {
public static final String JSON_PROPERTY_ARRAY_ARRAY_NUMBER = "ArrayArrayNumber";
private List<List<BigDecimal>> arrayArrayNumber = null;
private List<List<BigDecimal>> arrayArrayNumber = new ArrayList<>();
public ArrayOfArrayOfNumberOnly() {
}

View File

@@ -40,7 +40,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ArrayOfNumberOnly {
public static final String JSON_PROPERTY_ARRAY_NUMBER = "ArrayNumber";
private List<BigDecimal> arrayNumber = null;
private List<BigDecimal> arrayNumber = new ArrayList<>();
public ArrayOfNumberOnly() {
}

View File

@@ -42,13 +42,13 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class ArrayTest {
public static final String JSON_PROPERTY_ARRAY_OF_STRING = "array_of_string";
private List<String> arrayOfString = null;
private List<String> arrayOfString = new ArrayList<>();
public static final String JSON_PROPERTY_ARRAY_ARRAY_OF_INTEGER = "array_array_of_integer";
private List<List<Long>> arrayArrayOfInteger = null;
private List<List<Long>> arrayArrayOfInteger = new ArrayList<>();
public static final String JSON_PROPERTY_ARRAY_ARRAY_OF_MODEL = "array_array_of_model";
private List<List<ReadOnlyFirst>> arrayArrayOfModel = null;
private List<List<ReadOnlyFirst>> arrayArrayOfModel = new ArrayList<>();
public ArrayTest() {
}

View File

@@ -65,7 +65,7 @@ public class Drawing extends HashMap<String, Fruit> {
private JsonNullable<NullableShape> nullableShape = JsonNullable.<NullableShape>undefined();
public static final String JSON_PROPERTY_SHAPES = "shapes";
private List<Shape> shapes = null;
private List<Shape> shapes = new ArrayList<>();
public Drawing() {
}

View File

@@ -113,7 +113,7 @@ public class EnumArrays {
}
public static final String JSON_PROPERTY_ARRAY_ENUM = "array_enum";
private List<ArrayEnumEnum> arrayEnum = null;
private List<ArrayEnumEnum> arrayEnum = new ArrayList<>();
public EnumArrays() {
}

View File

@@ -44,7 +44,7 @@ public class FileSchemaTestClass {
private ModelFile _file;
public static final String JSON_PROPERTY_FILES = "files";
private List<ModelFile> files = null;
private List<ModelFile> files = new ArrayList<>();
public FileSchemaTestClass() {
}

View File

@@ -42,7 +42,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen")
public class MapTest {
public static final String JSON_PROPERTY_MAP_MAP_OF_STRING = "map_map_of_string";
private Map<String, Map<String, String>> mapMapOfString = null;
private Map<String, Map<String, String>> mapMapOfString = new HashMap<>();
/**
* Gets or Sets inner
@@ -80,13 +80,13 @@ public class MapTest {
}
public static final String JSON_PROPERTY_MAP_OF_ENUM_STRING = "map_of_enum_string";
private Map<String, InnerEnum> mapOfEnumString = null;
private Map<String, InnerEnum> mapOfEnumString = new HashMap<>();
public static final String JSON_PROPERTY_DIRECT_MAP = "direct_map";
private Map<String, Boolean> directMap = null;
private Map<String, Boolean> directMap = new HashMap<>();
public static final String JSON_PROPERTY_INDIRECT_MAP = "indirect_map";
private Map<String, Boolean> indirectMap = null;
private Map<String, Boolean> indirectMap = new HashMap<>();
public MapTest() {
}

View File

@@ -50,7 +50,7 @@ public class MixedPropertiesAndAdditionalPropertiesClass {
private OffsetDateTime dateTime;
public static final String JSON_PROPERTY_MAP = "map";
private Map<String, Animal> map = null;
private Map<String, Animal> map = new HashMap<>();
public MixedPropertiesAndAdditionalPropertiesClass() {
}

View File

@@ -87,7 +87,7 @@ public class NullableClass extends HashMap<String, Object> {
private JsonNullable<List<Object>> arrayAndItemsNullableProp = JsonNullable.<List<Object>>undefined();
public static final String JSON_PROPERTY_ARRAY_ITEMS_NULLABLE = "array_items_nullable";
private List<Object> arrayItemsNullable = null;
private List<Object> arrayItemsNullable = new ArrayList<>();
public static final String JSON_PROPERTY_OBJECT_NULLABLE_PROP = "object_nullable_prop";
private JsonNullable<Map<String, Object>> objectNullableProp = JsonNullable.<Map<String, Object>>undefined();
@@ -96,7 +96,7 @@ public class NullableClass extends HashMap<String, Object> {
private JsonNullable<Map<String, Object>> objectAndItemsNullableProp = JsonNullable.<Map<String, Object>>undefined();
public static final String JSON_PROPERTY_OBJECT_ITEMS_NULLABLE = "object_items_nullable";
private Map<String, Object> objectItemsNullable = null;
private Map<String, Object> objectItemsNullable = new HashMap<>();
public NullableClass() {
}
@@ -306,7 +306,7 @@ public class NullableClass extends HashMap<String, Object> {
public NullableClass addArrayNullablePropItem(Object arrayNullablePropItem) {
if (this.arrayNullableProp == null || !this.arrayNullableProp.isPresent()) {
this.arrayNullableProp = JsonNullable.<List<Object>>of(new ArrayList<>());
this.arrayNullableProp = JsonNullable.<List<Object>>of(null);
}
try {
this.arrayNullableProp.get().add(arrayNullablePropItem);
@@ -351,7 +351,7 @@ public class NullableClass extends HashMap<String, Object> {
public NullableClass addArrayAndItemsNullablePropItem(Object arrayAndItemsNullablePropItem) {
if (this.arrayAndItemsNullableProp == null || !this.arrayAndItemsNullableProp.isPresent()) {
this.arrayAndItemsNullableProp = JsonNullable.<List<Object>>of(new ArrayList<>());
this.arrayAndItemsNullableProp = JsonNullable.<List<Object>>of(null);
}
try {
this.arrayAndItemsNullableProp.get().add(arrayAndItemsNullablePropItem);
@@ -429,7 +429,7 @@ public class NullableClass extends HashMap<String, Object> {
public NullableClass putObjectNullablePropItem(String key, Object objectNullablePropItem) {
if (this.objectNullableProp == null || !this.objectNullableProp.isPresent()) {
this.objectNullableProp = JsonNullable.<Map<String, Object>>of(new HashMap<>());
this.objectNullableProp = JsonNullable.<Map<String, Object>>of(null);
}
try {
this.objectNullableProp.get().put(key, objectNullablePropItem);
@@ -474,7 +474,7 @@ public class NullableClass extends HashMap<String, Object> {
public NullableClass putObjectAndItemsNullablePropItem(String key, Object objectAndItemsNullablePropItem) {
if (this.objectAndItemsNullableProp == null || !this.objectAndItemsNullableProp.isPresent()) {
this.objectAndItemsNullableProp = JsonNullable.<Map<String, Object>>of(new HashMap<>());
this.objectAndItemsNullableProp = JsonNullable.<Map<String, Object>>of(null);
}
try {
this.objectAndItemsNullableProp.get().put(key, objectAndItemsNullablePropItem);

View File

@@ -53,7 +53,7 @@ public class ObjectWithDeprecatedFields {
private DeprecatedObject deprecatedRef;
public static final String JSON_PROPERTY_BARS = "bars";
private List<String> bars = null;
private List<String> bars = new ArrayList<>();
public ObjectWithDeprecatedFields() {
}

View File

@@ -58,7 +58,7 @@ public class Pet {
private List<String> photoUrls = new ArrayList<>();
public static final String JSON_PROPERTY_TAGS = "tags";
private List<Tag> tags = null;
private List<Tag> tags = new ArrayList<>();
/**
* pet status in the store

View File

@@ -0,0 +1,58 @@
/*
* OpenAPI Petstore
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.client.model;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
/**
* Model tests for ArrayDefaultValue
*/
public class ArrayDefaultValueTest {
private final ArrayDefaultValue model = new ArrayDefaultValue();
/**
* Model tests for ArrayDefaultValue
*/
@Test
public void testArrayDefaultValue() {
// TODO: test ArrayDefaultValue
}
/**
* Test the property 'id'
*/
@Test
public void idTest() {
// TODO: test id
}
/**
* Test the property 'outcomes'
*/
@Test
public void outcomesTest() {
// TODO: test outcomes
}
}