moha
moha

Reputation: 599

Change the parameters in body request of a method in Swagger

I've been working on a project and try to document it using Swagger-2.0. In this project due to some security issues, every method in controllers that has an object as a return type should change the return object to a completely flat object. In order to do this, we use some custom annotations above each method and the controller itself transform the object to the return type. By which I mean, the Object which is return by the method is not the same Object that pass to the browser. And also we have a same thing for the input parameters of methods in controller. It means that users have to send an Object for the methods which is different with the input parameter of the method. There is a sample method as an example below.

@RequestView(InputParamView.class)
@ResponseView(ReturnObjectView.class)
@RequestMapping(value = "/someMethod", method = RequestMethod.POST)
public ReturnObject someMethod(@RequestBody InputParam inputParam) {
      //do some thing
}

The problem is how to configure the Swagger to recognize InputParamView and ReturnObjectView as the input parameter and return type of this method. We do this for the return type of the method by using the ApiOperation annotation above each method just like below.

@ApiOperation(value = "/someMethod", response = ReturnObjectView.class)

Unfortunately, I have found any way to do the same thing for the input parameters yet. I will be wondering if someone help me with this issue with a proper example.

Upvotes: 0

Views: 1749

Answers (1)

Dilip Krishnan
Dilip Krishnan

Reputation: 5487

If you're objects usually have a one to one mapping for example, InputParam => InputParamView and ReturnObject => ReturnObjectView then its very simple to solve this problem. All you need to do is to set up alternate type rules in your docket.

docket
    .directModelSubstitute(InputParam.class, InputParamView.class)
    .directModelSubstitute(ReturnObject.class, ReturnObjectView.class)

Now if you have a one to many mapping like InputParam => [Method1InputParamView, Method2InputParamView] then its a little bit more involved and it ties into supporting @JsonView #601.

NOTE: Answer reproduced from the github issue. Please follow the github thread for the more involved discussion on options.

Upvotes: 1

Related Questions