Skip to content

Commit 3a3e1a8

Browse files
committed
fixes #313 add test case to reproduce the issue
1 parent 3069333 commit 3a3e1a8

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.Assert;
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
9+
import java.io.InputStream;
10+
import java.util.Set;
11+
12+
public class Issue313Test {
13+
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
14+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
15+
return factory.getSchema(schemaContent);
16+
}
17+
18+
protected JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) {
19+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
20+
return factory.getSchema(schemaContent);
21+
}
22+
23+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
24+
ObjectMapper mapper = new ObjectMapper();
25+
JsonNode node = mapper.readTree(content);
26+
return node;
27+
}
28+
29+
@Test
30+
@Ignore
31+
public void shouldFailV201909() throws Exception {
32+
String schemaPath = "/schema/issue313-2019-09.json";
33+
String dataPath = "/data/issue313.json";
34+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
35+
JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream);
36+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
37+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
38+
Set<ValidationMessage> errors = schema.validate(node);
39+
Assert.assertEquals(2, errors.size());
40+
}
41+
42+
@Test
43+
public void shouldFailV7() throws Exception {
44+
String schemaPath = "/schema/issue313-v7.json";
45+
String dataPath = "/data/issue313.json";
46+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
47+
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
48+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
49+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
50+
Set<ValidationMessage> errors = schema.validate(node);
51+
Assert.assertEquals(2, errors.size());
52+
}
53+
54+
}

