required vs optional JSON array validation (#13774) (#13777)

This commit is contained in:
Andrew Hatch
2022-10-22 14:06:00 +01:00
committed by GitHub
parent 9e5c919560
commit 232f354826
26 changed files with 128 additions and 100 deletions

View File

@@ -415,8 +415,10 @@ public class Pet {
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()) {