Skip to content

Commit

Permalink
[java] fix default value - empty list [] (OpenAPITools#15862)
Browse files Browse the repository at this point in the history
* fix empty default value in java okhttp-gson

* add files
  • Loading branch information
wing328 authored Jun 20, 2023
1 parent 396c675 commit 14fc61a
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,8 @@ public String toArrayDefaultValue(CodegenProperty cp, Schema schema) {

if (schema.getDefault() instanceof ArrayNode) { // array of default values
ArrayNode _default = (ArrayNode) schema.getDefault();
if (_default.isEmpty()) {
return "null";
if (_default.isEmpty()) { // e.g. default: []
return "new ArrayList<>()";
}

List<String> final_values = _values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2213,3 +2213,14 @@ components:
enum:
- type_a
- type_b
ArrayDefault:
properties:
WithDefaultEmptyBracket:
default: []
items:
type: string
type: array
WithoutDefault:
items:
type: string
type: array
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ docs/Animal.md
docs/AnotherFakeApi.md
docs/Apple.md
docs/AppleReq.md
docs/ArrayDefault.md
docs/ArrayOfArrayOfNumberOnly.md
docs/ArrayOfInlineAllOf.md
docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md
Expand Down Expand Up @@ -130,6 +131,7 @@ src/main/java/org/openapitools/client/model/AdditionalPropertiesClass.java
src/main/java/org/openapitools/client/model/Animal.java
src/main/java/org/openapitools/client/model/Apple.java
src/main/java/org/openapitools/client/model/AppleReq.java
src/main/java/org/openapitools/client/model/ArrayDefault.java
src/main/java/org/openapitools/client/model/ArrayOfArrayOfNumberOnly.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOf.java
src/main/java/org/openapitools/client/model/ArrayOfInlineAllOfArrayAllofDogPropertyInner.java
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/java/okhttp-gson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Class | Method | HTTP request | Description
- [Animal](docs/Animal.md)
- [Apple](docs/Apple.md)
- [AppleReq](docs/AppleReq.md)
- [ArrayDefault](docs/ArrayDefault.md)
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfInlineAllOf](docs/ArrayOfInlineAllOf.md)
- [ArrayOfInlineAllOfArrayAllofDogPropertyInner](docs/ArrayOfInlineAllOfArrayAllofDogPropertyInner.md)
Expand Down
11 changes: 11 additions & 0 deletions samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,17 @@ components:
required:
- enum_str_type
type: object
ArrayDefault:
properties:
WithDefaultEmptyBracket:
default: []
items:
type: string
type: array
WithoutDefault:
items:
type: string
type: array
_foo_get_default_response:
example:
string:
Expand Down
14 changes: 14 additions & 0 deletions samples/client/petstore/java/okhttp-gson/docs/ArrayDefault.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


# ArrayDefault


## Properties

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
|**withDefaultEmptyBracket** | **List&lt;String&gt;** | | [optional] |
|**withoutDefault** | **List&lt;String&gt;** | | [optional] |



Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesClass.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Apple.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.AppleReq.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayDefault.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfArrayOfNumberOnly.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOf.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfInlineAllOfArrayAllofDogPropertyInner.CustomTypeAdapterFactory());
Expand Down
Loading

0 comments on commit 14fc61a

Please sign in to comment.