Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Models or properties from swagger response

I used swagger in my apache cxf project , used @Api and @ApiOperations and @ApiParam annotations and generated a api doc for the rest services.

But I want to exclude some of the fields like EntityTag, StatusType and MediaType etc from Models attribute or complete modules or properties attribute.

How to do that?

I was fetching data from db and setting it to user object and passing that user object to JAX-RS response builder.

Below is one of my DTO Object:

  @ApiModel
  public class User{
  private String name;
   private String email;


 @ApiModelProperty(position = 1, required = true, notes = "used to display user name")
 public int getName() {
    return name;
 }

 public void setName(String name) {
    this.name= name;
}

@ApiModelProperty(position = 2, required = true, notes = "used to display user email")
public int getEmail() {
    return email;
}

 public void setEmail(String email) {
    this.email= email;
 }

Now I don't see the User object fields or properties inside the Swagger returned json format.

my service class method response is :

    @GET
    @ApiOperation(value = "xxx", httpMethod = "GET", notes = "user details", response =  Response.class)
   public Response getUserInfo(){
        User userdto = userdaoimpl.getUserDetails();
        ResponseBuilder builder = Response.ok(user, Status.OK), MediaType.APPLICATION_JSON);
        builder.build();
 }


<bean id="swaggerConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
    <property name="resourcePackage" value="com.services.impl" />
    <property name="version" value="1.0.0" />
    <property name="basePath" value="http://localhost:8080/api" />
    <property name="license" value="Apache 2.0 License" />
    <property name="licenseUrl"
        value="http://www.apache.org/licenses/LICENSE-2.0.html" />
    <property name="scan" value="true" />
 </bean>
like image 885
LazyGuy Avatar asked Jan 05 '15 10:01

LazyGuy


People also ask

How do I hide properties in swagger?

@ApiParam is also a Swagger annotation that we can use to specify metadata related to request parameters. We can set the hidden property to true in order to hide any property. Though, we have a limitation here: It works only if we're using @ModelAttribute instead of @RequestBody to access request data.

How do I hide a schema in swagger net core?

How to do it? add this property in your Swagger UI Options defaultModelsExpandDepth: -1 for hide schema section and for more reference refer this swagger.io/docs/open-source-tools/swagger-ui/usage/… Can you please add your swagger ui configuration settings in your question.

What is @API annotation in swagger?

The @ApiModelProperty annotation allows us to control Swagger-specific definitions such as description (value), name, data type, example values, and allowed values for the model properties. Also, it offers additional filtering properties in case we want to hide the property in certain scenarios.

What is @schema annotation in swagger?

The annotation may be used to define a Schema for a set of elements of the OpenAPI spec, and/or to define additional properties for the schema. It is applicable e.g. to parameters, schema classes (aka "models"), properties of such models, request and response content, header.


1 Answers

First of all, you should upgrade to the latest swagger-core version, currently 1.3.12 (you're using a really old one).

You have 3 ways to hide a property:

  1. If you're using JAXB annotations, you can use @XmlTransient.
  2. If you're using Jackson, you can use @JsonIgnore.
  3. If you're not using either, or don't want to affect the general de/serialization of your models, you can use Swagger's @ApiModelProperty's hidden attribute.

Keep in mind you may need to set these on your getters/setters rather than on the property itself. Play with the definitions to see what works for you.

With regards to the issue with the User model, the problem is that you do not reference it from the @ApiOperation (you also don't need the httpMethod property). Try changing it as follows:

@ApiOperation(value = "xxx", notes = "user details", response =  User.class)
like image 155
Ron Avatar answered Nov 22 '22 12:11

Ron