Skip to content

Commit ad486f4

Browse files
authored
Merge pull request #207 from andersonf/bugfix/ref-with-name-duplicate-files
Fixing ref validation with same name but different files
2 parents bbddae6 + 31a7712 commit ad486f4

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/main/java/com/networknt/schema/RefValidator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSch
4545
}
4646

4747
static JsonSchemaRef getRefSchema(JsonSchema parentSchema, ValidationContext validationContext, String refValue) {
48+
final String refValueOriginal = refValue;
49+
4850
if (!refValue.startsWith(REF_CURRENT)) {
4951
// This will be the uri extracted from the refValue (this may be a relative or absolute uri).
5052
final String refUri;
@@ -76,10 +78,10 @@ static JsonSchemaRef getRefSchema(JsonSchema parentSchema, ValidationContext val
7678
} else {
7779
JsonNode node = parentSchema.getRefSchemaNode(refValue);
7880
if (node != null) {
79-
JsonSchemaRef ref = validationContext.getReferenceParsingInProgress(refValue);
81+
JsonSchemaRef ref = validationContext.getReferenceParsingInProgress(refValueOriginal);
8082
if (ref == null) {
8183
ref = new JsonSchemaRef(validationContext, refValue);
82-
validationContext.setReferenceParsingInProgress(refValue, ref);
84+
validationContext.setReferenceParsingInProgress(refValueOriginal, ref);
8385
JsonSchema ret = new JsonSchema(validationContext, refValue, parentSchema.getCurrentUri(), node, parentSchema);
8486
ref.set(ret);
8587
}

src/test/resources/tests/ref.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@
9595
}
9696
]
9797
},
98+
{
99+
"description": "nested refs with duplicate ref values for different files",
100+
"schema": {
101+
"definitions": {
102+
"a": {"$ref": "subSchemas.json#/definitions/a"},
103+
"b": {"$ref": "#/definitions/a"},
104+
"c": {"$ref": "#/definitions/b"}
105+
},
106+
"$ref": "#/definitions/c"
107+
},
108+
"tests": [
109+
{
110+
"description": "nested ref valid",
111+
"data": 5,
112+
"valid": true
113+
},
114+
{
115+
"description": "nested ref invalid",
116+
"data": "a",
117+
"valid": false
118+
}
119+
]
120+
},
98121
{
99122
"description": "remote ref, containing refs itself",
100123
"schema": {"$ref": "http://json-schema.org/draft-04/schema#"},

src/test/resources/tests/subSchemas.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
},
55
"refToInteger" : {
66
"$ref" : "#/integer"
7+
},
8+
"definitions" : {
9+
"a" : { "type": "integer" }
710
}
811
}

0 commit comments

Comments
 (0)