I currently have several APIs that share common objects. In some cases, I want to hide certain properties of these common objects when generating the swagger documentation. For instance, let's say I have a simple class:
public class Person {
private String forename;
private String surname;
private int age;
/* getters and setters with annotations here... */
}
For some APIs, I want the age field to appear in the swagger documentation but in other APIs I don't.
I don't want to use the hidden attribute of the @ApiModelProperty annotation as this will hide the property for all APIs. I can see there is an access attribute which I could use with my own filter class that extends SwaggerSpecFilter.
How can I do this by implementing the isPropertyAllowed method of this interface? There does not seem to be any parameter in that method that I can use to find out which Api is using the Model and property.
I've managed to implement this by having all my API classes have a 'Filter' class that will add/remove/edit any properties from the API models like so:
public class MyApiFilter implements SwaggerFilterIF {
@Override
public void filter(Swagger swagger) {
Map<String, Model> definitions = swagger.getDefinitions();
// remove 'age' from 'Person'
Model model = definitions.get("Person");
model.getProperties().remove("age");
}
This is then referenced in the API class:
@Api
@Path("/myapi")
public class MyApi implements SwaggerApiIF {
public MyApi () {
super();
}
@Override
public SwaggerFilterIF getFilter() {
return new MyApiFilter();
}
}
I then have a servlet to generate the API documentation for every API that I can access individually. In this servlet, I call the filter method on the 'Filter' class for the API which will filter the swagger definition as required:
for (final SwaggerApiIF api : apis) {
final Swagger swagger = new Reader(new Swagger(), config).read(api.getClass());
api.getFilter().filter(swagger);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With