Skip to content

Commit 33fed2c

Browse files
committed
add test schema
1 parent e9791b3 commit 33fed2c

File tree

8 files changed

+402
-0
lines changed

8 files changed

+402
-0
lines changed

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2809,3 +2809,20 @@ components:
28092809
$ref: '#/components/schemas/ArrayOneOf'
28102810
anyof_prop:
28112811
$ref: '#/components/schemas/ArrayAnyOf'
2812+
NestedArrayWithDefaultValues:
2813+
type: object
2814+
properties:
2815+
nestedArray:
2816+
type: array
2817+
items:
2818+
type: array
2819+
items:
2820+
type: string
2821+
default: ["value1", "value2"]
2822+
default:
2823+
[
2824+
[ "h1", "Header 1" ],
2825+
[ "h2", "Header 2" ],
2826+
[ "h3", "Header 3" ],
2827+
[ "h4", "Header 4" ]
2828+
]

samples/client/petstore/java/okhttp-gson/.openapi-generator/FILES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ docs/ModelList.md
7171
docs/ModelReturn.md
7272
docs/ModelWithOneOfAnyOfProperties.md
7373
docs/Name.md
74+
docs/NestedArrayWithDefaultValues.md
7475
docs/NewPet.md
7576
docs/NewPetCategoryInlineAllof.md
7677
docs/NewPetCategoryInlineAllofAllOfCategoryTag.md
@@ -216,6 +217,7 @@ src/main/java/org/openapitools/client/model/ModelList.java
216217
src/main/java/org/openapitools/client/model/ModelReturn.java
217218
src/main/java/org/openapitools/client/model/ModelWithOneOfAnyOfProperties.java
218219
src/main/java/org/openapitools/client/model/Name.java
220+
src/main/java/org/openapitools/client/model/NestedArrayWithDefaultValues.java
219221
src/main/java/org/openapitools/client/model/NewPet.java
220222
src/main/java/org/openapitools/client/model/NewPetCategoryInlineAllof.java
221223
src/main/java/org/openapitools/client/model/NewPetCategoryInlineAllofAllOfCategoryTag.java

