Skip to content

Commit 7b20fd9

Browse files
devopsixfrantuma
authored andcommitted
fix: JsonView annotation ignored for subclasses
Forward JsonView annotation when resolving subtypes. Refs: #4239
1 parent 48696b5 commit 7b20fd9

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
742742
* This must be done after model.setProperties so that the model's set
743743
* of properties is available to filter from any subtypes
744744
**/
745-
if (!resolveSubtypes(model, beanDesc, context)) {
745+
if (!resolveSubtypes(model, beanDesc, context, annotatedType.getJsonViewAnnotation())) {
746746
model.setDiscriminator(null);
747747
}
748748

@@ -1370,7 +1370,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
13701370
}
13711371
}
13721372

1373-
private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context) {
1373+
private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConverterContext context, JsonView jsonViewAnnotation) {
13741374
final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
13751375
if (types == null) {
13761376
return false;
@@ -1398,7 +1398,8 @@ private boolean resolveSubtypes(Schema model, BeanDescription bean, ModelConvert
13981398
continue;
13991399
}
14001400

1401-
final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType));
1401+
final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType)
1402+
.jsonViewAnnotation(jsonViewAnnotation));
14021403

14031404
if ( StringUtils.isBlank(subtypeModel.getName()) ||
14041405
subtypeModel.getName().equals(model.getName())) {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.swagger.v3.core.resolving;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import com.fasterxml.jackson.annotation.JsonView;
6+
import io.swagger.v3.core.converter.AnnotatedType;
7+
import io.swagger.v3.core.converter.ModelConverterContextImpl;
8+
import io.swagger.v3.core.jackson.ModelResolver;
9+
import io.swagger.v3.core.matchers.SerializationMatchers;
10+
import org.testng.annotations.Test;
11+
12+
import java.lang.annotation.Annotation;
13+
14+
import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;
15+
16+
public class Ticket4239Test extends SwaggerTestBase {
17+
18+
public interface Input {}
19+
20+
public interface Output {}
21+
22+
@JsonTypeInfo(use = NAME)
23+
@JsonSubTypes({
24+
@JsonSubTypes.Type(value = A1.class),
25+
})
26+
public static abstract class A {
27+
@JsonView(Input.class)
28+
public String a_in;
29+
@JsonView(Output.class)
30+
public String a_out;
31+
}
32+
33+
public static class A1 extends A {
34+
@JsonView(Input.class)
35+
public String a1_in;
36+
@JsonView(Output.class)
37+
public String a1_out;
38+
}
39+
40+
private static final JsonView VIEW_OUTPUT = new JsonView() {
41+
public Class<? extends Annotation> annotationType() {
42+
return JsonView.class;
43+
}
44+
public Class<?>[] value() {
45+
return new Class[] {Output.class};
46+
}
47+
};
48+
49+
@Test
50+
public void testJsonValueSchemaAnnotation() {
51+
52+
final ModelResolver modelResolver = new ModelResolver(mapper());
53+
54+
ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
55+
56+
AnnotatedType type = new AnnotatedType(Ticket4239Test.A.class).jsonViewAnnotation(VIEW_OUTPUT);
57+
58+
context.resolve(type);
59+
60+
SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "A1_Output:\n" +
61+
" type: object\n" +
62+
" allOf:\n" +
63+
" - $ref: '#/components/schemas/A_Output'\n" +
64+
" - type: object\n" +
65+
" properties:\n" +
66+
" a1_out:\n" +
67+
" type: string\n" +
68+
"A_Output:\n" +
69+
" type: object\n" +
70+
" properties:\n" +
71+
" a_out:\n" +
72+
" type: string\n");
73+
74+
}
75+
}

0 commit comments

Comments
 (0)