[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

@@ -101,6 +101,11 @@ public class BigCat extends Cat {
return KindEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
KindEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_KIND = "kind";

View File

@@ -98,6 +98,11 @@ public class EnumArrays {
return JustSymbolEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
JustSymbolEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_JUST_SYMBOL = "just_symbol";
@@ -149,6 +154,11 @@ public class EnumArrays {
return ArrayEnumEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
ArrayEnumEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ARRAY_ENUM = "array_enum";
@@ -286,6 +296,10 @@ public class EnumArrays {
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()));
}
// validate the optional field `just_symbol`
if (jsonObj.get("just_symbol") != null && !jsonObj.get("just_symbol").isJsonNull()) {
JustSymbolEnum.validateJsonElement(jsonObj.get("just_symbol"));
}
// ensure the optional json data is an array if present
if (jsonObj.get("array_enum") != null && !jsonObj.get("array_enum").isJsonNull() && !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()));

View File

@@ -18,6 +18,7 @@ import com.google.gson.annotations.SerializedName;
import java.io.IOException;
import com.google.gson.TypeAdapter;
import com.google.gson.JsonElement;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
@@ -70,5 +71,10 @@ public enum EnumClass {
return EnumClass.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
EnumClass.fromValue(value);
}
}

View File

@@ -99,6 +99,11 @@ public class EnumTest {
return EnumStringEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
EnumStringEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ENUM_STRING = "enum_string";
@@ -152,6 +157,11 @@ public class EnumTest {
return EnumStringRequiredEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
EnumStringRequiredEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ENUM_STRING_REQUIRED = "enum_string_required";
@@ -203,6 +213,11 @@ public class EnumTest {
return EnumIntegerEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
Integer value = jsonElement.getAsInt();
EnumIntegerEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ENUM_INTEGER = "enum_integer";
@@ -254,6 +269,11 @@ public class EnumTest {
return EnumNumberEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
Double value = jsonElement.getAsDouble();
EnumNumberEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ENUM_NUMBER = "enum_number";
@@ -467,9 +487,27 @@ public class EnumTest {
if ((jsonObj.get("enum_string") != null && !jsonObj.get("enum_string").isJsonNull()) && !jsonObj.get("enum_string").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `enum_string` to be a primitive type in the JSON string but got `%s`", jsonObj.get("enum_string").toString()));
}
// validate the optional field `enum_string`
if (jsonObj.get("enum_string") != null && !jsonObj.get("enum_string").isJsonNull()) {
EnumStringEnum.validateJsonElement(jsonObj.get("enum_string"));
}
if (!jsonObj.get("enum_string_required").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `enum_string_required` to be a primitive type in the JSON string but got `%s`", jsonObj.get("enum_string_required").toString()));
}
// validate the required field `enum_string_required`
EnumStringRequiredEnum.validateJsonElement(jsonObj.get("enum_string_required"));
// validate the optional field `enum_integer`
if (jsonObj.get("enum_integer") != null && !jsonObj.get("enum_integer").isJsonNull()) {
EnumIntegerEnum.validateJsonElement(jsonObj.get("enum_integer"));
}
// validate the optional field `enum_number`
if (jsonObj.get("enum_number") != null && !jsonObj.get("enum_number").isJsonNull()) {
EnumNumberEnum.validateJsonElement(jsonObj.get("enum_number"));
}
// validate the optional field `outerEnum`
if (jsonObj.get("outerEnum") != null && !jsonObj.get("outerEnum").isJsonNull()) {
OuterEnum.validateJsonElement(jsonObj.get("outerEnum"));
}
}
public static class CustomTypeAdapterFactory implements TypeAdapterFactory {

View File

@@ -102,6 +102,11 @@ public class MapTest {
return InnerEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
InnerEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_MAP_OF_ENUM_STRING = "map_of_enum_string";

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";
@@ -344,6 +349,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

@@ -18,6 +18,7 @@ import com.google.gson.annotations.SerializedName;
import java.io.IOException;
import com.google.gson.TypeAdapter;
import com.google.gson.JsonElement;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
@@ -70,5 +71,10 @@ public enum OuterEnum {
return OuterEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
OuterEnum.fromValue(value);
}
}

View File

@@ -124,6 +124,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";
@@ -401,6 +406,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 {