From 587cc981d03abb561ad92cda31c157943fc54dc2 Mon Sep 17 00:00:00 2001 From: Daan Jonkers Date: Wed, 2 Nov 2022 20:42:26 +0100 Subject: [PATCH] Allow ApiResponse.reference to be set as Schema.$ref in json and yaml --- pom.xml | 2 +- .../swagger/docgen/reader/AbstractReader.java | 16 ++-- .../swagger/docgen/reader/ModelReference.java | 89 +++++++++++++++++++ .../java/com/wordnik/jaxrs/PetResource.java | 2 +- .../swagger-enhanced-operation-id.json | 5 +- .../expectedOutput/swagger-externalDocs.json | 5 +- .../expectedOutput/swagger-swaggerreader.json | 5 +- .../expectedOutput/swagger-swaggerreader.yaml | 2 + .../swagger-with-converter.json | 5 +- .../swagger-with-converter.yaml | 2 + .../resources/expectedOutput/swagger.json | 5 +- .../resources/expectedOutput/swagger.yaml | 2 + 12 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/github/kongchen/swagger/docgen/reader/ModelReference.java diff --git a/pom.xml b/pom.xml index 5672e7b40..f5bc161d1 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ 1.6 1.0 1.7.25 - 1.2.61 + 1.5.32 diff --git a/src/main/java/com/github/kongchen/swagger/docgen/reader/AbstractReader.java b/src/main/java/com/github/kongchen/swagger/docgen/reader/AbstractReader.java index 821f6c51e..99f3a22e8 100644 --- a/src/main/java/com/github/kongchen/swagger/docgen/reader/AbstractReader.java +++ b/src/main/java/com/github/kongchen/swagger/docgen/reader/AbstractReader.java @@ -42,7 +42,7 @@ public abstract class AbstractReader { protected List responseMessageOverrides; protected String operationIdFormat; - + /** * Supported parameters: {{packageName}}, {{className}}, {{methodName}}, {{httpMethod}} * Suggested default value is: "{{className}}_{{methodName}}_{{httpMethod}}" @@ -386,7 +386,11 @@ protected void updateApiResponse(Operation operation, ApiResponses responseAnnot .description(apiResponse.message()) .headers(responseHeaders); - if (responseClass.equals(Void.class)) { + if (apiResponse.reference() != null && !"".equals(apiResponse.reference()) && responseClass.equals(Void.class) ) { + ModelReference model = new ModelReference(); + model.setReference(apiResponse.reference()); + response.setResponseSchema(model); + } else if (responseClass.equals(Void.class)) { if (operation.getResponses() != null) { Response apiOperationResponse = operation.getResponses().get(String.valueOf(apiResponse.code())); if (apiOperationResponse != null) { @@ -522,22 +526,22 @@ void processOperationDecorator(Operation operation, Method method) { extension.decorateOperation(operation, method, chain); } } - + protected String getOperationId(Method method, String httpMethod) { if (this.operationIdFormat == null) { this.operationIdFormat = OPERATION_ID_FORMAT_DEFAULT; } - + String packageName = method.getDeclaringClass().getPackage().getName(); String className = method.getDeclaringClass().getSimpleName(); String methodName = method.getName(); - + StrBuilder sb = new StrBuilder(this.operationIdFormat); sb.replaceAll("{{packageName}}", packageName); sb.replaceAll("{{className}}", className); sb.replaceAll("{{methodName}}", methodName); sb.replaceAll("{{httpMethod}}", httpMethod); - + return sb.toString(); } diff --git a/src/main/java/com/github/kongchen/swagger/docgen/reader/ModelReference.java b/src/main/java/com/github/kongchen/swagger/docgen/reader/ModelReference.java new file mode 100644 index 000000000..e00e0389e --- /dev/null +++ b/src/main/java/com/github/kongchen/swagger/docgen/reader/ModelReference.java @@ -0,0 +1,89 @@ +package com.github.kongchen.swagger.docgen.reader; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.models.ExternalDocs; +import io.swagger.models.Model; +import io.swagger.models.properties.Property; + +import java.util.Map; + +public class ModelReference implements Model { + @JsonIgnore + @Override + public String getTitle() { + return null; + } + + @Override + public void setTitle(String s) { + + } + + + @JsonIgnore + @Override + public String getDescription() { + return null; + } + + @Override + public void setDescription(String s) { + + } + + + @JsonIgnore + @Override + public Map getProperties() { + return null; + } + + @Override + public void setProperties(Map map) { + + } + + + @JsonIgnore + @Override + public Object getExample() { + return null; + } + + @Override + public void setExample(Object o) { + + } + + + @JsonIgnore + @Override + public ExternalDocs getExternalDocs() { + return null; + } + + private String reference; + @JsonProperty("$ref") + @Override + public String getReference() { + return reference; + } + + @Override + public void setReference(String s) { + reference = s; + } + + @Override + public Object clone() { + return null; + } + + + @JsonIgnore + @Override + public Map getVendorExtensions() { + return null; + } +} diff --git a/src/test/java/com/wordnik/jaxrs/PetResource.java b/src/test/java/com/wordnik/jaxrs/PetResource.java index cdfeb80d3..b2a8c6dc7 100644 --- a/src/test/java/com/wordnik/jaxrs/PetResource.java +++ b/src/test/java/com/wordnik/jaxrs/PetResource.java @@ -107,7 +107,7 @@ public Response getPetsById( @DELETE @Path("/{petId}") @ApiOperation(value = "Deletes a pet", nickname = "removePet") - @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid pet value")}) + @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid pet value", reference = "https://pet.error.com")}) public Response deletePet( @ApiParam() @HeaderParam("api_key") String apiKey, @ApiParam(value = "Pet id to delete", required = true) @PathParam("petId") Long petId) { diff --git a/src/test/resources/expectedOutput/swagger-enhanced-operation-id.json b/src/test/resources/expectedOutput/swagger-enhanced-operation-id.json index 6664092e6..36798d70c 100644 --- a/src/test/resources/expectedOutput/swagger-enhanced-operation-id.json +++ b/src/test/resources/expectedOutput/swagger-enhanced-operation-id.json @@ -721,7 +721,10 @@ } ], "responses" : { "400" : { - "description" : "Invalid pet value" + "description" : "Invalid pet value", + "schema" : { + "$ref" : "https://pet.error.com" + } } }, "security" : [ { diff --git a/src/test/resources/expectedOutput/swagger-externalDocs.json b/src/test/resources/expectedOutput/swagger-externalDocs.json index 2fab4c42a..5050380f6 100644 --- a/src/test/resources/expectedOutput/swagger-externalDocs.json +++ b/src/test/resources/expectedOutput/swagger-externalDocs.json @@ -993,7 +993,10 @@ ], "responses": { "400": { - "description": "Invalid pet value" + "description": "Invalid pet value", + "schema" : { + "$ref" : "https://pet.error.com" + } } }, "security": [ diff --git a/src/test/resources/expectedOutput/swagger-swaggerreader.json b/src/test/resources/expectedOutput/swagger-swaggerreader.json index 146cb79e0..2247b4d08 100644 --- a/src/test/resources/expectedOutput/swagger-swaggerreader.json +++ b/src/test/resources/expectedOutput/swagger-swaggerreader.json @@ -698,7 +698,10 @@ } ], "responses" : { "400" : { - "description" : "Invalid pet value" + "description" : "Invalid pet value", + "schema" : { + "$ref" : "https://pet.error.com" + } } }, "security" : [ { diff --git a/src/test/resources/expectedOutput/swagger-swaggerreader.yaml b/src/test/resources/expectedOutput/swagger-swaggerreader.yaml index 2f3cb92c3..7bec6c20b 100644 --- a/src/test/resources/expectedOutput/swagger-swaggerreader.yaml +++ b/src/test/resources/expectedOutput/swagger-swaggerreader.yaml @@ -623,6 +623,8 @@ paths: responses: 400: description: "Invalid pet value" + schema: + $ref: "https://pet.error.com" security: - petstore_auth: - "write:pets" diff --git a/src/test/resources/expectedOutput/swagger-with-converter.json b/src/test/resources/expectedOutput/swagger-with-converter.json index cdac15a72..9b3881b6c 100644 --- a/src/test/resources/expectedOutput/swagger-with-converter.json +++ b/src/test/resources/expectedOutput/swagger-with-converter.json @@ -991,7 +991,10 @@ ], "responses": { "400": { - "description": "Invalid pet value" + "description": "Invalid pet value", + "schema" : { + "$ref" : "https://pet.error.com" + } } }, "security": [ diff --git a/src/test/resources/expectedOutput/swagger-with-converter.yaml b/src/test/resources/expectedOutput/swagger-with-converter.yaml index fc7c8727d..c5a92cae8 100644 --- a/src/test/resources/expectedOutput/swagger-with-converter.yaml +++ b/src/test/resources/expectedOutput/swagger-with-converter.yaml @@ -698,6 +698,8 @@ paths: responses: 400: description: "Invalid pet value" + schema: + $ref: "https://pet.error.com" security: - petstore_auth: - "write:pets" diff --git a/src/test/resources/expectedOutput/swagger.json b/src/test/resources/expectedOutput/swagger.json index 3e9675b70..d90ce825e 100644 --- a/src/test/resources/expectedOutput/swagger.json +++ b/src/test/resources/expectedOutput/swagger.json @@ -993,7 +993,10 @@ ], "responses": { "400": { - "description": "Invalid pet value" + "description": "Invalid pet value", + "schema" : { + "$ref" : "https://pet.error.com" + } } }, "security": [ diff --git a/src/test/resources/expectedOutput/swagger.yaml b/src/test/resources/expectedOutput/swagger.yaml index b45b2898e..b7beabcb0 100644 --- a/src/test/resources/expectedOutput/swagger.yaml +++ b/src/test/resources/expectedOutput/swagger.yaml @@ -698,6 +698,8 @@ paths: responses: 400: description: "Invalid pet value" + schema: + $ref: "https://pet.error.com" security: - petstore_auth: - "write:pets"