Spring-Doc / OpenAPI / Swagger + VAVR-Collections

29 Jul
2020

As mentioned in my previous articles about using VAVR together with Spring, use the following classes to make VAVRs Option and all Collection types based on Seq work correctly with SpringDoc. Just place them in a package that gets scanned by Spring!

import com.fasterxml.jackson.databind.JavaType;
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.util.Json;
import io.swagger.v3.oas.models.media.Schema;
import java.util.Iterator;
import org.springframework.stereotype.Component;
@Component
public class VavrOptionSupportConverter implements ModelConverter {
@Override
public Schema resolve(
AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = Json.mapper().constructType(annotatedType.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (io.vavr.control.Option.class.equals(cls)) {
annotatedType =
new AnnotatedType()
.type(javaType.containedType(0))
.ctxAnnotations(annotatedType.getCtxAnnotations())
.parent(annotatedType.getParent())
.schemaProperty(annotatedType.isSchemaProperty())
.name(annotatedType.getName())
.resolveAsRef(annotatedType.isResolveAsRef())
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
.propertyName(annotatedType.getPropertyName())
.skipOverride(true);
return this.resolve(annotatedType, context, chain);
}
}
return (chain.hasNext()) ? chain.next().resolve(annotatedType, context, chain) : null;
}
}

import com.fasterxml.jackson.databind.JavaType;
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.util.Json;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import java.util.Iterator;
import org.springframework.stereotype.Component;
@Component
public class VavrSeqSupportConverter implements ModelConverter {
@Override
public Schema resolve(
AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = Json.mapper().constructType(annotatedType.getType());
if (javaType != null) {
Class<?> cls = javaType.getRawClass();
if (io.vavr.collection.Seq.class.isAssignableFrom(cls)) {
annotatedType =
new AnnotatedType()
.type(javaType.containedType(0))
.ctxAnnotations(annotatedType.getCtxAnnotations())
.parent(annotatedType.getParent())
.schemaProperty(annotatedType.isSchemaProperty())
.name(annotatedType.getName())
.resolveAsRef(annotatedType.isResolveAsRef())
.jsonViewAnnotation(annotatedType.getJsonViewAnnotation())
.propertyName(annotatedType.getPropertyName())
.skipOverride(true);
return new ArraySchema().items(this.resolve(annotatedType, context, chain));
}
}
return (chain.hasNext()) ? chain.next().resolve(annotatedType, context, chain) : null;
}
}

A big shoutout to bnasslahsen who provided me with the VavrOptionSupportConverter that I adjusted to work with the collection types (see this issue)

Comment Form

top