Open stevecookform3 opened 7 years ago
This is a bug in all Java languages, I created #4718 for this to fix it in every languages. The fix is to use the baseType instead of the returnType for the response. Additionally, you also need a fix for checking for void baseTypes which I added in PR #4717 to AbstractJavaJAXRSServerCodegen.java.
So it is best to wait for #4717 to get merged, then we can fix it in Spring and in all other languages.
any updates?
@jfiala Just for understanding, you want to change the return type to be just Response
instead of Response<List<Pet>>
in this case? I'm not sure I approve raw type usage, though I suppose Java doesn't allow anything type-safe here. (Apart from throwing all the non-first responses as exceptions.)
The alternative here is to require uses to implement a controller advice class that correctly maps exceptions thrown in the controllers to the correct object.
https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
The generation of the @ApiResponse
annotations should be fixed though.
@ApiResponses(value = {
@ApiResponse(code = 200, message = "pet response", response = Pet.class),
@ApiResponse(code = 400, message = "unexpected error", response = Pet.class) }
I just tried version 2.3.1 of the codegen and this issue is still there. Is there an ETA for fixing? If not, is there an alternative swagger pattern for error responses that does generate api classes in spring that can be used directly without making fixes by hand?
Hi, I'm also struggling with this issue. What is the work around?
My generated ..Api.java has the response well defined.
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created", response = TheObject.class),
@ApiResponse(code = 400, message = "Bad Request", response = BadRequest.class) })
@RequestMapping(value = "/v1/objects",
produces = { "application/json" },
consumes = { "application/json" },
method = RequestMethod.POST)
ResponseEntity<TheObject> postObjects(@ApiParam(value = "" ) @Valid @RequestBody TheObject body
Is there any work around? I have changed the return to:
ResponseEntity<? extends java.lang.Object> postObjects(@ApiParam(value = "" ) @Valid @RequestBody TheObject body
Which is hard to check on git any changes made to the openapi spec.
Thanks!
Push - looks like this problem is still present. If not i would appreciate someone giving a proper implementation advice.
@wing328 could you give us an update please?
Hi all - we are running into exactly the same issue.
Unfortunately, the proposal from @JLLeitschuh also does not work as stated (afaik), as the method would need to throw an Exception (to be handled by the Controller's @ExceptionHandler
) which it is also not allowed to (the signature in the ...API class does not specify a "throws").
Possible solutions:
1) The proposal from @msilvestre to make the function return ResponseEntity<? extends java.lang.Object>
. As him, I don't think this is very nice.
2) add a generic throws Exception
to the method signature. This allows the implementation in the controller to throw Exceptions of any kind and we can handle and transform them into proper responses with @ExceptionHandler
s --> Also not very nice
3) "Full blown": Auto generate Exceptions for the different return paths (all 4xx and 5xx?), allow the method to throw them, autogenerate @ExceptionHandler
s for these Exceptions on the generated Controller.
I envision option 3 for the example above from @msilvestre to look something like this (obviously distributed across adequate files):
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created", response = TheObject.class),
@ApiResponse(code = 400, message = "Bad Request", response = BadRequest.class) })
@RequestMapping(value = "/v1/objects",
produces = { "application/json" },
consumes = { "application/json" },
method = RequestMethod.POST)
ResponseEntity<TheObject> postObjects(@ApiParam(value = "" ) @Valid @RequestBody TheObject body throws BadRequestException
public class BadRequestException extends Exception {}
@Controller
class ........ {
@ResponseStatus(code=HttpStatus.BAD_REQUEST, reason="Bad Request")
@ExceptionHandler(BadRequestException.class)
public BadRequest handleBadRequest () { // Note that this returns the BadRequest class
return null;
}
}
I am reasonably new to all of this, so I am not sure I have the full picture. I am happy to contribute to a solution if there is some vetting on my thinking and you guys agree / lead me to the right direction.
Hi,
I´m facing the same issue. Is there any fix?
Thanks.
I am facing this error too
Any chance for fix?
Description
If you have a schema that has multiple responses with different models, the generated controller code only supports the first model.
eg. using the simple pet example (and changing the 'default' response to '400'):
Generates the following interface:
Note that the error model isnt referenced anywhere in the generated code, and the response is set to
ResponseEntity<List<Pet>>
rather than the more generic Reponse class.Swagger-codegen version
Latest head. Also the current version on http://editor.swagger.io/#/
Command line used for generation
generate -v -i ./swagger.yaml -l spring -o .