src/test/resources/data/issue313.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"properties": {
3+
"field1": {
4+
"type": "invalid-type",
5+
"description": "string"
6+
}
7+
}
8+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"$id": "https://json-schema.org/draft/2019-09/schema",
4+
"$vocabulary": {
5+
"https://json-schema.org/draft/2019-09/vocab/core": true,
6+
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
7+
"https://json-schema.org/draft/2019-09/vocab/validation": true,
8+
"https://json-schema.org/draft/2019-09/vocab/meta-data": true,
9+
"https://json-schema.org/draft/2019-09/vocab/format": false,
10+
"https://json-schema.org/draft/2019-09/vocab/content": true
11+
},
12+
"$recursiveAnchor": true,
13+
"title": "Core and Validation specifications meta-schema",
14+
"allOf": [
15+
{"$ref": "meta/core"},
16+
{"$ref": "meta/applicator"},
17+
{"$ref": "meta/validation"},
18+
{"$ref": "meta/meta-data"},
19+
{"$ref": "meta/format"},
20+
{"$ref": "meta/content"}
21+
],
22+
"type": ["object", "boolean"],
23+
"properties": {
24+
"definitions": {
25+
"$comment": "While no longer an official keyword as it is replaced by $defs, this keyword is retained in the meta-schema to prevent incompatible extensions as it remains in common use.",
26+
"type": "object",
27+
"additionalProperties": { "$recursiveRef": "#" },
28+
"default": {}
29+
},
30+
"dependencies": {
31+
"$comment": "\"dependencies\" is no longer a keyword, but schema authors should avoid redefining it to facilitate a smooth transition to \"dependentSchemas\" and \"dependentRequired\"",
32+
"type": "object",
33+
"additionalProperties": {
34+
"anyOf": [
35+
{ "$recursiveRef": "#" },
36+
{ "$ref": "meta/validation#/$defs/stringArray" }
37+
]
38+
}
39+
}
40+
}
41+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "http://json-schema.org/draft-07/schema#",
4+
"title": "Core schema meta-schema",
5+
"definitions": {
6+
"schemaArray": {
7+
"type": "array",
8+
"minItems": 1,
9+
"items": { "$ref": "#" }
10+
},
11+
"nonNegativeInteger": {
12+
"type": "integer",
13+
"minimum": 0
14+
},
15+
"nonNegativeIntegerDefault0": {
16+
"allOf": [
17+
{ "$ref": "#/definitions/nonNegativeInteger" },
18+
{ "default": 0 }
19+
]
20+
},
21+
"simpleTypes": {
22+
"enum": [
23+
"array",
24+
"boolean",
25+
"integer",
26+
"null",
27+
"number",
28+
"object",
29+
"string"
30+
]
31+
},
32+
"stringArray": {
33+
"type": "array",
34+
"items": { "type": "string" },
35+
"uniqueItems": true,
36+
"default": []
37+
}
38+
},
39+
"type": ["object", "boolean"],
40+
"properties": {
41+
"$id": {
42+
"type": "string",
43+
"format": "uri-reference"
44+
},
45+
"$schema": {
46+
"type": "string",
47+
"format": "uri"
48+
},
49+
"$ref": {
50+
"type": "string",
51+
"format": "uri-reference"
52+
},
53+
"$comment": {
54+
"type": "string"
55+
},
56+
"title": {
57+
"type": "string"
58+
},
59+
"description": {
60+
"type": "string"
61+
},
62+
"default": true,
63+
"readOnly": {
64+
"type": "boolean",
65+
"default": false
66+
},
67+
"writeOnly": {
68+
"type": "boolean",
69+
"default": false
70+
},
71+
"examples": {
72+
"type": "array",
73+
"items": true
74+
},
75+
"multipleOf": {
76+
"type": "number",
77+
"exclusiveMinimum": 0
78+
},
79+
"maximum": {
80+
"type": "number"
81+
},
82+
"exclusiveMaximum": {
83+
"type": "number"
84+
},
85+
"minimum": {
86+
"type": "number"
87+
},
88+
"exclusiveMinimum": {
89+
"type": "number"
90+
},
91+
"maxLength": { "$ref": "#/definitions/nonNegativeInteger" },
92+
"minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
93+
"pattern": {
94+
"type": "string",
95+
"format": "regex"
96+
},
97+
"additionalItems": { "$ref": "#" },
98+
"items": {
99+
"anyOf": [
100+
{ "$ref": "#" },
101+
{ "$ref": "#/definitions/schemaArray" }
102+
],
103+
"default": true
104+
},
105+
"maxItems": { "$ref": "#/definitions/nonNegativeInteger" },
106+
"minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
107+
"uniqueItems": {
108+
"type": "boolean",
109+
"default": false
110+
},
111+
"contains": { "$ref": "#" },
112+
"maxProperties": { "$ref": "#/definitions/nonNegativeInteger" },
113+
"minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" },
114+
"required": { "$ref": "#/definitions/stringArray" },
115+
"additionalProperties": { "$ref": "#" },
116+
"definitions": {
117+
"type": "object",
118+
"additionalProperties": { "$ref": "#" },
119+
"default": {}
120+
},
121+
"properties": {
122+
"type": "object",
123+
"additionalProperties": { "$ref": "#" },
124+
"default": {}
125+
},
126+
"patternProperties": {
127+
"type": "object",
128+
"additionalProperties": { "$ref": "#" },
129+
"propertyNames": { "format": "regex" },
130+
"default": {}
131+
},
132+
"dependencies": {
133+
"type": "object",
134+
"additionalProperties": {
135+
"anyOf": [
136+
{ "$ref": "#" },
137+
{ "$ref": "#/definitions/stringArray" }
138+
]
139+
}
140+
},
141+
"propertyNames": { "$ref": "#" },
142+
"const": true,
143+
"enum": {
144+
"type": "array",
145+
"items": true,
146+
"minItems": 1,
147+
"uniqueItems": true
148+
},
149+
"type": {
150+
"anyOf": [
151+
{ "$ref": "#/definitions/simpleTypes" },
152+
{
153+
"type": "array",
154+
"items": { "$ref": "#/definitions/simpleTypes" },
155+
"minItems": 1,
156+
"uniqueItems": true
157+
}
158+
]
159+
},
160+
"format": { "type": "string" },
161+
"contentMediaType": { "type": "string" },
162+
"contentEncoding": { "type": "string" },
163+
"if": { "$ref": "#" },
164+
"then": { "$ref": "#" },
165+
"else": { "$ref": "#" },
166+
"allOf": { "$ref": "#/definitions/schemaArray" },
167+
"anyOf": { "$ref": "#/definitions/schemaArray" },
168+
"oneOf": { "$ref": "#/definitions/schemaArray" },
169+
"not": { "$ref": "#" }
170+
},
171+
"default": true
172+
}

0 commit comments

Comments
 (0)