Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 62d29c3

Browse files
[JAVA] fix ClassCastException validating an optional JsonArray which is a JsonNullable (#13448)
1 parent 517816d commit 62d29c3

File tree

11 files changed

+133
-110
lines changed

11 files changed

+133
-110
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -469,29 +469,32 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
469469
{{#vars}}
470470
{{#isArray}}
471471
{{#items.isModel}}
472-
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
473472
{{#isRequired}}
474473
// ensure the json data is an array
475474
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
476475
throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
477476
}
478477
478+
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
479479
// validate the required field `{{{baseName}}}` (array)
480480
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
481481
{{{items.dataType}}}.validateJsonObject(jsonArray{{name}}.get(i).getAsJsonObject());
482482
};
483483
{{/isRequired}}
484484
{{^isRequired}}
485-
if (jsonArray{{name}} != null) {
486-
// ensure the json data is an array
487-
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
488-
throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
485+
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
486+
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
487+
if (jsonArray{{name}} != null) {
488+
// ensure the json data is an array
489+
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
490+
throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
491+
}
492+
493+
// validate the optional field `{{{baseName}}}` (array)
494+
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
495+
{{{items.dataType}}}.validateJsonObject(jsonArray{{name}}.get(i).getAsJsonObject());
496+
};
489497
}
490-
491-
// validate the optional field `{{{baseName}}}` (array)
492-
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
493-
{{{items.dataType}}}.validateJsonObject(jsonArray{{name}}.get(i).getAsJsonObject());
494-
};
495498
}
496499
{{/isRequired}}
497500
{{/items.isModel}}

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/model/FileSchemaTestClass.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
197197
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
198198
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
199199
}
200-
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
201-
if (jsonArrayfiles != null) {
202-
// ensure the json data is an array
203-
if (!jsonObj.get("files").isJsonArray()) {
204-
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
200+
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
201+
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
202+
if (jsonArrayfiles != null) {
203+
// ensure the json data is an array
204+
if (!jsonObj.get("files").isJsonArray()) {
205+
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
206+
}
207+
208+
// validate the optional field `files` (array)
209+
for (int i = 0; i < jsonArrayfiles.size(); i++) {
210+
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
211+
};
205212
}
206-
207-
// validate the optional field `files` (array)
208-
for (int i = 0; i < jsonArrayfiles.size(); i++) {
209-
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
210-
};
211213
}
212214
}
213215

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/model/Pet.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
390390
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
391391
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
392392
}
393-
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
394-
if (jsonArraytags != null) {
395-
// ensure the json data is an array
396-
if (!jsonObj.get("tags").isJsonArray()) {
397-
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
393+
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
394+
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
395+
if (jsonArraytags != null) {
396+
// ensure the json data is an array
397+
if (!jsonObj.get("tags").isJsonArray()) {
398+
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
399+
}
400+
401+
// validate the optional field `tags` (array)
402+
for (int i = 0; i < jsonArraytags.size(); i++) {
403+
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
404+
};
398405
}
399-
400-
// validate the optional field `tags` (array)
401-
for (int i = 0; i < jsonArraytags.size(); i++) {
402-
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
403-
};
404406
}
405407
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
406408
throw new IllegalArgumentException(String.format("Expected the field `status` to be a primitive type in the JSON string but got `%s`", jsonObj.get("status").toString()));

samples/client/petstore/java/okhttp-gson-group-parameter/src/main/java/org/openapitools/client/model/Pet.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,17 +420,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
420420
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
421421
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
422422
}
423-
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
424-
if (jsonArraytags != null) {
425-
// ensure the json data is an array
426-
if (!jsonObj.get("tags").isJsonArray()) {
427-
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
423+
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
424+
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
425+
if (jsonArraytags != null) {
426+
// ensure the json data is an array
427+
if (!jsonObj.get("tags").isJsonArray()) {
428+
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
429+
}
430+
431+
// validate the optional field `tags` (array)
432+
for (int i = 0; i < jsonArraytags.size(); i++) {
433+
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
434+
};
428435
}
429-
430-
// validate the optional field `tags` (array)
431-
for (int i = 0; i < jsonArraytags.size(); i++) {
432-
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
433-
};
434436
}
435437
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
436438
throw new IllegalArgumentException(String.format("Expected the field `status` to be a primitive type in the JSON string but got `%s`", jsonObj.get("status").toString()));

samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/model/FileSchemaTestClass.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
222222
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
223223
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
224224
}
225-
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
226-
if (jsonArrayfiles != null) {
227-
// ensure the json data is an array
228-
if (!jsonObj.get("files").isJsonArray()) {
229-
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
225+
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
226+
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
227+
if (jsonArrayfiles != null) {
228+
// ensure the json data is an array
229+
if (!jsonObj.get("files").isJsonArray()) {
230+
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
231+
}
232+
233+
// validate the optional field `files` (array)
234+
for (int i = 0; i < jsonArrayfiles.size(); i++) {
235+
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
236+
};
230237
}
231-
232-
// validate the optional field `files` (array)
233-
for (int i = 0; i < jsonArrayfiles.size(); i++) {
234-
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
235-
};
236238
}
237239
}
238240

samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/model/Pet.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,17 +423,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
423423
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
424424
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
425425
}
426-
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
427-
if (jsonArraytags != null) {
428-
// ensure the json data is an array
429-
if (!jsonObj.get("tags").isJsonArray()) {
430-
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
426+
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
427+
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
428+
if (jsonArraytags != null) {
429+
// ensure the json data is an array
430+
if (!jsonObj.get("tags").isJsonArray()) {
431+
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
432+
}
433+
434+
// validate the optional field `tags` (array)
435+
for (int i = 0; i < jsonArraytags.size(); i++) {
436+
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
437+
};
431438
}
432-
433-
// validate the optional field `tags` (array)
434-
for (int i = 0; i < jsonArraytags.size(); i++) {
435-
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
436-
};
437439
}
438440
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
439441
throw new IllegalArgumentException(String.format("Expected the field `status` to be a primitive type in the JSON string but got `%s`", jsonObj.get("status").toString()));

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
263263
if ((jsonObj.get("name") != null && !jsonObj.get("name").isJsonNull()) && !jsonObj.get("name").isJsonPrimitive()) {
264264
throw new IllegalArgumentException(String.format("Expected the field `name` to be a primitive type in the JSON string but got `%s`", jsonObj.get("name").toString()));
265265
}
266-
JsonArray jsonArrayarrayAllofDogProperty = jsonObj.getAsJsonArray("array_allof_dog_property");
267-
if (jsonArrayarrayAllofDogProperty != null) {
268-
// ensure the json data is an array
269-
if (!jsonObj.get("array_allof_dog_property").isJsonArray()) {
270-
throw new IllegalArgumentException(String.format("Expected the field `array_allof_dog_property` to be an array in the JSON string but got `%s`", jsonObj.get("array_allof_dog_property").toString()));
266+
if (jsonObj.get("array_allof_dog_property") != null && !jsonObj.get("array_allof_dog_property").isJsonNull()) {
267+
JsonArray jsonArrayarrayAllofDogProperty = jsonObj.getAsJsonArray("array_allof_dog_property");
268+
if (jsonArrayarrayAllofDogProperty != null) {
269+
// ensure the json data is an array
270+
if (!jsonObj.get("array_allof_dog_property").isJsonArray()) {
271+
throw new IllegalArgumentException(String.format("Expected the field `array_allof_dog_property` to be an array in the JSON string but got `%s`", jsonObj.get("array_allof_dog_property").toString()));
272+
}
273+
274+
// validate the optional field `array_allof_dog_property` (array)
275+
for (int i = 0; i < jsonArrayarrayAllofDogProperty.size(); i++) {
276+
ArrayOfInlineAllOfArrayAllofDogPropertyInner.validateJsonObject(jsonArrayarrayAllofDogProperty.get(i).getAsJsonObject());
277+
};
271278
}
272-
273-
// validate the optional field `array_allof_dog_property` (array)
274-
for (int i = 0; i < jsonArrayarrayAllofDogProperty.size(); i++) {
275-
ArrayOfInlineAllOfArrayAllofDogPropertyInner.validateJsonObject(jsonArrayarrayAllofDogProperty.get(i).getAsJsonObject());
276-
};
277279
}
278280
}
279281

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
280280
if (jsonObj.get("nullableShape") != null && !jsonObj.get("nullableShape").isJsonNull()) {
281281
NullableShape.validateJsonObject(jsonObj.getAsJsonObject("nullableShape"));
282282
}
283-
JsonArray jsonArrayshapes = jsonObj.getAsJsonArray("shapes");
284-
if (jsonArrayshapes != null) {
285-
// ensure the json data is an array
286-
if (!jsonObj.get("shapes").isJsonArray()) {
287-
throw new IllegalArgumentException(String.format("Expected the field `shapes` to be an array in the JSON string but got `%s`", jsonObj.get("shapes").toString()));
283+
if (jsonObj.get("shapes") != null && !jsonObj.get("shapes").isJsonNull()) {
284+
JsonArray jsonArrayshapes = jsonObj.getAsJsonArray("shapes");
285+
if (jsonArrayshapes != null) {
286+
// ensure the json data is an array
287+
if (!jsonObj.get("shapes").isJsonArray()) {
288+
throw new IllegalArgumentException(String.format("Expected the field `shapes` to be an array in the JSON string but got `%s`", jsonObj.get("shapes").toString()));
289+
}
290+
291+
// validate the optional field `shapes` (array)
292+
for (int i = 0; i < jsonArrayshapes.size(); i++) {
293+
Shape.validateJsonObject(jsonArrayshapes.get(i).getAsJsonObject());
294+
};
288295
}
289-
290-
// validate the optional field `shapes` (array)
291-
for (int i = 0; i < jsonArrayshapes.size(); i++) {
292-
Shape.validateJsonObject(jsonArrayshapes.get(i).getAsJsonObject());
293-
};
294296
}
295297
}
296298

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
226226
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
227227
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
228228
}
229-
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
230-
if (jsonArrayfiles != null) {
231-
// ensure the json data is an array
232-
if (!jsonObj.get("files").isJsonArray()) {
233-
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
229+
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
230+
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
231+
if (jsonArrayfiles != null) {
232+
// ensure the json data is an array
233+
if (!jsonObj.get("files").isJsonArray()) {
234+
throw new IllegalArgumentException(String.format("Expected the field `files` to be an array in the JSON string but got `%s`", jsonObj.get("files").toString()));
235+
}
236+
237+
// validate the optional field `files` (array)
238+
for (int i = 0; i < jsonArrayfiles.size(); i++) {
239+
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
240+
};
234241
}
235-
236-
// validate the optional field `files` (array)
237-
for (int i = 0; i < jsonArrayfiles.size(); i++) {
238-
ModelFile.validateJsonObject(jsonArrayfiles.get(i).getAsJsonObject());
239-
};
240242
}
241243
}
242244

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,19 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
417417
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
418418
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
419419
}
420-
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
421-
if (jsonArraytags != null) {
422-
// ensure the json data is an array
423-
if (!jsonObj.get("tags").isJsonArray()) {
424-
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
420+
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
421+
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
422+
if (jsonArraytags != null) {
423+
// ensure the json data is an array
424+
if (!jsonObj.get("tags").isJsonArray()) {
425+
throw new IllegalArgumentException(String.format("Expected the field `tags` to be an array in the JSON string but got `%s`", jsonObj.get("tags").toString()));
426+
}
427+
428+
// validate the optional field `tags` (array)
429+
for (int i = 0; i < jsonArraytags.size(); i++) {
430+
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
431+
};
425432
}
426-
427-
// validate the optional field `tags` (array)
428-
for (int i = 0; i < jsonArraytags.size(); i++) {
429-
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
430-
};
431433
}
432434
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
433435
throw new IllegalArgumentException(String.format("Expected the field `status` to be a primitive type in the JSON string but got `%s`", jsonObj.get("status").toString()));

0 commit comments

Comments
 (0)