Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][java] required vs optional JSON array validation (#13774) #13777

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,20 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
{{/required}}
{{/items.isModel}}
{{^items.isModel}}
// ensure the json data is an array
if ({{#required}}(jsonObj.get("{{{baseName}}}") != null && !jsonObj.get("{{{baseName}}}").isJsonNull()) && {{/required}}!jsonObj.get("{{{baseName}}}").isJsonArray()) {
{{^required}}
// ensure the optional json data is an array if present
if (jsonObj.get("{{{baseName}}}") != null && !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()));
}
{{/required}}
{{#required}}
// ensure the required json array is present
if (jsonObj.get("{{{baseName}}}") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else 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()));
}
{{/required}}
{{/items.isModel}}
{{/isArray}}
{{^isContainer}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayOfArrayOfNumberOnly` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("ArrayArrayNumber").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("ArrayArrayNumber") != null && !jsonObj.get("ArrayArrayNumber").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `ArrayArrayNumber` to be an array in the JSON string but got `%s`", jsonObj.get("ArrayArrayNumber").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayOfNumberOnly` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("ArrayNumber").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("ArrayNumber") != null && !jsonObj.get("ArrayNumber").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `ArrayNumber` to be an array in the JSON string but got `%s`", jsonObj.get("ArrayNumber").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,16 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayTest` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("array_of_string").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_of_string") != null && !jsonObj.get("array_of_string").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_of_string` to be an array in the JSON string but got `%s`", jsonObj.get("array_of_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_array_of_integer").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_array_of_integer") != null && !jsonObj.get("array_array_of_integer").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_array_of_integer` to be an array in the JSON string but got `%s`", jsonObj.get("array_array_of_integer").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_array_of_model").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_array_of_model") != null && !jsonObj.get("array_array_of_model").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_array_of_model` to be an array in the JSON string but got `%s`", jsonObj.get("array_array_of_model").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if ((jsonObj.get("just_symbol") != null && !jsonObj.get("just_symbol").isJsonNull()) && !jsonObj.get("just_symbol").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `just_symbol` to be a primitive type in the JSON string but got `%s`", jsonObj.get("just_symbol").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_enum").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_enum") != null && !jsonObj.get("array_enum").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_enum` to be an array in the JSON string but got `%s`", jsonObj.get("array_enum").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if (!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()));
}
// ensure the json data is an array
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
// ensure the required json array is present
if (jsonObj.get("photoUrls") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if (!jsonObj.get("string_item").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `string_item` to be a primitive type in the JSON string but got `%s`", jsonObj.get("string_item").toString()));
}
// ensure the json data is an array
if ((jsonObj.get("array_item") != null && !jsonObj.get("array_item").isJsonNull()) && !jsonObj.get("array_item").isJsonArray()) {
// ensure the required json array is present
if (jsonObj.get("array_item") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!jsonObj.get("array_item").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_item` to be an array in the JSON string but got `%s`", jsonObj.get("array_item").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,10 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if (!jsonObj.get("string_item").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `string_item` to be a primitive type in the JSON string but got `%s`", jsonObj.get("string_item").toString()));
}
// ensure the json data is an array
if ((jsonObj.get("array_item") != null && !jsonObj.get("array_item").isJsonNull()) && !jsonObj.get("array_item").isJsonArray()) {
// ensure the required json array is present
if (jsonObj.get("array_item") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!jsonObj.get("array_item").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_item` to be an array in the JSON string but got `%s`", jsonObj.get("array_item").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,52 +1069,52 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if ((jsonObj.get("attribute_string") != null && !jsonObj.get("attribute_string").isJsonNull()) && !jsonObj.get("attribute_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `attribute_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("attribute_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("wrapped_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("wrapped_array") != null && !jsonObj.get("wrapped_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `wrapped_array` to be an array in the JSON string but got `%s`", jsonObj.get("wrapped_array").toString()));
}
if ((jsonObj.get("name_string") != null && !jsonObj.get("name_string").isJsonNull()) && !jsonObj.get("name_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `name_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("name_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("name_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("name_array") != null && !jsonObj.get("name_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `name_array` to be an array in the JSON string but got `%s`", jsonObj.get("name_array").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("name_wrapped_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("name_wrapped_array") != null && !jsonObj.get("name_wrapped_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `name_wrapped_array` to be an array in the JSON string but got `%s`", jsonObj.get("name_wrapped_array").toString()));
}
if ((jsonObj.get("prefix_string") != null && !jsonObj.get("prefix_string").isJsonNull()) && !jsonObj.get("prefix_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("prefix_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("prefix_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("prefix_array") != null && !jsonObj.get("prefix_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_array` to be an array in the JSON string but got `%s`", jsonObj.get("prefix_array").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("prefix_wrapped_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("prefix_wrapped_array") != null && !jsonObj.get("prefix_wrapped_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_wrapped_array` to be an array in the JSON string but got `%s`", jsonObj.get("prefix_wrapped_array").toString()));
}
if ((jsonObj.get("namespace_string") != null && !jsonObj.get("namespace_string").isJsonNull()) && !jsonObj.get("namespace_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `namespace_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("namespace_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("namespace_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("namespace_array") != null && !jsonObj.get("namespace_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `namespace_array` to be an array in the JSON string but got `%s`", jsonObj.get("namespace_array").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("namespace_wrapped_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("namespace_wrapped_array") != null && !jsonObj.get("namespace_wrapped_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `namespace_wrapped_array` to be an array in the JSON string but got `%s`", jsonObj.get("namespace_wrapped_array").toString()));
}
if ((jsonObj.get("prefix_ns_string") != null && !jsonObj.get("prefix_ns_string").isJsonNull()) && !jsonObj.get("prefix_ns_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_ns_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("prefix_ns_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("prefix_ns_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("prefix_ns_array") != null && !jsonObj.get("prefix_ns_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_ns_array` to be an array in the JSON string but got `%s`", jsonObj.get("prefix_ns_array").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("prefix_ns_wrapped_array").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("prefix_ns_wrapped_array") != null && !jsonObj.get("prefix_ns_wrapped_array").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `prefix_ns_wrapped_array` to be an array in the JSON string but got `%s`", jsonObj.get("prefix_ns_wrapped_array").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,10 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if (!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()));
}
// ensure the json data is an array
if ((jsonObj.get("photoUrls") != null && !jsonObj.get("photoUrls").isJsonNull()) && !jsonObj.get("photoUrls").isJsonArray()) {
// ensure the required json array is present
if (jsonObj.get("photoUrls") == null) {
throw new IllegalArgumentException("Expected the field `linkedContent` to be an array in the JSON string but got `null`");
} else if (!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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayOfArrayOfNumberOnly` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("ArrayArrayNumber").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("ArrayArrayNumber") != null && !jsonObj.get("ArrayArrayNumber").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `ArrayArrayNumber` to be an array in the JSON string but got `%s`", jsonObj.get("ArrayArrayNumber").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayOfNumberOnly` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("ArrayNumber").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("ArrayNumber") != null && !jsonObj.get("ArrayNumber").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `ArrayNumber` to be an array in the JSON string but got `%s`", jsonObj.get("ArrayNumber").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,16 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
throw new IllegalArgumentException(String.format("The field `%s` in the JSON string is not defined in the `ArrayTest` properties. JSON: %s", entry.getKey(), jsonObj.toString()));
}
}
// ensure the json data is an array
if (!jsonObj.get("array_of_string").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_of_string") != null && !jsonObj.get("array_of_string").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_of_string` to be an array in the JSON string but got `%s`", jsonObj.get("array_of_string").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_array_of_integer").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_array_of_integer") != null && !jsonObj.get("array_array_of_integer").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_array_of_integer` to be an array in the JSON string but got `%s`", jsonObj.get("array_array_of_integer").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_array_of_model").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_array_of_model") != null && !jsonObj.get("array_array_of_model").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_array_of_model` to be an array in the JSON string but got `%s`", jsonObj.get("array_array_of_model").toString()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ public static void validateJsonObject(JsonObject jsonObj) throws IOException {
if ((jsonObj.get("just_symbol") != null && !jsonObj.get("just_symbol").isJsonNull()) && !jsonObj.get("just_symbol").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `just_symbol` to be a primitive type in the JSON string but got `%s`", jsonObj.get("just_symbol").toString()));
}
// ensure the json data is an array
if (!jsonObj.get("array_enum").isJsonArray()) {
// ensure the optional json data is an array if present
if (jsonObj.get("array_enum") != null && !jsonObj.get("array_enum").isJsonArray()) {
throw new IllegalArgumentException(String.format("Expected the field `array_enum` to be an array in the JSON string but got `%s`", jsonObj.get("array_enum").toString()));
}
}
Expand Down
Loading