Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use my own annotation for Swagger?

How can I use my own annotation for building swagger ui page. For example I defined annotation and use it:

    @PUT
    @MyOwnAnnotationForAdditionalPropInSwagger(value = "Some text")
    @Path( "/{carId}" )
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
        value = "Updates car info"
    )
    public Response patchItem(@ApiParam(value = "Fields to update") Car item) {
            /*some code*/
    }

After that probably I should extend some class from swagger-core and specify to scan my annotation (@MyOwnAnnotationForAdditionalPropInSwagger).

As result I want to see additional column in swagger ui with my text.

How I can realize it? What class I need to extend?

like image 953
Alexiuscrow Avatar asked Jul 30 '15 08:07

Alexiuscrow


1 Answers

The swagger 2.0 supports custom fields, there was a Pull Request for this back in 2013 (https://github.com/swagger-api/swagger-node/pull/47).

While apparently it's easy to add the custom fields, since they are not present in the Swagger 2.0 spec, Swagger-UI won't display them by default.

For this to work you will have to change a couple of things.

  1. Implement the desired annotation in your parser implementation (ie. swagger-core or swagger-php) if it doesn't exist.
  2. Clone and modify swagger-ui to display your custom field as you wish.

Note that by doing this you will in fact violate the swagger json schema (https://github.com/swagger-api/swagger-spec/blob/master/schemas/v2.0/schema.json) and any third party validators you may use will fail.

like image 127
Mark Avatar answered Sep 22 '22 18:09

Mark