Skip to content

Commit

Permalink
adding PartialConverter, UpdateConverter, ResultsConverter to make us…
Browse files Browse the repository at this point in the history
…ing swagger with partials nicer
  • Loading branch information
eonwhite committed Oct 16, 2018
1 parent e485caa commit e0eca46
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<version>0.8.2</version>
<configuration>
<excludes>
<exclude>**/AutoValue_*</exclude>
Expand Down
2 changes: 1 addition & 1 deletion stardao-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down
10 changes: 10 additions & 0 deletions stardao-jersey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
<artifactId>jersey-common</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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<ModelConverter> 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<ModelConverter> chain) {
return chain.hasNext() ? chain.next().resolve(type, context, chain) : null;
}
}
Original file line number Diff line number Diff line change
@@ -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<ModelConverter> chain) {
return chain.hasNext() ? chain.next().resolveProperty(type, context, annotations, chain) : null;
}

public Model resolve(Type type, ModelConverterContext context, Iterator<ModelConverter> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<PartialModel> 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<ModelConverter> 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<ModelConverter> chain) {
return chain.hasNext() ? chain.next().resolve(type, context, chain) : null;
}
}

0 comments on commit e0eca46

Please sign in to comment.