samples/client/petstore/java/okhttp-gson/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Class | Method | HTTP request | Description
233233
- [ModelReturn](docs/ModelReturn.md)
234234
- [ModelWithOneOfAnyOfProperties](docs/ModelWithOneOfAnyOfProperties.md)
235235
- [Name](docs/Name.md)
236+
- [NestedArrayWithDefaultValues](docs/NestedArrayWithDefaultValues.md)
236237
- [NewPet](docs/NewPet.md)
237238
- [NewPetCategoryInlineAllof](docs/NewPetCategoryInlineAllof.md)
238239
- [NewPetCategoryInlineAllofAllOfCategoryTag](docs/NewPetCategoryInlineAllofAllOfCategoryTag.md)

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,6 +2916,27 @@ components:
29162916
anyof_prop:
29172917
$ref: "#/components/schemas/ArrayAnyOf"
29182918
type: object
2919+
NestedArrayWithDefaultValues:
2920+
properties:
2921+
nestedArray:
2922+
default:
2923+
- - h1
2924+
- Header 1
2925+
- - h2
2926+
- Header 2
2927+
- - h3
2928+
- Header 3
2929+
- - h4
2930+
- Header 4
2931+
items:
2932+
default:
2933+
- value1
2934+
- value2
2935+
items:
2936+
type: string
2937+
type: array
2938+
type: array
2939+
type: object
29192940
_foo_get_default_response:
29202941
example:
29212942
string:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
# NestedArrayWithDefaultValues
4+
5+
6+
## Properties
7+
8+
| Name | Type | Description | Notes |
9+
|------------ | ------------- | ------------- | -------------|
10+
|**nestedArray** | **List<List<String>>** | | [optional] |
11+
12+
13+

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/JSON.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ private static Class getClassByDiscriminator(Map classByDiscriminatorValue, Stri
303303
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelReturn.CustomTypeAdapterFactory());
304304
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.ModelWithOneOfAnyOfProperties.CustomTypeAdapterFactory());
305305
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.Name.CustomTypeAdapterFactory());
306+
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NestedArrayWithDefaultValues.CustomTypeAdapterFactory());
306307
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NewPet.CustomTypeAdapterFactory());
307308
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NewPetCategoryInlineAllof.CustomTypeAdapterFactory());
308309
gsonBuilder.registerTypeAdapterFactory(new org.openapitools.client.model.NewPetCategoryInlineAllofAllOfCategoryTag.CustomTypeAdapterFactory());
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
/*
2+
* OpenAPI Petstore
3+
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
4+
*
5+
* The version of the OpenAPI document: 1.0.0
6+
*
7+
*
8+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9+
* https://openapi-generator.tech
10+
* Do not edit the class manually.
11+
*/
12+
13+
14+
package org.openapitools.client.model;
15+
16+
import java.util.Objects;
17+
import com.google.gson.TypeAdapter;
18+
import com.google.gson.annotations.JsonAdapter;
19+
import com.google.gson.annotations.SerializedName;
20+
import com.google.gson.stream.JsonReader;
21+
import com.google.gson.stream.JsonWriter;
22+
import java.io.IOException;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
import com.google.gson.Gson;
28+
import com.google.gson.GsonBuilder;
29+
import com.google.gson.JsonArray;
30+
import com.google.gson.JsonDeserializationContext;
31+
import com.google.gson.JsonDeserializer;
32+
import com.google.gson.JsonElement;
33+
import com.google.gson.JsonObject;
34+
import com.google.gson.JsonParseException;
35+
import com.google.gson.TypeAdapterFactory;
36+
import com.google.gson.reflect.TypeToken;
37+
import com.google.gson.TypeAdapter;
38+
import com.google.gson.stream.JsonReader;
39+
import com.google.gson.stream.JsonWriter;
40+
import java.io.IOException;
41+
42+
import java.util.HashMap;
43+
import java.util.HashSet;
44+
import java.util.List;
45+
import java.util.Map;
46+
import java.util.Set;
47+
48+
import org.openapitools.client.JSON;
49+
50+
/**
51+
* NestedArrayWithDefaultValues
52+
*/
53+
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.15.0-SNAPSHOT")
54+
public class NestedArrayWithDefaultValues {
55+
public static final String SERIALIZED_NAME_NESTED_ARRAY = "nestedArray";
56+
@SerializedName(SERIALIZED_NAME_NESTED_ARRAY)
57+
@javax.annotation.Nullable
58+
private List<List<String>> nestedArray;
59+
60+
public NestedArrayWithDefaultValues() {
61+
}
62+
63+
public NestedArrayWithDefaultValues nestedArray(@javax.annotation.Nullable List<List<String>> nestedArray) {
64+
this.nestedArray = nestedArray;
65+
return this;
66+
}
67+
68+
public NestedArrayWithDefaultValues addNestedArrayItem(List<String> nestedArrayItem) {
69+
if (this.nestedArray == null) {
70+
this.nestedArray = new ArrayList<>();
71+
}
72+
this.nestedArray.add(nestedArrayItem);
73+
return this;
74+
}
75+
76+
/**
77+
* Get nestedArray
78+
* @return nestedArray
79+
*/
80+
@javax.annotation.Nullable
81+
public List<List<String>> getNestedArray() {
82+
return nestedArray;
83+
}
84+
85+
public void setNestedArray(@javax.annotation.Nullable List<List<String>> nestedArray) {
86+
this.nestedArray = nestedArray;
87+
}
88+
89+
/**
90+
* A container for additional, undeclared properties.
91+
* This is a holder for any undeclared properties as specified with
92+
* the 'additionalProperties' keyword in the OAS document.
93+
*/
94+
private Map<String, Object> additionalProperties;
95+
96+
/**
97+
* Set the additional (undeclared) property with the specified name and value.
98+
* If the property does not already exist, create it otherwise replace it.
99+
*
100+
* @param key name of the property
101+
* @param value value of the property
102+
* @return the NestedArrayWithDefaultValues instance itself
103+
*/
104+
public NestedArrayWithDefaultValues putAdditionalProperty(String key, Object value) {
105+
if (this.additionalProperties == null) {
106+
this.additionalProperties = new HashMap<String, Object>();
107+
}
108+
this.additionalProperties.put(key, value);
109+
return this;
110+
}
111+
112+
/**
113+
* Return the additional (undeclared) property.
114+
*
115+
* @return a map of objects
116+
*/
117+
public Map<String, Object> getAdditionalProperties() {
118+
return additionalProperties;
119+
}
120+
121+
/**
122+
* Return the additional (undeclared) property with the specified name.
123+
*
124+
* @param key name of the property
125+
* @return an object
126+
*/
127+
public Object getAdditionalProperty(String key) {
128+
if (this.additionalProperties == null) {
129+
return null;
130+
}
131+
return this.additionalProperties.get(key);
132+
}
133+
134+
135+
@Override
136+
public boolean equals(Object o) {
137+
if (this == o) {
138+
return true;
139+
}
140+
if (o == null || getClass() != o.getClass()) {
141+
return false;
142+
}
143+
NestedArrayWithDefaultValues nestedArrayWithDefaultValues = (NestedArrayWithDefaultValues) o;
144+
return Objects.equals(this.nestedArray, nestedArrayWithDefaultValues.nestedArray)&&
145+
Objects.equals(this.additionalProperties, nestedArrayWithDefaultValues.additionalProperties);
146+
}
147+
148+
@Override
149+
public int hashCode() {
150+
return Objects.hash(nestedArray, additionalProperties);
151+
}
152+
153+
@Override
154+
public String toString() {
155+
StringBuilder sb = new StringBuilder();
156+
sb.append("class NestedArrayWithDefaultValues {\n");
157+
sb.append(" nestedArray: ").append(toIndentedString(nestedArray)).append("\n");
158+
sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n");
159+
sb.append("}");
160+
return sb.toString();
161+
}
162+
163+
/**
164+
* Convert the given object to string with each line indented by 4 spaces
165+
* (except the first line).
166+
*/
167+
private String toIndentedString(Object o) {
168+
if (o == null) {
169+
return "null";
170+
}
171+
return o.toString().replace("\n", "\n ");
172+
}
173+
174+
175+
public static HashSet<String> openapiFields;
176+
public static HashSet<String> openapiRequiredFields;
177+
178+
static {
179+
// a set of all properties/fields (JSON key names)
180+
openapiFields = new HashSet<String>(Arrays.asList("nestedArray"));
181+
182+
// a set of required properties/fields (JSON key names)
183+
openapiRequiredFields = new HashSet<String>(0);
184+
}
185+
186+
/**
187+
* Validates the JSON Element and throws an exception if issues found
188+
*
189+
* @param jsonElement JSON Element
190+
* @throws IOException if the JSON Element is invalid with respect to NestedArrayWithDefaultValues
191+
*/
192+
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
193+
if (jsonElement == null) {
194+
if (!NestedArrayWithDefaultValues.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null
195+
throw new IllegalArgumentException(String.format("The required field(s) %s in NestedArrayWithDefaultValues is not found in the empty JSON string", NestedArrayWithDefaultValues.openapiRequiredFields.toString()));
196+
}
197+
}
198+
JsonObject jsonObj = jsonElement.getAsJsonObject();
199+
// ensure the optional json data is an array if present
200+
if (jsonObj.get("nestedArray") != null && !jsonObj.get("nestedArray").isJsonNull() && !jsonObj.get("nestedArray").isJsonArray()) {
201+
throw new IllegalArgumentException(String.format("Expected the field `nestedArray` to be an array in the JSON string but got `%s`", jsonObj.get("nestedArray").toString()));
202+
}
203+
}
204+
205+
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
206+
@SuppressWarnings("unchecked")
207+
@Override
208+
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
209+
if (!NestedArrayWithDefaultValues.class.isAssignableFrom(type.getRawType())) {
210+
return null; // this class only serializes 'NestedArrayWithDefaultValues' and its subtypes
211+
}
212+
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
213+
final TypeAdapter<NestedArrayWithDefaultValues> thisAdapter
214+
= gson.getDelegateAdapter(this, TypeToken.get(NestedArrayWithDefaultValues.class));
215+
216+
return (TypeAdapter<T>) new TypeAdapter<NestedArrayWithDefaultValues>() {
217+
@Override
218+
public void write(JsonWriter out, NestedArrayWithDefaultValues value) throws IOException {
219+
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
220+
obj.remove("additionalProperties");
221+
// serialize additional properties
222+
if (value.getAdditionalProperties() != null) {
223+
for (Map.Entry<String, Object> entry : value.getAdditionalProperties().entrySet()) {
224+
if (entry.getValue() instanceof String)
225+
obj.addProperty(entry.getKey(), (String) entry.getValue());
226+
else if (entry.getValue() instanceof Number)
227+
obj.addProperty(entry.getKey(), (Number) entry.getValue());
228+
else if (entry.getValue() instanceof Boolean)
229+
obj.addProperty(entry.getKey(), (Boolean) entry.getValue());
230+
else if (entry.getValue() instanceof Character)
231+
obj.addProperty(entry.getKey(), (Character) entry.getValue());
232+
else {
233+
JsonElement jsonElement = gson.toJsonTree(entry.getValue());
234+
if (jsonElement.isJsonArray()) {
235+
obj.add(entry.getKey(), jsonElement.getAsJsonArray());
236+
} else {
237+
obj.add(entry.getKey(), jsonElement.getAsJsonObject());
238+
}
239+
}
240+
}
241+
}
242+
elementAdapter.write(out, obj);
243+
}
244+
245+
@Override
246+
public NestedArrayWithDefaultValues read(JsonReader in) throws IOException {
247+
JsonElement jsonElement = elementAdapter.read(in);
248+
validateJsonElement(jsonElement);
249+
JsonObject jsonObj = jsonElement.getAsJsonObject();
250+
// store additional fields in the deserialized instance
251+
NestedArrayWithDefaultValues instance = thisAdapter.fromJsonTree(jsonObj);
252+
for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) {
253+
if (!openapiFields.contains(entry.getKey())) {
254+
if (entry.getValue().isJsonPrimitive()) { // primitive type
255+
if (entry.getValue().getAsJsonPrimitive().isString())
256+
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString());
257+
else if (entry.getValue().getAsJsonPrimitive().isNumber())
258+
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber());
259+
else if (entry.getValue().getAsJsonPrimitive().isBoolean())
260+
instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean());
261+
else
262+
throw new IllegalArgumentException(String.format("The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString()));
263+
} else if (entry.getValue().isJsonArray()) {
264+
instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class));
265+
} else { // JSON object
266+
instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class));
267+
}
268+
}
269+
}
270+
return instance;
271+
}
272+
273+
}.nullSafe();
274+
}
275+
}
276+
277+
/**
278+
* Create an instance of NestedArrayWithDefaultValues given an JSON string
279+
*
280+
* @param jsonString JSON string
281+
* @return An instance of NestedArrayWithDefaultValues
282+
* @throws IOException if the JSON string is invalid with respect to NestedArrayWithDefaultValues
283+
*/
284+
public static NestedArrayWithDefaultValues fromJson(String jsonString) throws IOException {
285+
return JSON.getGson().fromJson(jsonString, NestedArrayWithDefaultValues.class);
286+
}
287+
288+
/**
289+
* Convert an instance of NestedArrayWithDefaultValues to an JSON string
290+
*
291+
* @return JSON string
292+
*/
293+
public String toJson() {
294+
return JSON.getGson().toJson(this);
295+
}
296+
}
297+

0 commit comments

Comments
 (0)