Alexiuscrow
Alexiuscrow

Reputation: 785

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?

Upvotes: 9

Views: 9998

Answers (2)

user2179737
user2179737

Reputation: 551

I believe what you are trying to do ca be achieved extending the swagger core reader as described in swagger documentation. Here is an example in one of my projects.

Upvotes: 0

Mark
Mark

Reputation: 5757

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.

Upvotes: 1

Related Questions