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

Correctly resolve externalRefs that point to the root path #1963

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 @@ -30,6 +30,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -108,17 +109,25 @@ public ResolverCache(OpenAPI openApi, List<AuthorizationValue> auths, String par

}

private <T> T loadSchemaForInternalRef(String ref, Class<T> expectedType) {
Object loadedRef = loadInternalRef(ref);

try{
return expectedType.cast(loadedRef);
}
catch (Exception e) {
return null;
}
}

private boolean fileIsRootPath(Path file) {
return this.rootPath != null && file.normalize().equals(Paths.get(this.rootPath).toAbsolutePath().normalize());
}

public <T> T loadRef(String ref, RefFormat refFormat, Class<T> expectedType) {
if (refFormat == RefFormat.INTERNAL) {
//we don't need to go get anything for internal refs
Object loadedRef = loadInternalRef(ref);

try{
return expectedType.cast(loadedRef);
}
catch (Exception e) {
return null;
}
return loadSchemaForInternalRef(ref, expectedType);
}

final String[] refParts = ref.split("#/");
Expand All @@ -130,6 +139,12 @@ public <T> T loadRef(String ref, RefFormat refFormat, Class<T> expectedType) {
final String file = refParts[0];
final String definitionPath = refParts.length == 2 ? refParts[1] : null;

if (this.parentDirectory != null && fileIsRootPath(this.parentDirectory.resolve(file))) {
// If the file part of this external ref refers to the same file as the root path, we can treat it like an
// internal ref.
return loadSchemaForInternalRef("#/" + definitionPath, expectedType);
}

//we might have already resolved this ref, so check the resolutionCache
Object previouslyResolvedEntity = resolutionCache.get(ref);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.swagger.v3.parser.test;

import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.testng.annotations.Test;

import static org.testng.Assert.assertFalse;

public class ResolverCacheCircularTest {
@Test
public void testIssue1961_DuplicateSchemas_ABA() throws Exception {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1961/Foo.yaml", null, parseOptions);
assertFalse(result.getOpenAPI().getComponents().getSchemas().containsKey("FooBar_1"));
}
}
13 changes: 13 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1961/Bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
components:
schemas:
Bar:
anyOf:
- $ref: ./Foo.yaml#/components/schemas/FooBar
BarType:
enum:
- All
- OEM
type: string
info:
title: '#Issue1961.Bar'
openapi: 3.0.1
17 changes: 17 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1961/Foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
components:
schemas:
FooBar:
properties:
BypassTypes:
items:
$ref: ./Bar.yaml#/components/schemas/BarType
type: array
type: object
Foo:
properties:
MFABypass:
$ref: ./Bar.yaml#/components/schemas/Bar
type: object
info:
title: '#Issue1961.Foo'
openapi: 3.0.1
Loading