Skip to content

Commit a745f05

Browse files
authored
skip custom adapter for parent object (#10998)
1 parent 40609ad commit a745f05

File tree

5 files changed

+4
-127
lines changed

5 files changed

+4
-127
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson-nextgen/JSON.mustache

+2-14
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,9 @@ public class JSON {
132132
{{#models}}
133133
{{#model}}
134134
{{^isEnum}}
135-
{{#oneOf}}
136-
{{#-first}}
135+
{{^hasChildren}}
137136
.registerTypeAdapterFactory(new {{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
138-
{{/-first}}
139-
{{/oneOf}}
140-
{{^oneOf}}
141-
{{#anyOf}}
142-
{{#-first}}
143-
.registerTypeAdapterFactory(new {{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
144-
{{/-first}}
145-
{{/anyOf}}
146-
{{^anyOf}}
147-
.registerTypeAdapterFactory(new {{modelPackage}}.{{{classname}}}.CustomTypeAdapterFactory())
148-
{{/anyOf}}
149-
{{/oneOf}}
137+
{{/hasChildren}}
150138
{{/isEnum}}
151139
{{/model}}
152140
{{/models}}

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson-nextgen/pojo.mustache

+2
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
379379
}
380380
};
381381
{{/parcelableModel}}
382+
{{^hasChildren}}
382383
public static HashSet<String> openapiFields;
383384
public static HashSet<String> openapiRequiredFields;
384385
@@ -442,4 +443,5 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
442443
}.nullSafe();
443444
}
444445
}
446+
{{/hasChildren}}
445447
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ public JSON() {
221221
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
222222
.registerTypeAdapter(byte[].class, byteArrayAdapter)
223223
.registerTypeAdapterFactory(new org.openapitools.client.model.AdditionalPropertiesClass.CustomTypeAdapterFactory())
224-
.registerTypeAdapterFactory(new org.openapitools.client.model.Animal.CustomTypeAdapterFactory())
225224
.registerTypeAdapterFactory(new org.openapitools.client.model.Apple.CustomTypeAdapterFactory())
226225
.registerTypeAdapterFactory(new org.openapitools.client.model.AppleReq.CustomTypeAdapterFactory())
227226
.registerTypeAdapterFactory(new org.openapitools.client.model.ArrayOfArrayOfNumberOnly.CustomTypeAdapterFactory())
@@ -251,7 +250,6 @@ public JSON() {
251250
.registerTypeAdapterFactory(new org.openapitools.client.model.Fruit.CustomTypeAdapterFactory())
252251
.registerTypeAdapterFactory(new org.openapitools.client.model.FruitReq.CustomTypeAdapterFactory())
253252
.registerTypeAdapterFactory(new org.openapitools.client.model.GmFruit.CustomTypeAdapterFactory())
254-
.registerTypeAdapterFactory(new org.openapitools.client.model.GrandparentAnimal.CustomTypeAdapterFactory())
255253
.registerTypeAdapterFactory(new org.openapitools.client.model.HasOnlyReadOnly.CustomTypeAdapterFactory())
256254
.registerTypeAdapterFactory(new org.openapitools.client.model.HealthCheckResult.CustomTypeAdapterFactory())
257255
.registerTypeAdapterFactory(new org.openapitools.client.model.InlineResponseDefault.CustomTypeAdapterFactory())

samples/client/petstore/java/okhttp-gson-nextgen/src/main/java/org/openapitools/client/model/Animal.java

-56
Original file line numberDiff line numberDiff line change
@@ -144,60 +144,4 @@ private String toIndentedString(Object o) {
144144
return o.toString().replace("\n", "\n ");
145145
}
146146

147-
public static HashSet<String> openapiFields;
148-
public static HashSet<String> openapiRequiredFields;
149-
150-
static {
151-
// a set of all properties/fields (JSON key names)
152-
openapiFields = new HashSet<String>();
153-
openapiFields.add("className");
154-
openapiFields.add("color");
155-
156-
// a set of required properties/fields (JSON key names)
157-
openapiRequiredFields = new HashSet<String>();
158-
openapiRequiredFields.add("className");
159-
}
160-
161-
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
162-
@SuppressWarnings("unchecked")
163-
@Override
164-
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
165-
if (!Animal.class.isAssignableFrom(type.getRawType())) {
166-
return null; // this class only serializes 'Animal' and its subtypes
167-
}
168-
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
169-
final TypeAdapter<Animal> thisAdapter
170-
= gson.getDelegateAdapter(this, TypeToken.get(Animal.class));
171-
172-
return (TypeAdapter<T>) new TypeAdapter<Animal>() {
173-
@Override
174-
public void write(JsonWriter out, Animal value) throws IOException {
175-
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
176-
elementAdapter.write(out, obj);
177-
}
178-
179-
@Override
180-
public Animal read(JsonReader in) throws IOException {
181-
JsonObject obj = elementAdapter.read(in).getAsJsonObject();
182-
Set<Entry<String, JsonElement>> entries = obj.entrySet();//will return members of your object
183-
// check to see if the JSON string contains additional fields
184-
for (Entry<String, JsonElement> entry: entries) {
185-
if (!Animal.openapiFields.contains(entry.getKey())) {
186-
throw new IllegalArgumentException("The field `" + entry.getKey() + "` in the JSON string is not defined in the `Animal` properties");
187-
}
188-
}
189-
190-
// check to make sure all required properties/fields are present in the JSON string
191-
for (String requiredField : Animal.openapiRequiredFields) {
192-
if (obj.get(requiredField) == null) {
193-
throw new IllegalArgumentException("The required field `" + requiredField + "` is not found in the JSON string");
194-
}
195-
}
196-
197-
return thisAdapter.fromJsonTree(obj);
198-
}
199-
200-
}.nullSafe();
201-
}
202-
}
203147
}

samples/client/petstore/java/okhttp-gson-nextgen/src/main/java/org/openapitools/client/model/GrandparentAnimal.java

-55
Original file line numberDiff line numberDiff line change
@@ -114,59 +114,4 @@ private String toIndentedString(Object o) {
114114
return o.toString().replace("\n", "\n ");
115115
}
116116

117-
public static HashSet<String> openapiFields;
118-
public static HashSet<String> openapiRequiredFields;
119-
120-
static {
121-
// a set of all properties/fields (JSON key names)
122-
openapiFields = new HashSet<String>();
123-
openapiFields.add("pet_type");
124-
125-
// a set of required properties/fields (JSON key names)
126-
openapiRequiredFields = new HashSet<String>();
127-
openapiRequiredFields.add("pet_type");
128-
}
129-
130-
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
131-
@SuppressWarnings("unchecked")
132-
@Override
133-
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
134-
if (!GrandparentAnimal.class.isAssignableFrom(type.getRawType())) {
135-
return null; // this class only serializes 'GrandparentAnimal' and its subtypes
136-
}
137-
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
138-
final TypeAdapter<GrandparentAnimal> thisAdapter
139-
= gson.getDelegateAdapter(this, TypeToken.get(GrandparentAnimal.class));
140-
141-
return (TypeAdapter<T>) new TypeAdapter<GrandparentAnimal>() {
142-
@Override
143-
public void write(JsonWriter out, GrandparentAnimal value) throws IOException {
144-
JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject();
145-
elementAdapter.write(out, obj);
146-
}
147-
148-
@Override
149-
public GrandparentAnimal read(JsonReader in) throws IOException {
150-
JsonObject obj = elementAdapter.read(in).getAsJsonObject();
151-
Set<Entry<String, JsonElement>> entries = obj.entrySet();//will return members of your object
152-
// check to see if the JSON string contains additional fields
153-
for (Entry<String, JsonElement> entry: entries) {
154-
if (!GrandparentAnimal.openapiFields.contains(entry.getKey())) {
155-
throw new IllegalArgumentException("The field `" + entry.getKey() + "` in the JSON string is not defined in the `GrandparentAnimal` properties");
156-
}
157-
}
158-
159-
// check to make sure all required properties/fields are present in the JSON string
160-
for (String requiredField : GrandparentAnimal.openapiRequiredFields) {
161-
if (obj.get(requiredField) == null) {
162-
throw new IllegalArgumentException("The required field `" + requiredField + "` is not found in the JSON string");
163-
}
164-
}
165-
166-
return thisAdapter.fromJsonTree(obj);
167-
}
168-
169-
}.nullSafe();
170-
}
171-
}
172117
}

0 commit comments

Comments
 (0)