Swagger: How do you add ApiModelProperty for 3rd party code?

739 views Asked by At

I've got a Java project using Swagger. It does a great job of generating the swagger.json, but it's listing all the output fields as optional and I need some of them to be required. That's not a problem for my code because I can add the @ApiModelProperty annotation and specify whether it's required.

The problem is with objects from 3rd party jars. I can't go annotate that code. How do I create the equivalent functionality of @ApiModelProperty on 3rd party code?

1

There are 1 answers

0
Matthew On

Let's assume we're talking about newer version of swagger / openAPI. You can kludge this with a ReaderListener, something like so:

    public static class FixSwagger implements ReaderListener {
        @Override
        public void beforeScan(OpenApiReader reader, OpenAPI openAPI) {}

        @Override
        public void afterScan(OpenApiReader reader, OpenAPI openAPI) {
            var paths = openAPI.getPaths();
            paths.remove("/application.wadl");
            paths.remove("/application.wadl/{path}");
        }
    }

And then register this in your code using: jersey.register(FixSwagger.class);

The beforeScan() and afterScan() methods allow you to mutate the openAPI object, so you can programatically change whatever you need to.