Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Unwanted response with http status 200 #189

Open
stengvac opened this issue Jun 23, 2020 · 4 comments
Open

Unwanted response with http status 200 #189

stengvac opened this issue Jun 23, 2020 · 4 comments

Comments

@stengvac
Copy link

stengvac commented Jun 23, 2020

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"
@gigaSproule
Copy link
Owner

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.

@gigaSproule
Copy link
Owner

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).

@ghost
Copy link

ghost commented Dec 2, 2020

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.

@stengvac
Copy link
Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants