Skip to content

Commit

Permalink
refs #2079 - JsonView support minor fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Feb 22, 2018
1 parent 0fe3ca6 commit 964f8dd
Show file tree
Hide file tree
Showing 7 changed files with 731 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,10 @@
*/

Extension[] extensions() default @Extension(properties = @ExtensionProperty(name = "", value = ""));

/**
* Ignores JsonView annotations while resolving operations and types. For backward compatibility
*
*/
boolean ignoreJsonView() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,18 @@ public void addClassToSkip(String cls) {
this.skippedClasses.add(cls);
}

public Property readAsProperty(Type type) {
return readAsProperty(type, null);
}

public Property readAsProperty(Type type, JsonView jsonView) {
ModelConverterContextImpl context = new ModelConverterContextImpl(converters);
context.setJsonView(jsonView);
return context.resolveProperty(type, null);
}

public Property readAsProperty(Type type) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);
return context.resolveProperty(type, null);
}

public Map<String, Model> read(Type type) {
Map<String, Model> modelMap = new HashMap<String, Model>();
if (shouldProcess(type)) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);
Model resolve = context.resolve(type);
for (Entry<String, Model> entry : context.getDefinedModels()
.entrySet()) {
if (entry.getValue().equals(resolve)) {
modelMap.put(entry.getKey(), entry.getValue());
}
}
}
return modelMap;
return read(type, null);
}

public Map<String, Model> read(Type type, JsonView jsonView) {
Expand All @@ -99,15 +85,7 @@ public Map<String, Model> read(Type type, JsonView jsonView) {
}

public Map<String, Model> readAll(Type type) {
if (shouldProcess(type)) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);

LOGGER.debug("ModelConverters readAll from " + type);
context.resolve(type);
return context.getDefinedModels();
}
return new HashMap<String, Model>();
return readAll(type, null);
}

