diff --git a/modules/swagger-parser/src/main/java/io/swagger/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser/src/main/java/io/swagger/parser/processors/ExternalRefProcessor.java index 7b9a655c96..c00f525cc5 100644 --- a/modules/swagger-parser/src/main/java/io/swagger/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser/src/main/java/io/swagger/parser/processors/ExternalRefProcessor.java @@ -87,7 +87,8 @@ public String processRefToExternalDefinition(String $ref, RefFormat refFormat) { if (allOfModel instanceof RefModel) { RefModel refModel = (RefModel) allOfModel; if (isAnExternalRefFormat(refModel.getRefFormat())) { - refModel.set$ref(processRefToExternalDefinition(refModel.get$ref(), refModel.getRefFormat())); + String joinedRef = join(file, refModel.get$ref()); + refModel.set$ref(processRefToExternalDefinition(joinedRef, refModel.getRefFormat())); } else { processRefToExternalDefinition(file + refModel.get$ref(), RefFormat.RELATIVE); } diff --git a/modules/swagger-parser/src/test/java/io/swagger/parser/SwaggerParserTest.java b/modules/swagger-parser/src/test/java/io/swagger/parser/SwaggerParserTest.java index 8aa4fe3bfd..7cf1d4c0e5 100644 --- a/modules/swagger-parser/src/test/java/io/swagger/parser/SwaggerParserTest.java +++ b/modules/swagger-parser/src/test/java/io/swagger/parser/SwaggerParserTest.java @@ -1020,6 +1020,12 @@ public void checkAllOfAreTaken() { } + @Test(description = "Issue #616 Relative references inside of 'allOf'") + public void checkAllOfWithRelativeReferencesAreFound() { + Swagger swagger = new SwaggerParser().read("src/test/resources/allOf-relative-file-references/parent.json"); + assertEquals(4, swagger.getDefinitions().size()); + } + @Test(description = "A string example should not be over quoted when parsing a yaml string") public void readingSpecStringShouldNotOverQuotingStringExample() throws Exception { SwaggerParser parser = new SwaggerParser(); diff --git a/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/fancy_pet.json b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/fancy_pet.json new file mode 100644 index 0000000000..d544a24f7c --- /dev/null +++ b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/fancy_pet.json @@ -0,0 +1,15 @@ +{ + "allOf": [ + { + "$ref": "./pet.json" + }, + { + "properties": { + "breed": { + "type": "string", + "example": "Labrador" + } + } + } + ] +} \ No newline at end of file diff --git a/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/pet.json b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/pet.json new file mode 100644 index 0000000000..2749445a85 --- /dev/null +++ b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/models/pet.json @@ -0,0 +1,13 @@ +{ + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string", + "example": "Cooper" + } + } +} \ No newline at end of file diff --git a/modules/swagger-parser/src/test/resources/allOf-relative-file-references/parent.json b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/parent.json new file mode 100644 index 0000000000..86d65c7472 --- /dev/null +++ b/modules/swagger-parser/src/test/resources/allOf-relative-file-references/parent.json @@ -0,0 +1,84 @@ +{ + "swagger": "2.0", + "info": { + "description": "Issue #616. This is a modified Petstore server where we have two pet definitions 1. pet and 2. fancy_pet which inherits pet using allOf." + }, + "host": "petstore.swagger.io", + "basePath": "/v3", + "paths": { + "/pet": { + "get": { + "operationId": "getPetById", + "parameters": [ + { + "name": "petId", + "in": "path", + "required": true, + "type": "integer", + "format": "int64" + } + ], + "responses": { + "200": { + "schema": { + "$ref": "./models/pet.json" + } + } + } + } + }, + "/fancy-pet": { + "get": { + "operationId": "getFancyPetById", + "parameters": [ + { + "name": "fancyPetId", + "in": "path", + "required": true, + "type": "integer", + "format": "int64" + } + ], + "responses": { + "200": { + "schema": { + "$ref": "./models/fancy_pet.json" + } + } + } + } + } + }, + "definitions": { + "DomesticPet": { + "allOf": [ + { + "$ref": "./models/pet.json" + }, + { + "properties": { + "description": { + "type": "string", + "example": "friendly" + } + } + } + ] + }, + "DancingPet": { + "allOf": [ + { + "$ref": "./models/fancy_pet.json" + }, + { + "properties": { + "style": { + "type": "string", + "example": "salsa" + } + } + } + ] + } + } +} \ No newline at end of file