From 2d2834c209406f6d68487f63cc81e94541c973f5 Mon Sep 17 00:00:00 2001 From: George Dimitrov Date: Wed, 10 Mar 2021 16:52:41 +0200 Subject: [PATCH] Fixes #832 do not add definitions for excluded methods In some cases the method will be excluded from the swagger (because it misses httpMethod or Api annotation), but the return type of the method is added to the definitions as parseMethod() had side effect and modified the swagger.model object. Removed the side effect of parseMethod(). --- .../swagger/docgen/reader/JaxrsReader.java | 32 ++++++++++++------- .../com/wordnik/jaxrs/ParentResource.java | 14 ++++++++ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/github/kongchen/swagger/docgen/reader/JaxrsReader.java b/src/main/java/com/github/kongchen/swagger/docgen/reader/JaxrsReader.java index 5810323cb..2fec671de 100644 --- a/src/main/java/com/github/kongchen/swagger/docgen/reader/JaxrsReader.java +++ b/src/main/java/com/github/kongchen/swagger/docgen/reader/JaxrsReader.java @@ -12,8 +12,10 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import javax.ws.rs.Consumes; @@ -155,7 +157,8 @@ public Class annotationType() { String httpMethod = extractOperationMethod(apiOperation, method, SwaggerExtensions.chain()); - Operation operation = parseMethod(httpMethod, method); + final LinkedHashMap resultModels = new LinkedHashMap<>(); + final Operation operation = parseMethod(httpMethod, method, resultModels); updateOperationParameters(parentParameters, regexMap, operation); updateOperationProtocols(apiOperation, operation); @@ -178,9 +181,14 @@ public Class annotationType() { // can't continue without a valid http method httpMethod = (httpMethod == null) ? parentMethod : httpMethod; - updateTagsForOperation(operation, apiOperation); - updateOperation(apiConsumes, apiProduces, tags, securities, operation); - updatePath(operationPath, httpMethod, operation); + if (httpMethod != null) { + for (Entry m : resultModels.entrySet()) { + swagger.model(m.getKey(), m.getValue()); + } + updateTagsForOperation(operation, apiOperation); + updateOperation(apiConsumes, apiProduces, tags, securities, operation); + updatePath(operationPath, httpMethod, operation); + } } updateTagDescriptions(discoveredTags); } @@ -202,7 +210,7 @@ private List getFilteredMethods(Class cls) { /** * Returns true when the swagger object already contains a common parameter * with the same name and type as the passed parameter. - * + * * @param parameter The parameter to check. * @return true if the swagger object already contains a common parameter with the same name and type */ @@ -331,7 +339,7 @@ private String getPath(Path classLevelPath, Path methodLevelPath, String parentP } - public Operation parseMethod(String httpMethod, Method method) { + public Operation parseMethod(String httpMethod, Method method, LinkedHashMap resultModels) { int responseCode = 200; Operation operation = new Operation(); ApiOperation apiOperation = AnnotationUtils.findAnnotation(method, ApiOperation.class); @@ -392,6 +400,7 @@ public Operation parseMethod(String httpMethod, Method method) { if (responseClassType instanceof Class) { hasApiAnnotation = AnnotationUtils.findAnnotation((Class) responseClassType, Api.class) != null; } + if ((responseClassType != null) && !responseClassType.equals(Void.class) && !responseClassType.equals(void.class) @@ -417,20 +426,21 @@ public Operation parseMethod(String httpMethod, Method method) { .schema(p) .headers(defaultResponseHeaders)); } - for (String key : models.keySet()) { - Property responseProperty = RESPONSE_CONTAINER_CONVERTER.withResponseContainer(responseContainer, new RefProperty().asDefault(key)); - + for (Map.Entry entry : models.entrySet()) { + final String key = entry.getKey(); + Property responseProperty = RESPONSE_CONTAINER_CONVERTER + .withResponseContainer(responseContainer, new RefProperty().asDefault(key)); operation.response(responseCode, new Response() .description("successful operation") .schema(responseProperty) .headers(defaultResponseHeaders)); - swagger.model(key, models.get(key)); + resultModels.put(entry.getKey(), entry.getValue()); } } Map models = readAllModels(responseClassType); for (Map.Entry entry : models.entrySet()) { - swagger.model(entry.getKey(), entry.getValue()); + resultModels.put(entry.getKey(), entry.getValue()); } } diff --git a/src/test/java/com/wordnik/jaxrs/ParentResource.java b/src/test/java/com/wordnik/jaxrs/ParentResource.java index c7ac095db..faee45810 100644 --- a/src/test/java/com/wordnik/jaxrs/ParentResource.java +++ b/src/test/java/com/wordnik/jaxrs/ParentResource.java @@ -5,8 +5,12 @@ */ package com.wordnik.jaxrs; +import java.sql.SQLException; + import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; + +import javax.net.ssl.SSLException; import javax.ws.rs.Path; /** @@ -21,4 +25,14 @@ public class ParentResource { public SubResource getStudyResource() { return new SubResource(); } + + // this static method and return type should not be included in the swagger as there is no Path nor Api + public static SQLException getCauseSQLException(Throwable e) { + return null; + } + + // this method and return type should not be included in the swagger as there is no Path nor Api + public SSLException getCauseSSLException(Throwable e) { + return null; + } }