Skip to content

Commit af2d7e8

Browse files
djankowsfrantuma
authored andcommitted
Add
1 parent f74a954 commit af2d7e8

File tree

7 files changed

+111
-23
lines changed

7 files changed

+111
-23
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public class OpenAPIDeserializer {
173173
"default", "discriminator", "readOnly", "writeOnly", "xml", "externalDocs", "example", "deprecated",
174174
"const", "examples", "$id", "$comment", "if", "then", "else", "unevaluatedProperties","unevaluatedItems", "prefixItems",
175175
"contains","contentEncoding","contentMediaType","$anchor","$schema","contentSchema","propertyNames",
176-
"dependentSchemas","dependentRequired","minContains","maxContains","patternProperties", "$vocabulary", "$dynamicAnchor"));
176+
"dependentSchemas","dependentRequired","minContains","maxContains","patternProperties", "$vocabulary",
177+
"$dynamicAnchor", "$dynamicRef"));
177178
protected static Set<String> EXAMPLE_KEYS_31 = new LinkedHashSet<>(Arrays.asList("$ref", "summary", "description",
178179
"value", "externalValue"));
179180
protected static Set<String> HEADER_KEYS_31 = new LinkedHashSet<>(Arrays.asList("$ref", "name", "in", "description",
@@ -4120,9 +4121,7 @@ public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult resu
41204121
dependentRequired.add(n.textValue());
41214122
}
41224123
}
4123-
if (dependentRequired != null) {
4124-
dependentRequiredList.put(name, dependentRequired);
4125-
}
4124+
dependentRequiredList.put(name, dependentRequired);
41264125
}
41274126
}
41284127
}
@@ -4143,14 +4142,12 @@ public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult resu
41434142
dependentSchemasList.put(name, dependentSchemas);
41444143
}
41454144
}
4146-
if (dependentSchemasObj != null) {
4147-
schema.setDependentSchemas(dependentSchemasList);
4148-
}
4145+
schema.setDependentSchemas(dependentSchemasList);
41494146
}
41504147

41514148
//prefixItems
41524149
ArrayNode prefixItemsArray = getArray("prefixItems", node, false, location, result);
4153-
if(prefixItemsArray != null) {
4150+
if (prefixItemsArray != null) {
41544151
Schema prefixItems = new JsonSchema();
41554152

41564153
List<Schema> prefixItemsList = new ArrayList<>();
@@ -4180,11 +4177,9 @@ public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult resu
41804177
Set<String> keys = getKeys(propertiesObj);
41814178
for (String name : keys) {
41824179
JsonNode propertyValue = propertiesObj.get(name);
4183-
if (propertiesObj != null) {
4184-
property = getJsonSchema(propertyValue, location, result);
4185-
if (property != null) {
4186-
properties.put(name, property);
4187-
}
4180+
property = getJsonSchema(propertyValue, location, result);
4181+
if (property != null) {
4182+
properties.put(name, property);
41884183
}
41894184
}
41904185
if (propertiesObj != null) {
@@ -4250,6 +4245,11 @@ public Schema getJsonSchema(JsonNode jsonNode, String location, ParseResult resu
42504245
schema.set$dynamicAnchor(value);
42514246
}
42524247

4248+
value = getString("$dynamicRef", node, false, location, result);
4249+
if (value != null) {
4250+
schema.set$dynamicRef(value);
4251+
}
4252+
42534253
value = getString("$id", node, false, location, result);
42544254
if (value != null) {
42554255
schema.set$id(value);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.swagger.v3.parser.test;
2+
3+
import io.swagger.v3.oas.models.media.Schema;
4+
import io.swagger.v3.parser.OpenAPIV3Parser;
5+
import io.swagger.v3.parser.core.models.SwaggerParseResult;
6+
import org.testng.annotations.Test;
7+
8+
import static org.testng.Assert.assertEquals;
9+
import static org.testng.Assert.assertNotNull;
10+
11+
public class OpenAPIV3ParserDynamicRefTest {
12+
@Test
13+
public void testDynamicRefParsing() {
14+
SwaggerParseResult result = new OpenAPIV3Parser()
15+
.readLocation("dynamicRef/dynamicref-example.yaml", null, null);
16+
17+
assertNotNull(result.getOpenAPI(), "Parsed OpenAPI object should not be null");
18+
19+
Schema<?> rootSchema = result.getOpenAPI()
20+
.getPaths().get("/tree").getGet()
21+
.getResponses().get("200").getContent()
22+
.get("application/json").getSchema();
23+
24+
assertEquals(rootSchema.get$ref(), "#/components/schemas/Node",
25+
"Expected root schema to be a $ref to Node");
26+
27+
Schema<?> nodeSchema = result.getOpenAPI().getComponents()
28+
.getSchemas().get("Node");
29+
30+
assertNotNull(nodeSchema, "Node schema should be parsed");
31+
32+
Schema<?> childrenSchema = (Schema<?>) nodeSchema.getProperties().get("children");
33+
Schema<?> itemsSchema = childrenSchema.getItems();
34+
35+
// THIS is the actual test: you should have a get$dynamicRef() field
36+
assertEquals("#node", itemsSchema.get$dynamicRef(), "Expected $dynamicRef to be preserved");
37+
}
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Test DynamicRef
4+
version: 1.0.0
5+
paths:
6+
/tree:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/schemas/Node"
15+
16+
components:
17+
schemas:
18+
Node:
19+
$id: "https://example.com/schemas/node"
20+
$dynamicAnchor: "node"
21+
type: object
22+
properties:
23+
name:
24+
type: string
25+
children:
26+
type: array
27+
items:
28+
$dynamicRef: "#node"

modules/swagger-parser/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@
3232
<version>${jmockit-version}</version>
3333
<scope>test</scope>
3434
</dependency>
35-
<dependency>
36-
<groupId>junit</groupId>
37-
<artifactId>junit</artifactId>
38-
<version>${junit-version}</version>
39-
<scope>test</scope>
40-
</dependency>
4135
<dependency>
4236
<groupId>commons-io</groupId>
4337
<artifactId>commons-io</artifactId>

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
import io.swagger.v3.core.util.Json;
1818
import java.math.BigDecimal;
1919
import java.math.MathContext;
20-
import org.junit.Test;
20+
21+
import org.testng.annotations.Test;
2122
import org.testng.Assert;
2223

2324
import java.util.Map;
24-
2525
import java.util.List;
2626

27-
2827
import static org.testng.Assert.assertEquals;
2928
import static org.testng.Assert.assertNotNull;
3029
import static org.testng.Assert.assertTrue;
@@ -772,5 +771,6 @@ public void testIssue1552AdditionalProps() throws Exception {
772771
" x-original-swagger-version: \"2.0\"\n" +
773772
"openapi31: false\n");
774773
}
774+
775775
}
776776

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.1.0
2+
info:
3+
title: Test DynamicRef
4+
version: 1.0.0
5+
paths:
6+
/tree:
7+
get:
8+
responses:
9+
'200':
10+
description: OK
11+
content:
12+
application/json:
13+
schema:
14+
$ref: "#/components/schemas/Node"
15+
16+
components:
17+
schemas:
18+
Node:
19+
$id: "https://example.com/schemas/node"
20+
$dynamicAnchor: "node"
21+
type: object
22+
properties:
23+
name:
24+
type: string
25+
children:
26+
type: array
27+
items:
28+
$dynamicRef: "#node"

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@
416416
<swagger-parser-v2-version>1.0.73</swagger-parser-v2-version>
417417
<commons-io-version>2.18.0</commons-io-version>
418418
<slf4j-version>2.0.9</slf4j-version>
419-
<swagger-core-version>2.2.29</swagger-core-version>
419+
<swagger-core-version>2.2.32</swagger-core-version>
420420
<swagger-core-v2-version>1.6.15</swagger-core-v2-version>
421421
<junit-version>4.13.2</junit-version>
422422
<testng-version>7.11.0</testng-version>

0 commit comments

Comments
 (0)