wkennedy / swagger4spring-web

Swagger support for Spring MVC
89 stars 46 forks source link

'required' not being added to params #64

Open jhasenauer opened 10 years ago

jhasenauer commented 10 years ago

I am using:

My controllers get parsed correctly when simply using normal Spring annotations and swagger's @ApiOperation. However, I run into an issue when also using swagger's @Api annotation to add a description to my controller. The issue is that my required parameters in the controller aren't labeled as required anymore. I verify this by looking at the JSON that is produced in the resource list.

JSON snippet showing GET parameters for controller when using @Api as well as @ApiOperation:

...
"parameters": [{
    "name": "id",
    "description": "Subscription Id",
    "defaultValue": null,
    "required": false,
    "allowMultiple": false,
    "dataType": "string",
    "allowableValues": null,
    "paramType": "query",
    "paramAccess": null
},
{
    "name": "callback",
    "description": "Padding prefix for JSONP",
    "defaultValue": null,
    "required": false,
    "allowMultiple": false,
    "dataType": "string",
    "allowableValues": null,
    "paramType": "query",
    "paramAccess": null
}],
...

Here is the code for my controller:

@Controller
@RequestMapping(value = "/tag")
@Api(value="tag", description="Tag Controller")
public class TagController extends BasicController {

    @Autowired
    SubscriptionTagService subscriptionTagService;

    @RequestMapping(method=RequestMethod.POST)
    @ApiOperation(value = "Add a tag", 
                  notes="Add a tag to a subscrition")
    public void addTag(@ApiParam(value="Subscription Id") @RequestParam(value="id") String subscriptionId,
                       @ApiParam(value="Tag name") @RequestParam(value="tag") String tagName,
                       @ApiParam(value="Padding prefix for JSONP") @RequestParam(value="callback", required=false) String callback,
                       HttpServletRequest request) {
        String username = getIdFromRequest(request);
        subscriptionTagService.add(tagName, username, subscriptionId);
    }

    @RequestMapping(method=RequestMethod.GET,
                    produces=MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "Get subscription tags", 
                  notes = "Gets the tags of a user's subscription",
                  response = Tag.class)
    public @ResponseBody List getTags(@ApiParam(value="Subscription Id") @RequestParam(value="id") String subscriptionId,
                                           @ApiParam(value="Padding prefix for JSONP") @RequestParam(value="callback", required=false) String callback,
                                           HttpServletRequest request) {

        String username = getIdFromRequest(request);
        return subscriptionTagService.getSubscriptionTags(subscriptionId, username);
    }   
}

When I comment out @Api(value="tag", description="Tag Controller") the required parameters are correctly labeled.

Why would @Api be causing all parameters to have required = "false"? I have even tried to explicitly mark subscriptionId as required, but that doesn't have any effect.