Created
July 29, 2020 06:57
-
-
Save domdorn/bddf5ce1348464179fdd4ffc65b70870 to your computer and use it in GitHub Desktop.
Spring + SpringDoc + Vavr-Collections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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