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

Fix #4239: @JsonView ignored for subclasses #4282

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
* This must be done after model.setProperties so that the model's set
* of properties is available to filter from any subtypes
**/
if (!resolveSubtypes(model, beanDesc, context)) {
if (!resolveSubtypes(model, beanDesc, context, annotatedType.getJsonViewAnnotation())) {
model.setDiscriminator(null);
}

Expand Down Expand Up @@ -1370,7 +1370,7 @@ protected void applyBeanValidatorAnnotations(Schema property, Annotation[] annot
}
}

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

final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType));
final Schema subtypeModel = context.resolve(new AnnotatedType().type(subtypeType)
.jsonViewAnnotation(jsonViewAnnotation));

if ( StringUtils.isBlank(subtypeModel.getName()) ||
subtypeModel.getName().equals(model.getName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.swagger.v3.core.resolving;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverterContextImpl;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.matchers.SerializationMatchers;
import org.testng.annotations.Test;

import java.lang.annotation.Annotation;

import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME;

public class Ticket4239Test extends SwaggerTestBase {

public interface Input {}

public interface Output {}

@JsonTypeInfo(use = NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = A1.class),
})
public static abstract class A {
@JsonView(Input.class)
public String a_in;
@JsonView(Output.class)
public String a_out;
}

public static class A1 extends A {
@JsonView(Input.class)
public String a1_in;
@JsonView(Output.class)
public String a1_out;
}

private static final JsonView VIEW_OUTPUT = new JsonView() {
public Class<? extends Annotation> annotationType() {
return JsonView.class;
}
public Class<?>[] value() {
return new Class[] {Output.class};
}
};

@Test
public void testJsonValueSchemaAnnotation() {

final ModelResolver modelResolver = new ModelResolver(mapper());

ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);

AnnotatedType type = new AnnotatedType(Ticket4239Test.A.class).jsonViewAnnotation(VIEW_OUTPUT);

context.resolve(type);

SerializationMatchers.assertEqualsToYaml(context.getDefinedModels(), "A1_Output:\n" +
" type: object\n" +
" allOf:\n" +
" - $ref: '#/components/schemas/A_Output'\n" +
" - type: object\n" +
" properties:\n" +
" a1_out:\n" +
" type: string\n" +
"A_Output:\n" +
" type: object\n" +
" properties:\n" +
" a_out:\n" +
" type: string\n");

}
}