public Map<String, Model> readAll(Type type, JsonView annotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public Model resolve(JavaType type, ModelConverterContext context, Iterator<Mode
}

annotations = annotationList.toArray(new Annotation[annotationList.size()]);
if(hidenByJsonView(annotations, context)) {
if(hiddenByJsonView(annotations, context)) {
continue;
}

Expand Down Expand Up @@ -639,7 +639,7 @@ parent class @ApiModel annotation, then do the following:
*/
private String decorateModelName(ModelConverterContext context, String originalName) {
String name = originalName;
if (context.getJsonView() != null) {
if (context.getJsonView() != null && context.getJsonView().value().length > 0) {
String COMBINER = "-or-";
StringBuffer sb = new StringBuffer();
for (Class<?> view : context.getJsonView().value()) {
Expand All @@ -651,7 +651,7 @@ private String decorateModelName(ModelConverterContext context, String originalN
return name;
}

private boolean hidenByJsonView(Annotation[] annotations,
private boolean hiddenByJsonView(Annotation[] annotations,
ModelConverterContext context) {
JsonView jsonView = context.getJsonView();
if (jsonView == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,9 @@ private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annot
if (apiOperation.hidden()) {
return null;
}
if (apiOperation.ignoreJsonView()) {
jsonViewAnnotation = null;
}
if (!apiOperation.nickname().isEmpty()) {
operationId = apiOperation.nickname();
}
Expand Down
243 changes: 144 additions & 99 deletions modules/swagger-jaxrs/src/test/java/io/swagger/JsonViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,139 +9,184 @@
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.util.Json;

import java.util.Arrays;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.testng.Assert;
import org.testng.annotations.Test;


public class JsonViewTest {

private static class View {
private static class View {

interface Summary {
interface Summary {

}
}

interface Detail extends Summary {
interface Detail extends Summary {

}
}

interface Sale {
interface Sale {

}
}
}

private static class Car {
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.Summary.class)
@JsonProperty("manufacture")
private String made = "Honda";
@JsonView({View.Sale.class})
@JsonProperty
private int price = 40000;

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

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

private static class Tire {

@JsonView({View.Sale.class})
@JsonProperty
private int price = 40000;
@JsonView(View.Summary.class)
@JsonProperty("brand")
private String made = "Michelin";

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

// always in
private String color = "White";
@Api
@Path("/")
private static class CarSummaryApi {

public String getColor() {
return color;
@GET
@Path("/cars/summary")
@JsonView({View.Summary.class})
public List<Car> getSummaries() {
return Arrays.asList(new Car());
}
}
}

private static class Tire {
@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();
}
}

@JsonView(View.Summary.class)
@JsonProperty("brand")
private String made = "Michelin";
@Api
@Path("/")
private static class CarSaleSummaryApi {

@JsonView(View.Detail.class)
@JsonProperty
private String condition = "new";
}
@GET
@Path("/cars/sale")
@JsonView({View.Summary.class, View.Sale.class})
public List<Car> getSaleSummaries() {
return Arrays.asList(new Car());
}
}

@Api
@Path("/")
private static class CarSummaryApi {
@Api
@Path("/")
private static class CarApi {

@GET
@Path("/cars/summary")
@JsonView({View.Summary.class})
public List<Car> getSummaries() {
return Arrays.asList(new Car());
@GET
@Path("/cars")
public List<Car> getCars() {
return Arrays.asList(new Car());
}
}
}

@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();

@Api
@Path("/ignore")
private static class CarIgnoreApi {

@GET
@Path("/cars")
@JsonView({View.Summary.class, View.Sale.class})
@ApiOperation(value = "getCars", ignoreJsonView = true)
public List<Car> getCars() {
return Arrays.asList(new Car());
}
}
}

@Api
@Path("/")
private static class CarSaleSummaryApi {
@Test(description = "check awareness of JsonView")
public void shouldSerializeTypeParameter() throws JsonProcessingException {
Swagger swagger = getSwagger(CarSummaryApi.class);
String swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertFalse(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertFalse(swaggerJson.contains("condition"));

swagger = getSwagger(CarDetailApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("tires"));
Assert.assertTrue(swaggerJson.contains("brand"));
Assert.assertTrue(swaggerJson.contains("condition"));
Assert.assertTrue(swaggerJson.contains("Car_Detail"));

swagger = getSwagger(CarSaleSummaryApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("price"));
Assert.assertFalse(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertFalse(swaggerJson.contains("condition"));

swagger = getSwagger(CarApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("price"));
Assert.assertTrue(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertTrue(swaggerJson.contains("condition"));

swagger = getSwagger(CarIgnoreApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("price"));
Assert.assertTrue(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertTrue(swaggerJson.contains("condition"));
}

@GET
@Path("/cars/sale")
@JsonView({View.Summary.class, View.Sale.class})
public List<Car> getSaleSummaries() {
return Arrays.asList(new Car());
private Swagger getSwagger(Class<?> cls) {
return new Reader(new Swagger()).read(cls);
}
}

@Test(description = "check awareness of JsonView")
public void shouldSerializeTypeParameter() throws JsonProcessingException {
Swagger swagger = getSwagger(CarSummaryApi.class);
String swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertFalse(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertFalse(swaggerJson.contains("condition"));

swagger = getSwagger(CarDetailApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("tires"));
Assert.assertTrue(swaggerJson.contains("brand"));
Assert.assertTrue(swaggerJson.contains("condition"));
Assert.assertTrue(swaggerJson.contains("Car_Detail"));

swagger = getSwagger(CarSaleSummaryApi.class);
swaggerJson = Json.mapper().writeValueAsString(swagger);
Assert.assertTrue(swaggerJson.contains("manufacture"));
Assert.assertTrue(swaggerJson.contains("model"));
Assert.assertTrue(swaggerJson.contains("color"));
Assert.assertTrue(swaggerJson.contains("price"));
Assert.assertFalse(swaggerJson.contains("tires"));
Assert.assertFalse(swaggerJson.contains("made"));
Assert.assertFalse(swaggerJson.contains("condition"));
}

private Swagger getSwagger(Class<?> cls) {
return new Reader(new Swagger()).read(cls);
}

}
Loading

0 comments on commit 964f8dd

Please sign in to comment.