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 @@ -7,6 +7,7 @@
import io.swagger.models.properties.RefProperty;
import io.swagger.models.refs.RefFormat;
import io.swagger.parser.ResolverCache;
import io.swagger.parser.util.RefUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -153,6 +154,30 @@ public String processRefToExternalDefinition(String $ref, RefFormat refFormat) {
return newRef;
}


public void processRefToExternalResponse(String $ref, RefFormat refFormat) {

final Response response = cache.loadRef($ref, refFormat, Response.class);

if(response != null) {

String file = $ref.split("#/")[0];

Model model = null;
if (response.getResponseSchema() != null) {
model = response.getResponseSchema();
if (model instanceof RefModel) {
RefModel refModel = (RefModel) model;
if (RefUtils.isAnExternalRefFormat(refFormat)) {
processRefModel(refModel, $ref);
} else {
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
}
}
}
}
}

private void processProperties(final Map<String, Property> subProps, final String file) {
if (subProps == null || 0 == subProps.entrySet().size() ) {
return;
Expand Down Expand Up @@ -187,6 +212,16 @@ private void processRefProperty(RefProperty subRef, String externalFile) {
processRefToExternalDefinition(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
}
}

private void processRefModel(RefModel subRef, String externalFile) {

if (isAnExternalRefFormat(subRef.getRefFormat())) {
String joinedRef = join(externalFile, subRef.get$ref());
subRef.set$ref(processRefToExternalDefinition(joinedRef, subRef.getRefFormat()));
} else {
processRefToExternalDefinition(externalFile + subRef.get$ref(), RefFormat.RELATIVE);
}
}


public static String join(String source, String fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void processOperation(Operation operation) {
if(response != null) {
if (response instanceof RefResponse) {
RefResponse refResponse = (RefResponse) response;
responseProcessor.processResponse(response);

Response resolvedResponse = cache.loadRef(refResponse.get$ref(), refResponse.getRefFormat(), Response.class);

if (resolvedResponse != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package io.swagger.parser.processors;

import io.swagger.models.Model;
import io.swagger.models.RefResponse;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.properties.Property;
import io.swagger.models.refs.RefFormat;
import io.swagger.parser.ResolverCache;
import io.swagger.parser.util.RefUtils;

public class ResponseProcessor {

private final ModelProcessor modelProcessor;
private final ExternalRefProcessor externalRefProcessor;

public ResponseProcessor(ResolverCache cache, Swagger swagger) {
modelProcessor = new ModelProcessor(cache, swagger);
externalRefProcessor = new ExternalRefProcessor(cache, swagger);

}

public void processResponse(Response response) {
//process the response body
final Model schema = response.getResponseSchema();

if (response instanceof RefResponse) {
RefResponse refResponse = (RefResponse) response;
processReferenceResponse(refResponse);
}


if (schema != null) {
modelProcessor.processModel(schema);
}
Expand All @@ -29,4 +40,12 @@ public void processResponse(Response response) {
*/

}

public void processReferenceResponse(RefResponse refResponse){
RefFormat refFormat = refResponse.getRefFormat();
String $ref = refResponse.get$ref();
if (RefUtils.isAnExternalRefFormat(refFormat)){
externalRefProcessor.processRefToExternalResponse($ref, refFormat);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -114,6 +119,8 @@ else if ("..".equals(relPathParts[i])) {
return StringUtils.join(outputParts, "/");
}



public static String readExternalRef(String file, RefFormat refFormat, List<AuthorizationValue> auths,
Path parentDirectory) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@

public class SwaggerParserTest {

@Test
public void testIssue811() throws Exception {
SwaggerParser parser = new SwaggerParser();
final Swagger swagger = parser.read("src/test/resources/oapi-reference-test/index.yaml");
Assert.assertNotNull(swagger);
assertTrue(swagger.getPaths().get("/").getGet().getResponses().get("200").getResponseSchema() instanceof RefModel);
RefModel model = (RefModel) swagger.getPaths().get("/").getGet().getResponses().get("200").getResponseSchema();
Assert.assertEquals(model.get$ref(),"#/definitions/schema-with-reference");

}

@Test
public void testIssue727() throws Exception {
SwaggerParser parser = new SwaggerParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testProcessOperation(@Injectable final List<Parameter> inputParamete
operation.setParameters(inputParameterList);

final String ref = "http://my.company.com/path/to/file.json#/foo/bar";
RefResponse refResponse = new RefResponse(ref);
final RefResponse refResponse = new RefResponse(ref);

operation.response(200, refResponse);
operation.response(400, incomingResponse);
Expand All @@ -59,6 +59,9 @@ public void testProcessOperation(@Injectable final List<Parameter> inputParamete
times = 1;
result = outputParameterList;

responseProcessor.processResponse(refResponse);
times = 1;

cache.loadRef(ref, RefFormat.URL, Response.class);
times = 1;
result = resolvedResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
type: object
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
description: Response with schema containing reference
schema:
$ref: "./schema-with-reference.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
type: object
properties:
reference_array:
type: array
items:
$ref: "./referent.yaml"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
swagger: "2.0"

info:
version: 1.0.0
title: Path include test case child

paths:
/:
get:
description: Test
summary: Test
operationId: getTest
responses:
200:
$ref: "./components/response-with-reference.yaml"
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"parameters": [
{
"$ref": "./parameters/params.json#/param1"
"$ref": "../parameters/params.json#/param1"
},
{
"$ref": "./parameters/params.json#/param2"
"$ref": "../parameters/params.json#/param2"
}
],
"get": {
Expand All @@ -25,22 +25,22 @@
"in": "body",
"required": true,
"schema": {
"$ref": "./models/health.json"
"$ref": "../models/health.json"
}
}
],
"responses": {
"200": {
"description": "Health information from the server",
"schema": {
"$ref": "./models/health.json"
"$ref": "../models/health.json"
}
},
"400": {
"$ref": "./responses/errorResponses.json#/bad-request"
"$ref": "../responses/errorResponses.json#/bad-request"
},
"500": {
"$ref": "./responses/errorResponses.json#/internal-server-error"
"$ref": "../responses/errorResponses.json#/internal-server-error"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"bad-request": {
"description": "Your request was not valid",
"schema": {
"$ref": "./models/error.json"
"$ref": "../models/error.json"
}
},
"internal-server-error": {
"description": "An unexpected error occur during processing",
"schema": {
"$ref": "./models/error.json"
"$ref": "../models/error.json"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
parameters:
- $ref: "./parameters/params.yaml#/param1"
- $ref: "./parameters/params.yaml#/param2"
- $ref: "../parameters/params.yaml#/param1"
- $ref: "../parameters/params.yaml#/param2"
get:
summary: "Returns server health information"
operationId: "getHealth"
Expand All @@ -14,13 +14,13 @@ get:
in: "body"
required: true
schema:
$ref: "./models/health.yaml"
$ref: "../models/health.yaml"
responses:
200:
description: "Health information from the server"
schema:
$ref: "./models/health.yaml"
$ref: "../models/health.yaml"
400:
$ref: "./responses/errorResponses.yaml#/bad-request"
$ref: "../responses/errorResponses.yaml#/bad-request"
500:
$ref: "./responses/errorResponses.yaml#/internal-server-error"
$ref: "../responses/errorResponses.yaml#/internal-server-error"
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
bad-request:
description: "Your request was not valid"
schema:
$ref: "./models/error.yaml"
$ref: "../models/error.yaml"
internal-server-error:
description: "An unexpected error occur during processing"
schema:
$ref: "./models/error.yaml"
$ref: "../models/error.yaml"