Skip to content

Commit 893e220

Browse files
authored
update normalizer to clear types when $ref is set (#22945)
1 parent b39aad0 commit 893e220

File tree

9 files changed

+373
-0
lines changed

9 files changed

+373
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,13 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
803803
* @param schema Schema
804804
*/
805805
protected void normalizeReferenceSchema(Schema schema) {
806+
if (schema.getType() != null || schema.getTypes() != null && !schema.getTypes().isEmpty()) {
807+
// clears type(s) given that $ref is set
808+
schema.setType(null);
809+
schema.setTypes(null);
810+
LOGGER.warn("Type(s) cleared (set to null) given $ref is set to {}.", schema.get$ref());
811+
}
812+
806813
if (schema.getTitle() != null || schema.getDescription() != null
807814
|| schema.getNullable() != null || schema.getDefault() != null || schema.getDeprecated() != null
808815
|| schema.getMaximum() != null || schema.getMinimum() != null

modules/openapi-generator/src/test/resources/3_1/java/petstore.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,3 +1117,11 @@ components:
11171117
properties:
11181118
dummy:
11191119
type: string
1120+
PetWithTypesObjectNullAndRef:
1121+
type: object
1122+
properties:
1123+
first_property:
1124+
type:
1125+
- "object"
1126+
- "null"
1127+
$ref: '#/components/schemas/Pet'

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ docs/OneOfStringOrInt.md
2525
docs/Order.md
2626
docs/Pet.md
2727
docs/PetApi.md
28+
docs/PetWithTypesObjectNullAndRef.md
2829
docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md
2930
docs/RefToRefParameterAnyofRefToAnyofParameter.md
3031
docs/SelfReferenceAdditionalProperties.md
@@ -89,6 +90,7 @@ src/main/java/org/openapitools/client/model/ModelApiResponse.java
8990
src/main/java/org/openapitools/client/model/OneOfStringOrInt.java
9091
src/main/java/org/openapitools/client/model/Order.java
9192
src/main/java/org/openapitools/client/model/Pet.java
93+
src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java
9294
src/main/java/org/openapitools/client/model/RefRefToPathLevelParameterOneofRefToOneofParameter.java
9395
src/main/java/org/openapitools/client/model/RefToRefParameterAnyofRefToAnyofParameter.java
9496
src/main/java/org/openapitools/client/model/SelfReferenceAdditionalProperties.java

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Class | Method | HTTP request | Description
166166
- [OneOfStringOrInt](docs/OneOfStringOrInt.md)
167167
- [Order](docs/Order.md)
168168
- [Pet](docs/Pet.md)
169+
- [PetWithTypesObjectNullAndRef](docs/PetWithTypesObjectNullAndRef.md)
169170
- [RefRefToPathLevelParameterOneofRefToOneofParameter](docs/RefRefToPathLevelParameterOneofRefToOneofParameter.md)
170171
- [RefToRefParameterAnyofRefToAnyofParameter](docs/RefToRefParameterAnyofRefToAnyofParameter.md)
171172
- [SelfReferenceAdditionalProperties](docs/SelfReferenceAdditionalProperties.md)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,10 @@ components:
11821182
properties:
11831183
dummy:
11841184
type: string
1185+
PetWithTypesObjectNullAndRef:
1186+
properties:
1187+
first_property:
1188+
$ref: "#/components/schemas/Pet"
11851189
updatePetWithForm_request:
11861190
properties:
11871191
name:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
# PetWithTypesObjectNullAndRef
4+
5+
6+
## Properties
7+
8+
| Name | Type | Description | Notes |
9+
|------------ | ------------- | ------------- | -------------|
10+
|**firstProperty** | [**Pet**](Pet.md) | | [optional] |
11+
12+
13+

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

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

0 commit comments

Comments
 (0)