Skip to content

Commit 51fd82b

Browse files
authored
Fix resolve (#952)
1 parent 61bf64a commit 51fd82b

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,21 @@ public static String resolve(String parent, String iri) {
106106
scheme = scheme + 3;
107107
}
108108
base = parent(base, scheme);
109-
while (iri.startsWith("../")) {
110-
base = parent(base, scheme);
111-
iri = iri.substring(3);
109+
110+
String[] iriParts = iri.split("/");
111+
for (int x = 0; x < iriParts.length; x++) {
112+
if ("..".equals(iriParts[x])) {
113+
base = parent(base, scheme);
114+
} else if (".".equals(iriParts[x])) {
115+
// skip
116+
} else {
117+
base = base + "/" + iriParts[x];
118+
}
119+
}
120+
if (iri.endsWith("/")) {
121+
base = base + "/";
112122
}
113-
return base + "/" + iri;
123+
return base;
114124
}
115125
}
116126
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.networknt.schema;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.util.Collections;
21+
import java.util.Set;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import com.networknt.schema.SpecVersion.VersionFlag;
26+
27+
/**
28+
* Tests for RefValidator.
29+
*/
30+
public class RefValidatorTest {
31+
@Test
32+
void resolveSamePathDotSlash() {
33+
String mainSchema = "{\r\n"
34+
+ " \"$id\": \"https://www.example.com/schema/test.json\",\r\n"
35+
+ " \"$ref\": \"./integer.json\"\r\n"
36+
+ "}";
37+
38+
String otherSchema = "{\r\n"
39+
+ " \"type\": \"integer\"\r\n"
40+
+ "}";
41+
42+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
43+
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
44+
Collections.singletonMap("https://www.example.com/schema/integer.json", otherSchema))));
45+
JsonSchema jsonSchema = factory.getSchema(mainSchema);
46+
Set<ValidationMessage> messages = jsonSchema.validate("\"string\"", InputFormat.JSON);
47+
assertEquals(1, messages.size());
48+
}
49+
50+
@Test
51+
void resolveSamePath() {
52+
String mainSchema = "{\r\n"
53+
+ " \"$id\": \"https://www.example.com/schema/test.json\",\r\n"
54+
+ " \"$ref\": \"integer.json\"\r\n"
55+
+ "}";
56+
57+
String otherSchema = "{\r\n"
58+
+ " \"type\": \"integer\"\r\n"
59+
+ "}";
60+
61+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
62+
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
63+
Collections.singletonMap("https://www.example.com/schema/integer.json", otherSchema))));
64+
JsonSchema jsonSchema = factory.getSchema(mainSchema);
65+
Set<ValidationMessage> messages = jsonSchema.validate("\"string\"", InputFormat.JSON);
66+
assertEquals(1, messages.size());
67+
}
68+
69+
@Test
70+
void resolveParent() {
71+
String mainSchema = "{\r\n"
72+
+ " \"$id\": \"https://www.example.com/schema/test.json\",\r\n"
73+
+ " \"$ref\": \"../integer.json\"\r\n"
74+
+ "}";
75+
76+
String otherSchema = "{\r\n"
77+
+ " \"type\": \"integer\"\r\n"
78+
+ "}";
79+
80+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
81+
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
82+
Collections.singletonMap("https://www.example.com/integer.json", otherSchema))));
83+
JsonSchema jsonSchema = factory.getSchema(mainSchema);
84+
Set<ValidationMessage> messages = jsonSchema.validate("\"string\"", InputFormat.JSON);
85+
assertEquals(1, messages.size());
86+
}
87+
88+
@Test
89+
void resolveComplex() {
90+
String mainSchema = "{\r\n"
91+
+ " \"$id\": \"https://www.example.com/schema/test.json\",\r\n"
92+
+ " \"$ref\": \"./hello/././world/../integer.json\"\r\n"
93+
+ "}";
94+
95+
String otherSchema = "{\r\n"
96+
+ " \"type\": \"integer\"\r\n"
97+
+ "}";
98+
99+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
100+
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
101+
Collections.singletonMap("https://www.example.com/schema/hello/integer.json", otherSchema))));
102+
JsonSchema jsonSchema = factory.getSchema(mainSchema);
103+
Set<ValidationMessage> messages = jsonSchema.validate("\"string\"", InputFormat.JSON);
104+
assertEquals(1, messages.size());
105+
}
106+
}

0 commit comments

Comments
 (0)