Skip to content

Commit 2838b95

Browse files
authored
add logic in normalizer to auto fix self-reference schemas (#19849)
1 parent d1ca82c commit 2838b95

File tree

9 files changed

+978
-0
lines changed

9 files changed

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

0 commit comments

Comments
 (0)