From e0eca46b634222e458359bff311964932c500bd4 Mon Sep 17 00:00:00 2001 From: Ian White Date: Tue, 16 Oct 2018 15:12:31 -0400 Subject: [PATCH] adding PartialConverter, UpdateConverter, ResultsConverter to make using swagger with partials nicer --- pom.xml | 2 +- stardao-core/pom.xml | 2 +- stardao-jersey/pom.xml | 10 ++++ .../stardao/swagger/PartialConverter.java | 38 +++++++++++++ .../stardao/swagger/ResultsConverter.java | 56 +++++++++++++++++++ .../stardao/swagger/UpdateConverter.java | 42 ++++++++++++++ 6 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 stardao-jersey/src/main/java/io/stardog/stardao/swagger/PartialConverter.java create mode 100644 stardao-jersey/src/main/java/io/stardog/stardao/swagger/ResultsConverter.java create mode 100644 stardao-jersey/src/main/java/io/stardog/stardao/swagger/UpdateConverter.java diff --git a/pom.xml b/pom.xml index e823ed7..3cc58d4 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,7 @@ org.jacoco jacoco-maven-plugin - 0.8.1 + 0.8.2 **/AutoValue_* diff --git a/stardao-core/pom.xml b/stardao-core/pom.xml index b24b34a..2e32dfc 100644 --- a/stardao-core/pom.xml +++ b/stardao-core/pom.xml @@ -67,7 +67,7 @@ io.swagger swagger-annotations - 1.5.18 + 1.5.21 org.jetbrains.kotlin diff --git a/stardao-jersey/pom.xml b/stardao-jersey/pom.xml index 2911985..6e06414 100644 --- a/stardao-jersey/pom.xml +++ b/stardao-jersey/pom.xml @@ -28,6 +28,16 @@ jersey-common 2.26 + + io.swagger + swagger-core + 1.5.21 + + + io.swagger + swagger-annotations + 1.5.21 + \ No newline at end of file diff --git a/stardao-jersey/src/main/java/io/stardog/stardao/swagger/PartialConverter.java b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/PartialConverter.java new file mode 100644 index 0000000..3f00c48 --- /dev/null +++ b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/PartialConverter.java @@ -0,0 +1,38 @@ +package io.stardog.stardao.swagger; + +import com.fasterxml.jackson.databind.JavaType; +import io.swagger.converter.ModelConverter; +import io.swagger.converter.ModelConverterContext; +import io.swagger.models.Model; +import io.swagger.models.properties.Property; +import io.swagger.models.properties.RefProperty; +import io.swagger.util.Json; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Iterator; + +/** + * The PartialConverter takes references to PartialModel objects and turns them into references to Model objects. + */ +public class PartialConverter implements ModelConverter { + public PartialConverter() { + } + + public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator chain) { + JavaType jType = Json.mapper().constructType(type); + if(jType != null) { + Class cls = jType.getRawClass(); + if (cls.getSimpleName().startsWith("Partial")) { + String baseClass = cls.getSimpleName().substring(7); + return new RefProperty("#/definitions/" + baseClass); + } + } + + return chain.hasNext() ? chain.next().resolveProperty(type, context, annotations, chain) : null; + } + + public Model resolve(Type type, ModelConverterContext context, Iterator chain) { + return chain.hasNext() ? chain.next().resolve(type, context, chain) : null; + } +} diff --git a/stardao-jersey/src/main/java/io/stardog/stardao/swagger/ResultsConverter.java b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/ResultsConverter.java new file mode 100644 index 0000000..1062f60 --- /dev/null +++ b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/ResultsConverter.java @@ -0,0 +1,56 @@ +package io.stardog.stardao.swagger; + +import com.fasterxml.jackson.databind.JavaType; +import io.swagger.converter.ModelConverter; +import io.swagger.converter.ModelConverterContext; +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.Property; +import io.swagger.models.properties.RefProperty; +import io.swagger.models.properties.StringProperty; +import io.swagger.util.Json; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Iterator; + +/** + * The ResultsConverter turns a Results object that contains Partial objects into a Results object that does not + * contain Partial in the name. + */ +public class ResultsConverter implements ModelConverter { + public ResultsConverter() { + } + + public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator chain) { + return chain.hasNext() ? chain.next().resolveProperty(type, context, annotations, chain) : null; + } + + public Model resolve(Type type, ModelConverterContext context, Iterator chain) { + JavaType jType = Json.mapper().constructType(type); + if(jType != null) { + Class cls = jType.getRawClass(); + if (cls.getName().equals("io.stardog.stardao.core.Results") && jType.containedTypeCount() == 2) { + String modelType = jType.containedType(0).getRawClass().getSimpleName(); + if (modelType.startsWith("Partial")) { + modelType = modelType.substring(7); + } + String nextType = jType.containedType(1).getRawClass().getSimpleName(); + + ModelImpl result = new ModelImpl(); + result.name("Results" + modelType + nextType); + + Property dataProperty = new ArrayProperty(new RefProperty("#/definitions/" + modelType)); + dataProperty.setRequired(true); + + Property nextProperty = new StringProperty(); + result.addProperty("data", dataProperty); + result.addProperty("next", nextProperty); + return result; + } + } + + return chain.hasNext() ? chain.next().resolve(type, context, chain) : null; + } +} diff --git a/stardao-jersey/src/main/java/io/stardog/stardao/swagger/UpdateConverter.java b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/UpdateConverter.java new file mode 100644 index 0000000..66ae4ab --- /dev/null +++ b/stardao-jersey/src/main/java/io/stardog/stardao/swagger/UpdateConverter.java @@ -0,0 +1,42 @@ +package io.stardog.stardao.swagger; + +import com.fasterxml.jackson.databind.JavaType; +import io.swagger.converter.ModelConverter; +import io.swagger.converter.ModelConverterContext; +import io.swagger.models.Model; +import io.swagger.models.properties.Property; +import io.swagger.models.properties.RefProperty; +import io.swagger.util.Json; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Iterator; + +/** + * The UpdateConverter takes references to Update objects and turns them into references to Model objects. + */ +public class UpdateConverter implements ModelConverter { + public UpdateConverter() { + } + + public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator chain) { + JavaType jType = Json.mapper().constructType(type); + if(jType != null) { + Class cls = jType.getRawClass(); + if (cls.getName().equals("io.stardog.stardao.core.Update") && jType.containedTypeCount() > 0) { + String innerName = jType.containedType(0).getRawClass().getSimpleName(); + if (innerName.startsWith("Partial")) { + return new RefProperty("#/definitions/" + innerName.substring(7)); + } else { + return new RefProperty("#/definitions/" + innerName); + } + } + } + + return chain.hasNext() ? chain.next().resolveProperty(type, context, annotations, chain) : null; + } + + public Model resolve(Type type, ModelConverterContext context, Iterator chain) { + return chain.hasNext() ? chain.next().resolve(type, context, chain) : null; + } +}