Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(issues-2079): Implement JsonView support for swagger. #2662

Closed
wants to merge 2 commits into from

Conversation

p4ali
Copy link

@p4ali p4ali commented Feb 14, 2018

See original request #2079

The patch will create various definition models for response based on the original model and
the JsonView, e.g., given the original model Car, and JsonView named Detail and Summary, the
definition model could be Car_Summary, Car_Detail, or Car_Summary-or-Detail.

The patch also support the inheritance of the JsonView, e.g., The Detail is a subtype of Summary,
so any type/field annotated by Summary, should be visible to Detail view.

Please refer to the JsonViewTest.java for detail.
Also refer to the CarResource.java and the updated ApiListingResourceIT.java

  private static class View {

    interface Summary {}

    interface Detail extends Summary { }

    interface Sale {}
  }

  private static class Car {

    @JsonView(View.Summary.class)
    @JsonProperty("manufacture")
    private String made = "Honda";

    @JsonView({View.Summary.class, View.Detail.class})
    private String model = "Accord Hybrid";

    @JsonView({View.Detail.class})
    @JsonProperty
    private List<Tire> tires = Arrays.asList(new Tire());

    @JsonView({View.Sale.class})
    @JsonProperty
    private int price = 40000;

    // always in
    private String color = "White";

    public String getColor() {
      return color;
    }
  }

  private static class Tire {

    @JsonView(View.Summary.class)
    @JsonProperty("brand")
    private String made = "Michelin";

    @JsonView(View.Detail.class)
    @JsonProperty
    private String condition = "new";
  }

 @Api
  @Path("/")
  private static class CarDetailApi {

    @GET
    @Path("/cars/detail")
    @JsonView({View.Detail.class})
    @ApiOperation(value = "Return car detail", response = Car.class, responseContainer = "List")
    public Response getDetails() {
      return Response.ok(Arrays.asList(new Car())).build();
    }
  }

The following swagger will be generated:

{
  "swagger": "2.0",
  "paths": {
    "/cars/detail": {
      "get": {
        "summary": "Return car detail",
        "description": "",
        "operationId": "getDetails",
        "parameters": [
          
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Car_Detail"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "Car_Detail": {
      "type": "object",
      "properties": {
        "model": {
          "type": "string"
        },
        "tires": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Tire_Detail"
          }
        },
        "color": {
          "type": "string"
        },
        "manufacture": {
          "type": "string"
        }
      }
    },
    "Tire_Detail": {
      "type": "object",
      "properties": {
        "condition": {
          "type": "string"
        },
        "brand": {
          "type": "string"
        }
      }
    }
  }
}

See original request swagger-api#2079

The patch will create various definition models for response based on the original model, plus
the JsonView, e.g., given the original model Car, and JsonView named Detail and Summary, the
definition model could be Car_Summary, Car_Detail, or Car_Summary-or-Detail.

The patch also support the inheritance of the JsonView, e.g., The Detail is a subtype of Summary,
so any type/field annotated by Summary, should be visible to Detail view.

Please refer to the JsonViewTest.java for detail.
Also refer to the CarResource.java and the updated ApiListingResourceIT.java
@webron webron requested a review from frantuma February 15, 2018 02:11
frantuma added a commit that referenced this pull request Feb 23, 2018
 refs #2079 - JsonView support (includes and replaces #2662)
@frantuma
Copy link
Member

@p4ali Thanks! this PR has been included in merged #2681 which adds some tests and minor fixes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants