[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

@@ -106,6 +106,11 @@ public class DefaultValue {
return ArrayStringEnumDefaultEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
ArrayStringEnumDefaultEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_ARRAY_STRING_ENUM_DEFAULT = "array_string_enum_default";

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";
@@ -399,6 +404,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 {

View File

@@ -104,6 +104,11 @@ public class Query {
return OutcomesEnum.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
OutcomesEnum.fromValue(value);
}
}
public static final String SERIALIZED_NAME_OUTCOMES = "outcomes";

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 StringEnumRef {
return StringEnumRef.fromValue(value);
}
}
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
String value = jsonElement.getAsString();
StringEnumRef.fromValue(value);
}
}