Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue-1891: Fix for mixed folder and classpath references not resolving. #1892

Merged
merged 1 commit into from
Mar 14, 2023
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 @@ -125,7 +125,7 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) {
if (!ref.equals(RefFormat.URL)) {
String schemaFullRef = schema.get$ref();
String parent = (file.contains("/")) ? file.substring(0, file.lastIndexOf('/')) : "";
if (!parent.isEmpty()) {
if (!parent.isEmpty() && !schemaFullRef.startsWith("/")) {
if (schemaFullRef.contains("#/")) {
String[] parts = schemaFullRef.split("#/");
String schemaFullRefFilePart = parts[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3398,6 +3398,26 @@ public void testValidateExternalRefsTrue() {

}

@Test(description = "test that a model in a folder that has a ref to a model in the classpath is properly resolved.")
public void testIssue1891() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);

SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./issue-1891/openapi.yaml", null, options);
assertTrue(result.getMessages().isEmpty());

// Expect all references to be properly resolved to local references.
OpenAPI openAPI = result.getOpenAPI();
Schema<?> localModel = openAPI.getComponents().getSchemas().get("LocalModel");
Schema<?> typesModel = openAPI.getComponents().getSchemas().get("TypesModel");
Schema<?> sharedModel = openAPI.getComponents().getSchemas().get("SharedModel");

assertEquals("#/components/schemas/TypesModel", localModel.getProperties().get("sharedModelField").get$ref());
assertEquals("#/components/schemas/SharedModel", typesModel.get$ref());
assertNotNull(sharedModel);
}

@Test(description = "directly parsed definition, tested in previous method as reference relative/local ")
public void testValidateDefinition() {
ParseOptions options = new ParseOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
openapi: 3.0.0

info:
description: Models that would published in a jar and then read from the classpath.
version: 1.0.0
title: shared-types

paths:
/empty:
description: >
Empty API so that we can load this file in Swagger Editor for validation and
not get flagged for a contract without a paths element.

components:
schemas:
SharedModel:
description: A shared model
type: object
properties:
name:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
openapi: 3.0.0

info:
description: Models that would published in a jar and then read from the classpath.
version: 1.0.0
title: shared-models

paths:
/empty:
description: >
Empty API so that we can load this file in Swagger Editor for validation and
not get flagged for a contract without a paths element.

components:
schemas:
LocalModel:
type: object
properties:
sharedModelField:
# This is resolved via the file system, but looking in the folder 'types'
$ref: 'types/local-types.yaml#/components/schemas/TypesModel'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
openapi: 3.0.0

info:
description: Models that would published in a jar and then read from the classpath.
version: 1.0.0
title: shared-models

paths:
/empty:
description: >
Empty API so that we can load this file in Swagger Editor for validation and
not get flagged for a contract without a paths element.

components:
schemas:
TypesModel:
# The file 'issue-1891-shared-types.yaml' is located outside this directory and resolved
# via an absolute path reference (starts with a /). Typically, this resource would be
# found in a jar file on the classpath, but for the purposes of this test, it is
# found at the root of the file system, which will be loaded via the classpath helper.
$ref: '/issue-1891-shared-types.yaml#/components/schemas/SharedModel'