Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response code in Response Entity ignored #822

Closed
jfearon opened this issue Jun 30, 2015 · 6 comments

Comments

@jfearon
Copy link
Contributor

commented Jun 30, 2015

I've setup a endpoint that takes a POST, and returns a 201. If you set this up as:

@ApiOperation(value = "Create a new thing",
        notes = "Insert a new thing into the system"
)
@ApiResponses(value = {
        @ApiResponse(code = 400, message = "Thing failed validation", response = ValidationException.class)})
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Thing> newThing(@RequestBody Thing thing) {

    Thing insertedThing = thingService.insertThing(thing);
    return new ResponseEntity<Thing>(thing, HttpStatus.CREATED);

}

Then this generates as returning a 200 status code:

"responses":{"200":{"description":"OK","schema":{"$ref":"#/definitions/Thing"}}

By contrast this:

@ApiOperation(value = "Create a new thing",
        notes = "Insert a new thing into the system"
)
@ApiResponses(value = {
        @ApiResponse(code = 400, message = "Thing failed validation", response = ValidationException.class)})
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value= HttpStatus.CREATED)
public Thing newThing(@RequestBody Thing thing) {

    Thing insertedThing = thingService.insertThing(thing);
    return insertedThing;

}

Seems to work fine. Should the ResponseEntity parameter be picked up?

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Jun 30, 2015

A hybrid of what you have should work

@ApiOperation(value = "Create a new thing",
        notes = "Insert a new thing into the system"
)
@ApiResponses(value = {
         @ApiResponse(code = 201, message = "Thing was created "), //<----this and/or
        @ApiResponse(code = 400, message = "Thing failed validation", response = ValidationException.class)})
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value= HttpStatus.CREATED) // <---- and/or this
public ResponseEntity<Thing> newThing(@RequestBody Thing thing) {

    Thing insertedThing = thingService.insertThing(thing);
    return new ResponseEntity<Thing>(thing, HttpStatus.CREATED);

}
@jfearon

This comment has been minimized.

Copy link
Contributor Author

commented Jun 30, 2015

Ah, both of those do work, though it seems a shame to have to put the status in twice. I experimented with the code parameter on ApiOperation too, but that also seems to be ignored.

Anyway thanks for the quick response - feel free to close, sorry for not realising I could have the annotation and the ResponseEntity....

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Jun 30, 2015

put the status in twice

where? It's either in the ResponseStatus or in the ApiResponse. If you're talking about the one in your method body, well that's implementation details not visible to the library.

@davidnewcomb

This comment has been minimized.

Copy link
Contributor

commented Sep 19, 2016

This report is pointed to from "Answers to common questions and problems -> Why does springfox ignore the http status code in the return value of a controller method?" but it doesn't answer the question it only confirms that the code is ignored.
I don't understand why this isn't a bug. The user is programmatically setting the response code and the annotation (or lack of annotation) is modifying it.

dilipkrish added a commit that referenced this issue Sep 25, 2016

@dilipkrish

This comment has been minimized.

Copy link
Member

commented Sep 25, 2016

@davidnewcomb Iike I mentioned, this library works by using annotations and reflection. Its not able to introspect method bodies and examine implementation details.

@stefan-huettemann

This comment has been minimized.

Copy link

commented Mar 17, 2017

I am running on spring-boot 1.5.1 and springfox-swagger2:2.6.1.

I would like to pick up the question from @davidnewcomb.

I too see that there is a problem with the attribute code in the annotation @ApiOperation.

According to the JavaDoc the attribute code should reflect the HTTP return code of the operation annotated by @ApiOperation. The default is said to be 200.

Therefore annotating a (RestController) method with @ApiOperation(code = 201)
should change the swagger-ui line "Response Class (Status 200)" to something like "Response Class (Status 201)".

But it does not. I would call this a bug :)

Note: yes you can additionally add the 201 response code with @ApiResponse but that is not the same as changing the default code on @ApiOperation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.