Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,9 @@ public Schema traverseSchema(Schema schema, ReferenceVisitor visitor, List<Strin
resolvedURI = ReferenceUtils.toBaseURI(resolvedURI);
}
context.getIdsCache().put(resolvedURI, Json31.pretty(schema));
if (!visitor.reference.getReferenceSet().containsKey(resolvedURI)) {
visitor.reference.getReferenceSet().put(resolvedURI, visitor.reference);
}
} catch (Exception e) {
//
}
Expand Down Expand Up @@ -920,6 +923,9 @@ public Schema traverseSchema(Schema schema, ReferenceVisitor visitor, List<Strin
visiting.remove(schema);
return handleRootLocalRefs(schema.get$ref(), resolved, context.getOpenApi().getComponents().getSchemas());
}
if (resolvedNotNull && schema.getProperties() != null && !schema.getProperties().isEmpty()) {
traverseSchemaMap(schema.getProperties(), visitor, inheritedIds);
}
// merge ALL STUFF
mergeSchemas(schema, resolved);
visitedMap.put(schema, deepcopy(resolved, Schema.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ private void setUpWireMockServer() throws IOException {
.withBody(pathFile
.getBytes(StandardCharsets.UTF_8))));

pathFile = FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/schema/external-oas-with-id-defs/oas.yaml"), StandardCharsets.UTF_8);

WireMock.stubFor(get(urlPathMatching("/oai-v3.1-schema.yaml"))
.willReturn(aResponse()
.withStatus(HttpURLConnection.HTTP_OK)
.withHeader("Content-type", "application/yaml")
.withBody(pathFile.getBytes(StandardCharsets.UTF_8))));

}

@AfterClass
Expand Down Expand Up @@ -216,6 +224,25 @@ public void testAnchorUnresolve() throws Exception {
compare("$anchor-not-found", swaggerParseResult);
}

@Test
public void testExternalSchemaWithIdAndDefsResolution_issue2271() throws IOException {
String rootYaml = FileUtils.readFileToString(
new File("src/test/resources/3.1.0/dereference/schema/external-oas-with-id-defs/root.yaml"),
StandardCharsets.UTF_8);
rootYaml = rootYaml.replace("${dynamicPort}", String.valueOf(this.serverPort));

ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readContents(rootYaml, null, options);

org.testng.Assert.assertNotNull(result.getOpenAPI());
org.testng.Assert.assertTrue(
result.getMessages() == null
|| result.getMessages().stream().noneMatch(m -> m.contains("Could not find /$defs")),
"Expected no $defs resolution errors when resolving external schema with $id, but got: "
+ result.getMessages());
}

public void compare(String dir, SwaggerParseResult result) throws Exception {
ObjectMapper mapper = Json31.mapper().copy();
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
$id: 'https://spec.openapis.org/oas/3.1/schema/2022-10-07'
$schema: 'https://json-schema.org/draft/2020-12/schema'
type: object
properties:
info:
$ref: '#/$defs/info'
servers:
type: array
items:
$ref: '#/$defs/server'
$ref: '#/$defs/extensions'
$defs:
info:
type: object
properties:
title:
type: string
required:
- title
server:
type: object
properties:
url:
type: string
required:
- url
extensions:
patternProperties:
'^x-': true
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
openapi: 3.1.0
info:
title: Test external OAI schema with $id and $defs resolution
version: 1.0.0
paths:
/hello:
get:
operationId: hello
responses:
"200":
description: A successful response
$ref: '#/components/responses/Data'
components:
responses:
Data:
description: Description of the response
content:
application/json:
schema:
type: object
required:
- items
properties:
items:
message:
type: string
schema:
$ref: "#/components/schemas/OpenApiSchema"
schemas:
OpenApiSchema:
$ref: "http://localhost:${dynamicPort}/oai-v3.1-schema.yaml"
Loading