swagger-gradle-plugin
swagger-gradle-plugin copied to clipboard
Unwanted response with http status 200
Hello,
thx for nice swaggergen plugin.
I run into problem with included HTTP 200 status response when ApiResponses is used. Desired result is to have response with statuses 201 and 422 which are specified in annotation, but generated swagger contains 200, 201, 422.
Using: Kotlin, Spring, plugin version 1.0.8, gradle 6.5
Sample:
@RestController
@RequestMapping("/somepath")
class DocGenController {
@ApiResponses(
value = [
ApiResponse(code = 201, message = "Success", response = SomeResponse::class),
ApiResponse(code = 422, message = "Business errors", response = SomeErrorTO::class)
]
)
@PostMapping("/someMethod")
@ResponseStatus(HttpStatus.CREATED) //this one is ignored when ApiResponses are present
fun someMethod(): SomeResponse = TODO()
}
data class SomeErrorTO(
val errorCode: String
)
data class SomeResponse(
val token: String
)
Either please do not ignore @ResponseStatus(HttpStatus.CREATED)
when @ApiResponses
is present or take responses solely from ApiResponses.
Problem seems to be https://github.com/gigaSproule/swagger-gradle-plugin/blob/master/plugin/src/main/groovy/com/benjaminsproule/swagger/gradleplugin/reader/SpringMvcApiReader.groovy
ApiResponses responseAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, ApiResponses)
if (responseAnnotation != null) {
updateApiResponse(operation, responseAnnotation, jsonView)
} else {
ResponseStatus responseStatus = AnnotatedElementUtils.findMergedAnnotation(method, ResponseStatus)
if (responseStatus != null) {
operation.response(responseStatus.value().value(), new Response().description(responseStatus.reason()))
}
}
generated swagger
---
swagger: "2.0"
info:
version: "DRAFT"
title: "some title"
tags: []
paths:
/somepath/someMethod:
post:
operationId: "someMethod"
parameters: []
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/SomeResponse"
201:
description: "Success"
schema:
$ref: "#/definitions/SomeResponse"
422:
description: "Business errors"
schema:
$ref: "#/definitions/SomeErrorTO"
definitions:
SomeErrorTO:
type: "object"
required:
- "errorCode"
properties:
errorCode:
type: "string"
SomeResponse:
type: "object"
required:
- "token"
properties:
token:
type: "string"
Just created a test for this locally and I'm getting the 200 state as well. However, in the test, it's not producing the schema for the 201 and 422 responses, just the 200.
So the problem is that we create an Operation
object in parseMethod
in both JAXRS and Spring MVC readers, with the status code of 200 and use that everywhere. Unfortunately, it's been nearly 3 years since that logic was put in and I wasn't the one to do it, so it's going to take a while to understand that. I also have extremely little time these days (I'm lucky to get half an hour in a day), so I'm more than happy to review a PR if you ever have time to take a look at it (but I totally understand if you don't).
I have the same problem.
Every one of my APIs that should return '201 Created' and a JSON response body is showing both 200 and 201:
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/SomeResponse"
201:
description: "Created"
schema:
$ref: "#/definitions/SomeResponse"
Using version 1.0.12 of the plugin with Spring MVC and Groovy.
If u desire to rewrite project to kotlin then i can help, but otherwise I am not much familliar with groovy so I dont know if i can help much.