Skip to content

Commit

Permalink
Merge pull request #1355 from swagger-api/reference-response-schema
Browse files Browse the repository at this point in the history
Reference response schema
  • Loading branch information
gracekarina authored Apr 14, 2020
2 parents 86f1829 + 398b1fe commit 1136de9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


import static io.swagger.parser.util.RefUtils.computeDefinitionName;
Expand Down Expand Up @@ -180,24 +181,66 @@ public String processRefToExternalResponse(String $ref, RefFormat refFormat) {

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(refModel.getRefFormat())) {
processRefModel(refModel, $ref);
} else {
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
}
}
processRefSchemaObject(response.getResponseSchema(), $ref);
}
}
return newRef;
}

private void processRefSchemaObject(Model schema, String $ref) {
String file = $ref.split("#/")[0];

if (schema instanceof RefModel) {
RefModel refModel = (RefModel) schema;
RefFormat ref = refModel.getRefFormat();
if (isAnExternalRefFormat(ref)) {
processRefModel(refModel, $ref);
} else {
processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE);
}
}else{
processSchema(schema,file);
}
}

private void processSchema(Model property, String file) {
if (property != null) {
if (property instanceof RefModel) {
processRefModel((RefModel)property, file);
}
if (property.getProperties() != null) {
processProperties(property.getProperties(), file);
}
if (property instanceof ArrayModel) {
processProperty(((ArrayModel) property).getItems(), file);
}
if (property instanceof MapProperty){
MapProperty mapProperty = (MapProperty) property;
if (mapProperty.getAdditionalProperties() instanceof Model) {
processProperty(mapProperty.getAdditionalProperties(), file);
}
}
if (property instanceof ComposedModel) {
ComposedModel composed = (ComposedModel) property;
processComposedProperties(composed.getAllOf(), file);
}
}
}

private void processProperty(Property property, String file) {
}

private void processComposedProperties(Collection<Model> properties, String file) {
if (properties != null) {
for (Model property : properties) {
processSchema(property, file);
}
}
}


private void processDiscriminator(String discriminator, Map<String, Property> properties, String file) {
if (properties == null || properties.isEmpty()) {
return;
Expand Down Expand Up @@ -292,14 +335,42 @@ private void processRefProperty(RefProperty subRef, String externalFile) {

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);
RefFormat format = subRef.getRefFormat();

if (!isAnExternalRefFormat(format)) {
subRef.set$ref(RefType.DEFINITION.getInternalPrefix()+ processRefToExternalDefinition(externalFile + subRef.get$ref(), RefFormat.RELATIVE));
return;
}
String $ref = subRef.get$ref();
String subRefExternalPath = getExternalPath(subRef.get$ref());

if (format.equals(RefFormat.RELATIVE) && !Objects.equals(subRefExternalPath, externalFile)) {
$ref = constructRef(subRef, externalFile);
subRef.set$ref($ref);
}else {
processRefToExternalDefinition($ref, format);
}
}

protected String constructRef(Model refProperty, String rootLocation) {
RefModel refModel = (RefModel) refProperty;
String ref = refModel.get$ref();
return join(rootLocation, ref);
}

public static String getExternalPath(String ref) {
if (ref == null) {
return null;
}
String[] elements = ref.split("#/");
String element = null;
for (int i = 0; i < elements.length; i++ ) {
if (elements[i].length() == 2){
element = elements[i];
}
}
return element;
}


public static String join(String source, String fragment) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@

public class SwaggerParserTest {


@Test
public void testIssueRelativeRefs3() {
String location = "swagger-reference-response.yaml";
Swagger swagger = new SwaggerParser().read(location, null, true);
assertNotNull(swagger);
}

@Test
public void testIssue1143() {
SwaggerDeserializationResult result = new SwaggerParser().readWithInfo("issue1143.json", null, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### Domains, a place to put your reusable components

responses:
GeneralError:
description: Occurs when something goes wrong
schema:
$ref: 'v2.yaml#/definitions/Test.Common.ErrorResponseMessage'


definitions:

Test.Common.DocUrl:
type: string
format: uri
description: The documentation related to this operation.
example: 'https://api-docs.test.com/#operation/EnableChannelCatalog'


Test.Common.ErrorResponseMessage:
type: object
required:
- errors
properties:
errors:
type: array
uniqueItems: false
items:
$ref: 'v2.yaml#/definitions/Test.Common.UserErrorMessage'

Test.Common.UserErrorMessage:
type: object
required:
- code
- message
properties:
docUrl:
$ref: 'v2.yaml#/definitions/Test.Common.DocUrl'
code:
type: string
description: the error code. The error code can be a pattern containing the argument's name
example: Here goes an example text
message:
type: string
description: The error message
example: |
There is already an importation in progress: 12345-XYZBN-00122-44444
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
swagger: '2.0'
info:
version: "2.0"
title: Marketplaces - Orders - Subscriptions
description: The order subscription management

contact:
email: [email protected]

license:
name: Test
url: http://www.test.com


security:
- api_key: []

consumes:
- application/json
produces:
- application/json


paths:

'/':
get:
tags:
- Subscriptions
operationId: GetSubscriptionList
summary: Get the subscription list
responses:
200:
$ref: 'folder/domains/Test/api.test.com/v2.yaml#/responses/GeneralError'
deprecated: false


definitions: {}

host: api.test.com
basePath: /v2/user/marketplaces/orders

0 comments on commit 1136de9

Please sign in to comment.