|
24 | 24 | import java.util.Set;
|
25 | 25 |
|
26 | 26 | public class TypeValidator extends BaseJsonValidator implements JsonValidator {
|
| 27 | + private static final String TYPE = "type"; |
| 28 | + private static final String ENUM = "enum"; |
| 29 | + private static final String REF = "$ref"; |
| 30 | + |
27 | 31 | private static final Logger logger = LoggerFactory.getLogger(TypeValidator.class);
|
28 | 32 |
|
29 | 33 | private JsonType schemaType;
|
| 34 | + private JsonSchema parentSchema; |
30 | 35 | private UnionTypeValidator unionTypeValidator;
|
31 | 36 |
|
32 | 37 | public TypeValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
|
33 | 38 | super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.TYPE, validationContext);
|
34 | 39 | schemaType = TypeFactory.getSchemaNodeType(schemaNode);
|
| 40 | + this.parentSchema = parentSchema; |
35 | 41 |
|
36 | 42 | if (schemaType == JsonType.UNION) {
|
37 | 43 | unionTypeValidator = new UnionTypeValidator(schemaPath, schemaNode, parentSchema, validationContext);
|
@@ -62,6 +68,11 @@ public boolean equalsToSchemaType(JsonNode node) {
|
62 | 68 | return true;
|
63 | 69 | }
|
64 | 70 | }
|
| 71 | + // Skip the type validation when the schema is an enum object schema. Since the current type |
| 72 | + // of node itself can be used for type validation. |
| 73 | + if (isEnumObjectSchema(parentSchema)) { |
| 74 | + return true; |
| 75 | + } |
65 | 76 | if(config.isTypeLoose()) {
|
66 | 77 | // if typeLoose is true, everything can be a size 1 array
|
67 | 78 | if (schemaType == JsonType.ARRAY) {
|
@@ -219,4 +230,28 @@ public static boolean isNumber(JsonNode node, boolean isTypeLoose) {
|
219 | 230 | }
|
220 | 231 | return false;
|
221 | 232 | }
|
| 233 | + |
| 234 | + private static boolean isEnumObjectSchema(JsonSchema jsonSchema) { |
| 235 | + // There are three conditions for enum object schema |
| 236 | + // 1. The current schema contains key "type", and the value is object |
| 237 | + // 2. The current schema contains key "enum", and the value is an array |
| 238 | + // 3. The parent schema if refer from components, which means the corresponding enum object class would be generated |
| 239 | + JsonNode typeNode = null; |
| 240 | + JsonNode enumNode = null; |
| 241 | + JsonNode refNode = null; |
| 242 | + |
| 243 | + if (jsonSchema != null) { |
| 244 | + if (jsonSchema.getSchemaNode() != null) { |
| 245 | + typeNode = jsonSchema.getSchemaNode().get(TYPE); |
| 246 | + enumNode = jsonSchema.getSchemaNode().get(ENUM); |
| 247 | + } |
| 248 | + if (jsonSchema.getParentSchema() != null && jsonSchema.getParentSchema().getSchemaNode() != null) { |
| 249 | + refNode = jsonSchema.getParentSchema().getSchemaNode().get(REF); |
| 250 | + } |
| 251 | + } |
| 252 | + if (typeNode != null && enumNode != null && refNode != null) { |
| 253 | + return TypeFactory.getSchemaNodeType(typeNode) == JsonType.OBJECT && enumNode.isArray(); |
| 254 | + } |
| 255 | + return false; |
| 256 | + } |
222 | 257 | }
|
0 commit comments