Skip to content

Commit

Permalink
support custom annotation for containers
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jun 16, 2023
1 parent c6b104d commit 2676db0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,18 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
BeanDescription valueTypeBeanDesc = _mapper.getSerializationConfig().introspect(valueType);
pName = _typeName(valueType, valueTypeBeanDesc);
}
Annotation[] schemaAnnotations = null;
List<Annotation> strippedCtxAnnotations = new ArrayList<>();
if (resolvedSchemaAnnotation != null) {
schemaAnnotations = new Annotation[]{resolvedSchemaAnnotation};
strippedCtxAnnotations.add(0, resolvedSchemaAnnotation);
}
if (annotatedType.getCtxAnnotations() != null) {
strippedCtxAnnotations.addAll(Arrays.stream(
annotatedType.getCtxAnnotations()).filter(
ass -> !ass.annotationType().getName().startsWith("io.swagger")
).collect(Collectors.toList()));
}


if (keyType != null && valueType != null) {
if (ReflectionUtils.isSystemType(type) && !annotatedType.isSchemaProperty() && !annotatedType.isResolveAsRef()) {
context.resolve(new AnnotatedType().type(valueType).jsonViewAnnotation(annotatedType.getJsonViewAnnotation()));
Expand All @@ -424,7 +432,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
new AnnotatedType()
.type(valueType)
.schemaProperty(annotatedType.isSchemaProperty())
.ctxAnnotations(schemaAnnotations)
.ctxAnnotations(strippedCtxAnnotations.toArray(new Annotation[0]))
.skipSchemaName(true)
.resolveAsRef(annotatedType.isResolveAsRef())
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
Expand Down Expand Up @@ -454,7 +462,7 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
Schema items = context.resolve(new AnnotatedType()
.type(valueType)
.schemaProperty(annotatedType.isSchemaProperty())
.ctxAnnotations(schemaAnnotations)
.ctxAnnotations(strippedCtxAnnotations.toArray(new Annotation[0]))
.skipSchemaName(true)
.resolveAsRef(annotatedType.isResolveAsRef())
.propertyName(annotatedType.getPropertyName())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.swagger.v3.core.converting.override;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.core.resolving.resources.BidimensionalArray;
import io.swagger.v3.core.util.AnnotationsUtils;
import io.swagger.v3.oas.models.media.Schema;

import java.util.Iterator;

public class CustomAnnotationConverter extends ModelResolver {

public CustomAnnotationConverter(ObjectMapper mapper) {
super(mapper);
}

@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
Schema s = chain.next().resolve(type, context, chain);
if (s != null) {
if ("array".equals(s.getType()) && s.getItems() != null && "array".equals(s.getItems().getType())) {
BidimensionalArray.MySizeAnnotation size = AnnotationsUtils.getAnnotation(BidimensionalArray.MySizeAnnotation.class, type.getCtxAnnotations());
if (size != null) {
s.getItems().maxItems(size.maxItems());
}
}
return s;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.v3.core.converter.ModelConverters;
import io.swagger.v3.core.converting.override.resources.MyCustomClass;
import io.swagger.v3.core.matchers.SerializationMatchers;
import io.swagger.v3.core.resolving.resources.BidimensionalArray;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -69,4 +70,30 @@ public MyCustomClass getMyCustomClass() {
public void setMyCustomClass(MyCustomClass myCustomClass) {
}
}

@Test
public void customAnnotationTest() throws Exception {
ModelConverters.getInstance().addConverter(new CustomAnnotationConverter(Json.mapper()));
final Map<String, Schema> model = ModelConverters.getInstance().read(BidimensionalArray.class);
final String expected = "BidimensionalArray:\n" +
" type: object\n" +
" properties:\n" +
" withCustomAnnotation:\n" +
" maxItems: 3\n" +
" type: array\n" +
" items:\n" +
" maxItems: 2\n" +
" type: array\n" +
" items:\n" +
" type: string\n" +
" withHelperClass:\n" +
" maxItems: 3\n" +
" type: array\n" +
" items:\n" +
" maxItems: 2\n" +
" type: array\n" +
" items:\n" +
" type: string\n";
SerializationMatchers.assertEqualsToYaml(model, expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.swagger.v3.core.resolving.resources;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.media.ArraySchema;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

public class BidimensionalArray {
@ArraySchema(maxItems = 3)
@MySizeAnnotation(maxItems = 2)
public List<List<String>> withCustomAnnotation;

@ArraySchema(maxItems = 3)
public List<Foo<String>> withHelperClass;


@ArraySchema(maxItems = 2)
@JsonIgnoreProperties({"empty"})
public static interface Foo<T> extends List<T> {

}

@Retention(RetentionPolicy.RUNTIME)
@Inherited
public static @interface MySizeAnnotation {
int maxItems() default Integer.MIN_VALUE;
}
}

0 comments on commit 2676db0

Please sign in to comment.