[JAVA] fix ClassCastException validating an optional JsonArray which is a JsonNullable (#13448)

This commit is contained in:
ehealthexperts-rk 2022-09-17 16:37:09 +02:00 committed by GitHub
parent 517816d72b
commit 62d29c3be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 133 additions and 110 deletions

View File

@ -469,19 +469,21 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{#vars}}
{{#isArray}}
{{#items.isModel}}
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
{{#isRequired}}
// ensure the json data is an array
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `{{{baseName}}}` to be an array in the JSON string but got `%s`", jsonObj.get("{{{baseName}}}").toString()));
}
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
// validate the required field `{{{baseName}}}` (array)
for (int i = 0; i < jsonArray{{name}}.size(); i++) {
{{{items.dataType}}}.validateJsonObject(jsonArray{{name}}.get(i).getAsJsonObject());
};
{{/isRequired}}
{{^isRequired}}
if (jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) {
JsonArray jsonArray{{name}} = jsonObj.getAsJsonArray("{{{baseName}}}");
if (jsonArray{{name}} != null) {
// ensure the json data is an array
if (!jsonObj.get("{{{baseName}}}").isJsonArray()) {
@ -493,6 +495,7 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{{items.dataType}}}.validateJsonObject(jsonArray{{name}}.get(i).getAsJsonObject());
};
}
}
{{/isRequired}}
{{/items.isModel}}
{{^items.isModel}}

View File

@ -197,6 +197,7 @@ public class FileSchemaTestClass {
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
}
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
if (jsonArrayfiles != null) {
// ensure the json data is an array
@ -210,6 +211,7 @@ public class FileSchemaTestClass {
};
}
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")

View File

@ -390,6 +390,7 @@ public class Pet {
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
}
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
if (jsonArraytags != null) {
// ensure the json data is an array
@ -402,6 +403,7 @@ public class Pet {
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
};
}
}
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
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()));
}

View File

@ -420,6 +420,7 @@ public class Pet {
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
}
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
if (jsonArraytags != null) {
// ensure the json data is an array
@ -432,6 +433,7 @@ public class Pet {
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
};
}
}
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
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()));
}

View File

@ -222,6 +222,7 @@ public class FileSchemaTestClass implements Parcelable {
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
}
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
if (jsonArrayfiles != null) {
// ensure the json data is an array
@ -235,6 +236,7 @@ public class FileSchemaTestClass implements Parcelable {
};
}
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")

View File

@ -423,6 +423,7 @@ public class Pet implements Parcelable {
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
}
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
if (jsonArraytags != null) {
// ensure the json data is an array
@ -435,6 +436,7 @@ public class Pet implements Parcelable {
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
};
}
}
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
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()));
}

View File

@ -263,6 +263,7 @@ public class ArrayOfInlineAllOf {
if ((jsonObj.get("name") != null && !jsonObj.get("name").isJsonNull()) && !jsonObj.get("name").isJsonPrimitive()) {
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()));
}
if (jsonObj.get("array_allof_dog_property") != null && !jsonObj.get("array_allof_dog_property").isJsonNull()) {
JsonArray jsonArrayarrayAllofDogProperty = jsonObj.getAsJsonArray("array_allof_dog_property");
if (jsonArrayarrayAllofDogProperty != null) {
// ensure the json data is an array
@ -276,6 +277,7 @@ public class ArrayOfInlineAllOf {
};
}
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")

View File

@ -280,6 +280,7 @@ public class Drawing {
if (jsonObj.get("nullableShape") != null && !jsonObj.get("nullableShape").isJsonNull()) {
NullableShape.validateJsonObject(jsonObj.getAsJsonObject("nullableShape"));
}
if (jsonObj.get("shapes") != null && !jsonObj.get("shapes").isJsonNull()) {
JsonArray jsonArrayshapes = jsonObj.getAsJsonArray("shapes");
if (jsonArrayshapes != null) {
// ensure the json data is an array
@ -293,6 +294,7 @@ public class Drawing {
};
}
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")

View File

@ -226,6 +226,7 @@ public class FileSchemaTestClass {
if (jsonObj.get("file") != null && !jsonObj.get("file").isJsonNull()) {
ModelFile.validateJsonObject(jsonObj.getAsJsonObject("file"));
}
if (jsonObj.get("files") != null && !jsonObj.get("files").isJsonNull()) {
JsonArray jsonArrayfiles = jsonObj.getAsJsonArray("files");
if (jsonArrayfiles != null) {
// ensure the json data is an array
@ -239,6 +240,7 @@ public class FileSchemaTestClass {
};
}
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {
@SuppressWarnings("unchecked")

View File

@ -417,6 +417,7 @@ public class Pet {
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
}
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
if (jsonArraytags != null) {
// ensure the json data is an array
@ -429,6 +430,7 @@ public class Pet {
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
};
}
}
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
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()));
}

View File

@ -415,6 +415,7 @@ public class PetWithRequiredTags {
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `photoUrls` to be an array in the JSON string but got `%s`", jsonObj.get("photoUrls").toString()));
}
if (jsonObj.get("tags") != null && !jsonObj.get("tags").isJsonNull()) {
JsonArray jsonArraytags = jsonObj.getAsJsonArray("tags");
if (jsonArraytags != null) {
// ensure the json data is an array
@ -427,6 +428,7 @@ public class PetWithRequiredTags {
Tag.validateJsonObject(jsonArraytags.get(i).getAsJsonObject());
};
}
}
if ((jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) && !jsonObj.get("status").isJsonPrimitive()) {
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()));
}