Skip to content

Commit

Permalink
Merge pull request #2052 from swagger-api/issue-2037
Browse files Browse the repository at this point in the history
Fix issue #2037
  • Loading branch information
gracekarina authored Feb 5, 2024
2 parents d380fbf + 89be06a commit 285907c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
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

0 comments on commit 285907c

Please sign in to comment.