Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring + SpringDoc + Vavr-Collections
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.vavr.concurrent.Future;
import io.vavr.jackson.datatype.VavrModule;
import org.springdoc.core.converters.ConverterUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenAPIConfiguration {
static {
ConverterUtils.addResponseWrapperToIgnore(Future.class);
// use this if you need to replace a class. If it contains generics, you'll have to create a
// converter - see below.
// SpringDocUtils.getConfig()
// .replaceWithClass(io.vavr.collection.Map.class, java.util.Map.class)
// ;
Json.mapper().registerModule(new VavrModule());
}
@Bean
public OpenAPI masterDataOpenAPI() {
return new OpenAPI()
.info(
new Info()
.title("My-Service API")
.description("API Spec for the My Service")
.version("v0.0.1"));
}
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.