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

Fix issue #2037 #2052

Merged
merged 1 commit into from
Feb 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ protected void updateRefs(ApiResponse response, String pathRef) {
if(response.getContent() != null) {
Map<String, MediaType> content = response.getContent();
for (String key: content.keySet()) {
MediaType mediaType = content.get(key);
if (mediaType.getSchema() != null) {
updateRefs(mediaType.getSchema(), pathRef);
}
Map<String, Example> examples = content.get(key).getExamples();
if (examples != null) {
for( Example example:examples.values()){
Expand Down Expand Up @@ -285,10 +289,18 @@ protected boolean isAbsoluteRef(String ref) {
return false;
}

private boolean isInternalSchemaRef(String $ref) {
if($ref.startsWith("#/components/schemas")) {
return true;
}
return false;
}

protected String computeRef(String ref, String prefix) {
if(isLocalRef(ref)) return computeLocalRef(ref, prefix);
if(isAbsoluteRef(ref)) return ref;
return computeRelativeRef(ref, prefix);
if(isLocalRef(ref)&& !isInternalSchemaRef(ref)) return computeLocalRef(ref, prefix);
if(isAbsoluteRef(ref)) return ref;
if(isInternalSchemaRef(ref)) return ref;
return computeRelativeRef(ref, prefix);
}

protected String computeRelativeRef(String ref, String prefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
public class OpenAPIV3ParserTest {
List<AuthorizationValue> auths = new ArrayList<>();

@Test
public void testFailedToResolveResponseReferences() {
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult parseResult = openApiParser.readLocation("issue-2037/openapi.yaml", null, options);
OpenAPI openAPI = parseResult.getOpenAPI();

Assert.assertTrue(openAPI.getPaths().get("/get").get$ref() == null);
Assert.assertEquals(openAPI.getPaths().get("/get").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ResponsesRef");
}


@Test
public void testFailedToResolveExternalReferences() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
openapi: 3.0.3
info:
title: title
version: LATEST

paths:
/get:
$ref: 'paths/get.yaml#/endpoint'
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
endpoint:
get:
operationId: get
requestBody:
content:
application/json:
schema:
$ref: '#/RequestBodyRef'
responses:
'200':
content:
application/json:
schema:
$ref: '#/ResponsesRef'

RequestBodyRef:
type: string

ResponsesRef:
type: string