forked from loafle/openapi-generator-original
[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:
@@ -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";
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user