Skip to content

Commit 4ca50d4

Browse files
authored
Merge pull request #3096 from swagger-api/ticket-3082
ref #3082 - updated returnType handling
2 parents b3aa8d4 + 90ebfd5 commit 4ca50d4

File tree

7 files changed

+149
-5
lines changed

7 files changed

+149
-5
lines changed

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ public OpenAPI read(Class<?> cls,
413413
continue;
414414
} else if (StringUtils.isBlank(httpMethod) && subResource != null) {
415415
Type returnType = method.getGenericReturnType();
416+
if (annotatedMethod != null && annotatedMethod.getType() != null) {
417+
returnType = annotatedMethod.getType();
418+
}
419+
416420
if (shouldIgnoreClass(returnType.getTypeName()) && !returnType.equals(subResource)) {
417421
continue;
418422
}
@@ -458,8 +462,8 @@ public OpenAPI read(Class<?> cls,
458462
parentRequestBody,
459463
parentResponses,
460464
jsonViewAnnotation,
461-
classResponses
462-
);
465+
classResponses,
466+
annotatedMethod);
463467
if (operation != null) {
464468

465469
List<Parameter> operationParameters = new ArrayList<>();
@@ -749,6 +753,7 @@ public Operation parseMethod(
749753
null,
750754
null,
751755
jsonViewAnnotation,
756+
null,
752757
null);
753758
}
754759

@@ -785,7 +790,46 @@ public Operation parseMethod(
785790
parentRequestBody,
786791
parentResponses,
787792
jsonViewAnnotation,
788-
classResponses);
793+
classResponses,
794+
null);
795+
}
796+
797+
public Operation parseMethod(
798+
Method method,
799+
List<Parameter> globalParameters,
800+
Produces methodProduces,
801+
Produces classProduces,
802+
Consumes methodConsumes,
803+
Consumes classConsumes,
804+
List<SecurityRequirement> classSecurityRequirements,
805+
Optional<io.swagger.v3.oas.models.ExternalDocumentation> classExternalDocs,
806+
Set<String> classTags,
807+
List<io.swagger.v3.oas.models.servers.Server> classServers,
808+
boolean isSubresource,
809+
RequestBody parentRequestBody,
810+
ApiResponses parentResponses,
811+
JsonView jsonViewAnnotation,
812+
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses,
813+
AnnotatedMethod annotatedMethod) {
814+
JavaType classType = TypeFactory.defaultInstance().constructType(method.getDeclaringClass());
815+
return parseMethod(
816+
classType.getClass(),
817+
method,
818+
globalParameters,
819+
methodProduces,
820+
classProduces,
821+
methodConsumes,
822+
classConsumes,
823+
classSecurityRequirements,
824+
classExternalDocs,
825+
classTags,
826+
classServers,
827+
isSubresource,
828+
parentRequestBody,
829+
parentResponses,
830+
jsonViewAnnotation,
831+
classResponses,
832+
annotatedMethod);
789833
}
790834

