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

Support for adding additional models for request or response that are not inferred from operations #735

Closed
cabbonizio opened this issue May 8, 2015 · 5 comments

Comments

@cabbonizio
Copy link

Hi, this is just a question about how to configure a the ModelRef object within the ResponseMessage object. Essentially, I would like to default the ResponseMessages globally within startup of Sprint Boot application such as HTTP 400, 404, and 500. I did figure out how to do this which is to use the
globalResponseMessage() method of the Docket object like such:

@bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2).select().paths(userOnlyEndpoints()).build()
.pathMapping("/rest/").globalResponseMessage(RequestMethod.GET, getDefaultResponseMessages()).apiInfo(apiInfo());
}

private List getDefaultResponseMessages() {
List defaultResponseMessages = Lists.newArrayList();
defaultResponseMessages.add(new ResponseMessage(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase(), new ModelRef("")));
defaultResponseMessages.add(new ResponseMessage(HttpStatus.NOT_FOUND.value(), HttpStatus.NOT_FOUND.getReasonPhrase(), new ModelRef("")));
defaultResponseMessages.add(new ResponseMessage(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), new ModelRef("")));
return defaultResponseMessages;
}

It is working defaulting each of the GET methods in my controller with those HTTP status codes, but I want to be able to specify an "error" response object which is different than the response if the return code was a 200. Where I'm getting hung up is how to configure ModelRef() to specify the error object (Error.class). I can actually do this within each controller method using Swaggers @ApiResponses({ @ApiResponse(code = 404, message = "Not Found", response=Error.class) }) but I would prefer to do this in my Spring Boot config class (Application.java) and just trying to determine how do this with the ResponseMessage object.

Any help would be appreciated thanks very much. This is by far one of the best and easiest ways to incorporate Swagger into a Spring (especially Boot) application. Our developers are all having fun using this.

Chris

@dilipkrish
Copy link
Member

@cabbonizio Thats a use case that we haven't thought through completely :-o! thanks for reporting it. The good news is that there work around for this

  • annotate any ONE method of your choosing with and @ApiResponses(@ApiResponse(...) annotation with the error model. Don't have to annotate all the operations. This is just so that the the Error model is read and available in the models section.
  • Do what you're doing currently, and when you create the model ref create it with the simple name of the class; new ModelRef("Error") in your case.

@cabbonizio
Copy link
Author

Wow, you're right that works perfectly. Currently the only extra piece of code is just having to annotate one of my controller methods and the others can remain without it. This workaround is not that painful at all to be honest. I really like being able to globally use the globalResponseMessage() method I guess in the future it would be a nice to have to specify the model class within that method this way it's less configuration in the controller. In any event, this is working great, doesn't make things really messy in the end I like the results.

Have a great weekend and I look forward to any enhancements to this area!

@dilipkrish
Copy link
Member

Reopening so that it can be fixed :)

@dilipkrish dilipkrish reopened this May 8, 2015
@dilipkrish dilipkrish added this to the 2.1 milestone May 8, 2015
@cabbonizio
Copy link
Author

Ah...... my bad @dilipkrish thanks for reopening. I will be glad to test this for you guys when something is ready.

@dilipkrish dilipkrish modified the milestones: 2.3.0, 2.2.0 Aug 14, 2015
@dilipkrish dilipkrish modified the milestones: 2.4.0, 2.3.0 Dec 2, 2015
@dilipkrish dilipkrish changed the title Configuring ModelRef for defaulting ResponseMessages in Spring Boot app Support for adding additional models for request or response that are not inferred from operations Jan 19, 2016
@dilipkrish
Copy link
Member

Fixed via f748a6b

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

No branches or pull requests

2 participants