-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding PartialConverter, UpdateConverter, ResultsConverter to make us…
…ing swagger with partials nicer
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
38 changes: 38 additions & 0 deletions
38
stardao-jersey/src/main/java/io/stardog/stardao/swagger/PartialConverter.java
This file contains 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
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
stardao-jersey/src/main/java/io/stardog/stardao/swagger/ResultsConverter.java
This file contains 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
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; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
stardao-jersey/src/main/java/io/stardog/stardao/swagger/UpdateConverter.java
This file contains 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
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; | ||
} | ||
} |