791835
private Operation parseMethod(
@@ -804,7 +848,8 @@ private Operation parseMethod(
804848
RequestBody parentRequestBody,
805849
ApiResponses parentResponses,
806850
JsonView jsonViewAnnotation,
807-
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses) {
851+
io.swagger.v3.oas.annotations.responses.ApiResponse[] classResponses,
852+
AnnotatedMethod annotatedMethod) {
808853
Operation operation = new Operation();
809854

810855
io.swagger.v3.oas.annotations.Operation apiOperation = ReflectionUtils.getAnnotation(method, io.swagger.v3.oas.annotations.Operation.class);
@@ -962,6 +1007,11 @@ private Operation parseMethod(
9621007

9631008
// handle return type, add as response in case.
9641009
Type returnType = method.getGenericReturnType();
1010+
1011+
if (annotatedMethod != null && annotatedMethod.getType() != null) {
1012+
returnType = annotatedMethod.getType();
1013+
}
1014+
9651015
final Class<?> subResource = getSubResourceWithJaxRsSubresourceLocatorSpecs(method);
9661016
if (!shouldIgnoreClass(returnType.getTypeName()) && !returnType.equals(subResource)) {
9671017
ResolvedSchema resolvedSchema = ModelConverters.getInstance().resolveAsResolvedSchema(new AnnotatedType(returnType).resolveAsRef(true).jsonViewAnnotation(jsonViewAnnotation));
@@ -1016,8 +1066,9 @@ private boolean shouldIgnoreClass(String className) {
10161066
return true;
10171067
}
10181068
boolean ignore = false;
1019-
ignore = ignore || className.startsWith("javax.ws.rs.");
1069+
ignore = ignore || className.replace("[simple type, class ", "").startsWith("javax.ws.rs.");
10201070
ignore = ignore || className.equalsIgnoreCase("void");
1071+
ignore = ignore || className.equalsIgnoreCase("[simple type, class void]");
10211072
return ignore;
10221073
}
10231074

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import io.swagger.v3.jaxrs2.resources.extensions.OperationExtensionsResource;
6262
import io.swagger.v3.jaxrs2.resources.extensions.ParameterExtensionsResource;
6363
import io.swagger.v3.jaxrs2.resources.extensions.RequestBodyExtensionsResource;
64+
import io.swagger.v3.jaxrs2.resources.rs.ProcessTokenRestService;
6465
import io.swagger.v3.oas.annotations.enums.ParameterIn;
6566
import io.swagger.v3.oas.models.Components;
6667
import io.swagger.v3.oas.models.ExternalDocumentation;
@@ -1970,4 +1971,36 @@ public void testTicket3029() {
19701971
" example: 1\n";
19711972
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
19721973
}
1974+
1975+
@Test(description = "response generic subclass")
1976+
public void testTicket3082() {
1977+
Reader reader = new Reader(new OpenAPI());
1978+
OpenAPI openAPI = reader.read(ProcessTokenRestService.class);
1979+
1980+
String yaml = "openapi: 3.0.1\n" +
1981+
"paths:\n" +
1982+
" /token:\n" +
1983+
" post:\n" +
1984+
" operationId: create\n" +
1985+
" requestBody:\n" +
1986+
" content:\n" +
1987+
" application/json:\n" +
1988+
" schema:\n" +
1989+
" $ref: '#/components/schemas/ProcessTokenDTO'\n" +
1990+
" responses:\n" +
1991+
" default:\n" +
1992+
" description: default response\n" +
1993+
" content:\n" +
1994+
" application/json:\n" +
1995+
" schema:\n" +
1996+
" $ref: '#/components/schemas/ProcessTokenDTO'\n" +
1997+
"components:\n" +
1998+
" schemas:\n" +
1999+
" ProcessTokenDTO:\n" +
2000+
" type: object\n" +
2001+
" properties:\n" +
2002+
" guid:\n" +
2003+
" type: string\n";
2004+
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
2005+
}
19732006
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.swagger.v3.jaxrs2.resources.rs;
2+
3+
public abstract class AbstractEntityRestService<DTO extends PersistentDTO> implements EntityRestService<DTO> {
4+
5+
public DTO create(DTO object) throws Exception {
6+
return null;
7+
}
8+
9+
}
10+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.swagger.v3.jaxrs2.resources.rs;
2+
3+
import javax.ws.rs.POST;
4+
import javax.ws.rs.Path;
5+
6+
public interface EntityRestService<DTO> {
7+
8+
@POST
9+
@Path("/")
10+
public DTO create(DTO object) throws Exception;
11+
12+
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.swagger.v3.jaxrs2.resources.rs;
2+
3+
public class PersistentDTO {
4+
5+
private String guid;
6+
7+
public void setGuid(String guid) {
8+
this.guid = guid;
9+
}
10+
11+
public String getGuid() {
12+
return guid;
13+
}
14+
15+
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.swagger.v3.jaxrs2.resources.rs;
2+
3+
public class ProcessTokenDTO extends PersistentDTO {
4+
5+
private String name;
6+
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.swagger.v3.jaxrs2.resources.rs;
2+
3+
import javax.ws.rs.Consumes;
4+
import javax.ws.rs.Path;
5+
import javax.ws.rs.Produces;
6+
import javax.ws.rs.core.MediaType;
7+
8+
@Path("token")
9+
@Consumes(MediaType.APPLICATION_JSON)
10+
@Produces(MediaType.APPLICATION_JSON)
11+
public class ProcessTokenRestService extends AbstractEntityRestService<ProcessTokenDTO> {
12+
13+
14+
}

0 commit comments

Comments
 (0)