[Java][okhttp-gson] validateJsonElement does not validate enum values (#16865)

* validate enum properties in validateJsonElement

* regenerate samples

* add test for enum validation in okhttp-gson models
This commit is contained in:
Charles Treatman
2023-11-08 00:14:48 -06:00
committed by GitHub
parent 291ce353ce
commit 166ebc50b0
54 changed files with 761 additions and 1 deletions

View File

@@ -115,6 +115,11 @@ public class Order {
return StatusEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
StatusEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_STATUS = "status";
@@ -382,6 +387,10 @@ public class Order {
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()));
}
// validate the optional field `status`
if (jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) {
StatusEnum.validateJsonElement(jsonObj.get("status"));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {

View File

@@ -122,6 +122,11 @@ public class Pet {
return StatusEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
StatusEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_STATUS = "status";
@@ -442,6 +447,10 @@ public class Pet {
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()));
}
// validate the optional field `status`
if (jsonObj.get("status") != null && !jsonObj.get("status").isJsonNull()) {
StatusEnum.validateJsonElement(jsonObj.get("status"));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {