|
1
|
|
|
package com.hltech.vaunt.validator; |
|
2
|
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
|
4
|
|
|
import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema; |
|
5
|
|
|
import com.google.common.collect.Lists; |
|
6
|
|
|
|
|
7
|
|
|
import java.util.Iterator; |
|
8
|
|
|
import java.util.List; |
|
9
|
|
|
import java.util.Map; |
|
10
|
|
|
import java.util.stream.Collectors; |
|
11
|
|
|
|
|
12
|
|
|
public class ObjectSchemaValidator extends ContainerTypeSchemaValidator { |
|
13
|
|
|
private static final String UNMATCHING_SCHEMA_TYPE = |
|
14
|
|
|
"Consumer schema with id %s and type %s does not match provider schema with id %s and name %s"; |
|
15
|
|
|
|
|
16
|
|
|
@Override |
|
17
|
|
|
public List<String> validate(JsonSchema consumerSchema, JsonSchema providerSchema) { |
|
18
|
|
|
List<String> errors = super.validate(consumerSchema, providerSchema); |
|
19
|
|
|
|
|
20
|
|
|
ObjectSchema consumerObjectSchema = consumerSchema.asObjectSchema(); |
|
21
|
|
|
ObjectSchema providerObjectSchema = providerSchema.asObjectSchema(); |
|
22
|
|
|
|
|
23
|
|
|
if (!equals(consumerObjectSchema.getAdditionalProperties(), providerObjectSchema.getAdditionalProperties())) { |
|
24
|
|
|
errors.add(String.format(ERROR_FORMAT_SHORT, |
|
25
|
|
|
consumerSchema.getId(), |
|
26
|
|
|
"additionalProperties")); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (!equals(consumerObjectSchema.getDependencies(), providerObjectSchema.getDependencies())) { |
|
30
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
31
|
|
|
consumerSchema.getId(), |
|
32
|
|
|
"dependencies", |
|
33
|
|
|
consumerObjectSchema.getDependencies(), |
|
34
|
|
|
providerObjectSchema.getDependencies())); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (!equals(consumerObjectSchema.getPatternProperties(), providerObjectSchema.getPatternProperties())) { |
|
38
|
|
|
errors.add(String.format(ERROR_FORMAT, |
|
39
|
|
|
consumerSchema.getId(), |
|
40
|
|
|
"patternProperties", |
|
41
|
|
|
mapToString(consumerObjectSchema.getPatternProperties()), |
|
42
|
|
|
mapToString(providerObjectSchema.getPatternProperties()))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
errors.addAll(compareObjectProperties( |
|
46
|
|
|
consumerSchema.getId(), |
|
47
|
|
|
consumerObjectSchema.getProperties(), |
|
48
|
|
|
providerObjectSchema.getProperties())); |
|
49
|
|
|
|
|
50
|
|
|
return errors; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
@Override |
|
54
|
|
|
public Class<ObjectSchema> supportsSchemaType() { |
|
55
|
|
|
return ObjectSchema.class; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private List<String> compareObjectProperties(String id, Map<String, JsonSchema> consumerProperties, |
|
59
|
|
|
Map<String, JsonSchema> providerProperties) { |
|
60
|
|
|
|
|
61
|
|
|
if (!providerProperties.keySet().containsAll(consumerProperties.keySet())) { |
|
62
|
|
|
return Lists.newArrayList(String.format(ERROR_FORMAT, |
|
63
|
|
|
id, |
|
64
|
|
|
"properties", |
|
65
|
|
|
"ids of properties: " + consumerProperties.keySet(), |
|
66
|
|
|
"ids of properties: " + providerProperties.keySet() |
|
67
|
|
|
)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return consumerProperties.keySet().stream() |
|
71
|
|
|
.flatMap(key -> vslidateProperty(consumerProperties.get(key), providerProperties.get(key)).stream()) |
|
72
|
|
|
.collect(Collectors.toList()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private List<String> vslidateProperty(JsonSchema consumerProperty, JsonSchema providerProperty) { |
|
76
|
|
|
return consumerProperty.getClass() == providerProperty.getClass() |
|
77
|
|
|
? VauntSchemaValidator.validate(consumerProperty, providerProperty) |
|
78
|
|
|
: Lists.newArrayList(String.format( |
|
79
|
|
|
UNMATCHING_SCHEMA_TYPE, |
|
80
|
|
|
consumerProperty.getId(), |
|
81
|
|
|
consumerProperty.getClass().getSimpleName(), |
|
82
|
|
|
providerProperty.getId(), |
|
83
|
|
|
providerProperty.getClass().getSimpleName())); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private String mapToString(Map<String, JsonSchema> props) { |
|
87
|
|
|
if (props.isEmpty()) { |
|
88
|
|
|
return "{}"; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
int max = props.size() - 1; |
|
92
|
|
|
|
|
93
|
|
|
StringBuilder sb = new StringBuilder(); |
|
94
|
|
|
Iterator<Map.Entry<String, JsonSchema>> it = props.entrySet().iterator(); |
|
95
|
|
|
|
|
96
|
|
|
sb.append('{'); |
|
97
|
|
|
for (int i = 0; ; i++) { |
|
98
|
|
|
Map.Entry<String, JsonSchema> entry = it.next(); |
|
99
|
|
|
sb.append(entry.getKey()); |
|
100
|
|
|
sb.append('='); |
|
101
|
|
|
sb.append(jsonToString(entry.getValue())); |
|
102
|
|
|
|
|
103
|
|
|
if (i == max) { |
|
104
|
|
|
return sb.append('}').toString(); |
|
105
|
|
|
} |
|
106
|
|
|
sb.append(", "); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|