Skip to content

Commit 111f3f6

Browse files
committed
added swagger json output and started improving the swagger output
1 parent 115fa75 commit 111f3f6

File tree

7 files changed

+108
-19
lines changed

7 files changed

+108
-19
lines changed

challenger/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
<version>${thingifier.version}</version>
2020
</dependency>
2121

22+
<dependency>
23+
<groupId>io.swagger.core.v3</groupId>
24+
<artifactId>swagger-models</artifactId>
25+
<version>${swagger-models-version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.swagger.core.v3</groupId>
29+
<artifactId>swagger-core</artifactId>
30+
<version>${swagger-models-version}</version>
31+
</dependency>
2232
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk -->
2333
<!-- experimental cloud storage of challenger sessions -->
2434
<!-- todo: split into two apps, one aws enabled, one with local persistance only -->

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<properties>
1212
<thingifier.version>1.5.6-SNAPSHOT</thingifier.version>
13-
<swagger-models-version>2.1.4</swagger-models-version>
13+
<swagger-models-version>2.2.27</swagger-models-version>
1414
<gson-version>2.8.6</gson-version>
1515
<junit.jupiter.version>5.6.2</junit.jupiter.version>
1616
<maven.compiler.source>15</maven.compiler.source>

thingifier/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
<artifactId>swagger-models</artifactId>
6666
<version>${swagger-models-version}</version>
6767
</dependency>
68+
<dependency>
69+
<groupId>io.swagger.core.v3</groupId>
70+
<artifactId>swagger-core</artifactId>
71+
<version>${swagger-models-version}</version>
72+
</dependency>
6873
<dependency>
6974
<groupId>com.google.code.gson</groupId>
7075
<artifactId>gson</artifactId>

thingifier/src/main/java/uk/co/compendiumdev/thingifier/api/docgen/ApiRoutingDefinition.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package uk.co.compendiumdev.thingifier.api.docgen;
22

33
import uk.co.compendiumdev.thingifier.api.response.ResponseHeader;
4+
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
45

56
import java.util.ArrayList;
67
import java.util.Collection;
8+
import java.util.HashMap;
79
import java.util.List;
810

911
final public class ApiRoutingDefinition {
1012

1113
private List<RoutingDefinition> routings;
14+
private HashMap<String, EntityDefinition> objectSchemas;
1215

1316
public ApiRoutingDefinition() {
14-
this.routings = new ArrayList<>();
17+
routings = new ArrayList<>();
18+
objectSchemas = new HashMap<String,EntityDefinition>();
1519
}
1620

1721
public Collection<RoutingDefinition> definitions() {
@@ -29,4 +33,18 @@ public RoutingDefinition addRouting(final String documentation, final RoutingVer
2933
routings.add(defn);
3034
return defn;
3135
}
36+
37+
public void addObjectSchema(EntityDefinition entityDefn) {
38+
// TODO this should be an object schema rather than entityDefinition
39+
// because we don't want it to be editable
40+
objectSchemas.put(entityDefn.getName(),entityDefn);
41+
}
42+
43+
public boolean hasObjectSchemaNamed(String aName){
44+
return objectSchemas.containsKey(aName);
45+
}
46+
47+
public Collection<EntityDefinition> getObjectSchemas() {
48+
return objectSchemas.values();
49+
}
3250
}

thingifier/src/main/java/uk/co/compendiumdev/thingifier/api/docgen/ApiRoutingDefinitionDocGenerator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
6767

6868
for (EntityDefinition entityDefn : thingifier.getERmodel().getEntityDefinitions()) {
6969

70+
defn.addObjectSchema(entityDefn);
71+
7072
String uniqueIdentifier="?";
7173
String uniqueIdFieldName="fieldName";
7274

@@ -97,10 +99,11 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
9799
// we should be able to get things e.g. GET project
98100
defn.addRouting(
99101
String.format("return all the instances of %s", entityDefn.getName()),
100-
RoutingVerb.GET, pluralUrl, RoutingStatus.returnedFromCall()).
101-
addPossibleStatus(RoutingStatus.returnValue(
102-
200, String.format("All the %s", entityDefn.getPlural()))).
103-
setAsFilterableFrom(entityDefn);
102+
RoutingVerb.GET, pluralUrl, RoutingStatus.returnedFromCall()
103+
).addPossibleStatus(RoutingStatus.returnValue(
104+
200, String.format("All the %s", entityDefn.getPlural()))
105+
).setAsFilterableFrom(entityDefn)
106+
.returnPayload(200, entityDefn.getName());
104107

105108
defn.addRouting(String.format("headers for all the instances of %s", entityDefn.getName()),
106109
RoutingVerb.HEAD, pluralUrl, RoutingStatus.returnedFromCall()).

thingifier/src/main/java/uk/co/compendiumdev/thingifier/api/docgen/RoutingDefinition.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
55

66
import java.util.ArrayList;
7+
import java.util.HashMap;
78
import java.util.List;
89

910
public class RoutingDefinition {
@@ -15,6 +16,7 @@ public class RoutingDefinition {
1516
private boolean isFilterable;
1617
private EntityDefinition filterableEntityDefn;
1718
private List<RoutingStatus> possibleStatusResponses;
19+
private HashMap<Integer,String> returnPayload;
1820

1921
public RoutingDefinition(RoutingVerb verb, String url, RoutingStatus routingStatus, ResponseHeader header) {
2022
this.verb = verb;
@@ -25,9 +27,12 @@ public RoutingDefinition(RoutingVerb verb, String url, RoutingStatus routingStat
2527
}
2628
this.routingStatus = routingStatus;
2729
this.header = header;
28-
this.isFilterable=false;
30+
31+
// defaults
32+
isFilterable=false;
2933
filterableEntityDefn=null;
30-
this.possibleStatusResponses= new ArrayList<>();
34+
possibleStatusResponses= new ArrayList<>();
35+
returnPayload=new HashMap<Integer, String>();
3136
}
3237

3338
private List<RoutingStatus> getDefaultPossibleStatusResponses() {
@@ -78,9 +83,10 @@ public boolean isFilterable() {
7883
return isFilterable;
7984
}
8085

81-
public void setAsFilterableFrom(final EntityDefinition definition) {
86+
public RoutingDefinition setAsFilterableFrom(final EntityDefinition definition) {
8287
isFilterable=true;
8388
filterableEntityDefn = definition;
89+
return this;
8490
}
8591

8692
public EntityDefinition getFilterableEntity() {
@@ -106,4 +112,17 @@ public RoutingDefinition addPossibleStatuses(final Integer... statusCodes) {
106112
}
107113
return this;
108114
}
115+
116+
public RoutingDefinition returnPayload(final Integer statusCode, String objectSchemaName) {
117+
returnPayload.put(statusCode, objectSchemaName);
118+
return this;
119+
}
120+
121+
public boolean hasReturnPayloadFor(final Integer statusCode) {
122+
return returnPayload.containsKey(statusCode);
123+
}
124+
125+
public String getReturnPayloadFor(final Integer statusCode) {
126+
return returnPayload.get(statusCode);
127+
}
109128
}

thingifier/src/main/java/uk/co/compendiumdev/thingifier/swaggerizer/Swaggerizer.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package uk.co.compendiumdev.thingifier.swaggerizer;
22

3-
import com.google.gson.GsonBuilder;
4-
import io.swagger.v3.oas.models.OpenAPI;
5-
import io.swagger.v3.oas.models.Operation;
6-
import io.swagger.v3.oas.models.PathItem;
7-
import io.swagger.v3.oas.models.Paths;
3+
import io.swagger.v3.core.util.Json31;
4+
import io.swagger.v3.oas.models.*;
85
import io.swagger.v3.oas.models.info.Info;
6+
import io.swagger.v3.oas.models.media.*;
97
import io.swagger.v3.oas.models.responses.ApiResponse;
108
import io.swagger.v3.oas.models.responses.ApiResponses;
119
import io.swagger.v3.oas.models.servers.Server;
@@ -15,6 +13,8 @@
1513
import uk.co.compendiumdev.thingifier.api.docgen.ApiRoutingDefinitionDocGenerator;
1614
import uk.co.compendiumdev.thingifier.api.docgen.RoutingDefinition;
1715
import uk.co.compendiumdev.thingifier.api.docgen.RoutingStatus;
16+
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
17+
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
@@ -64,6 +64,26 @@ public OpenAPI swagger(){
6464

6565
List<String> processedAdditionalRoutes = new ArrayList<>();
6666

67+
Components components = new Components();
68+
for(EntityDefinition objectSchemaDefinition : routingDefinitions.getObjectSchemas()){
69+
70+
ObjectSchema object = new ObjectSchema();
71+
object.setDescription(objectSchemaDefinition.getName());
72+
object.setTitle(objectSchemaDefinition.getName());
73+
74+
//object.name(objectSchemaDefinition.getName());
75+
for(String propertyName : objectSchemaDefinition.getFieldNames()){
76+
Field propertyDefinition = objectSchemaDefinition.getField(propertyName);
77+
Schema<String> propertyItem = new Schema<String>();
78+
propertyItem.setExample(propertyDefinition.getExamples().get(0));
79+
object.addProperties(propertyName, propertyItem);
80+
}
81+
82+
components.addSchemas(objectSchemaDefinition.getName(), object);
83+
}
84+
85+
api.components(components);
86+
6787
if(routes!=null) {
6888

6989
api.setPaths(new Paths());
@@ -98,10 +118,25 @@ public OpenAPI swagger(){
98118
final ApiResponses responses = new ApiResponses();
99119
final List<RoutingStatus> possibleStatusResponses = subroute.getPossibleStatusReponses();
100120
for(RoutingStatus possibleStatus : possibleStatusResponses){
121+
122+
ApiResponse response = new ApiResponse().description(
123+
possibleStatus.description()
124+
);
125+
if(route.hasReturnPayloadFor(possibleStatus.value())){
126+
if(routingDefinitions.hasObjectSchemaNamed(route.getReturnPayloadFor(possibleStatus.value()))){
127+
String ref = "#/components/schemas/" + route.getReturnPayloadFor(possibleStatus.value());
128+
129+
Schema<String> object = new Schema<>();
130+
MediaType schema = new MediaType();
131+
schema.setSchema(object);
132+
object.set$ref(ref);
133+
response.setContent(
134+
new Content().addMediaType("application/json", schema));
135+
}
136+
}
101137
responses.addApiResponse(
102-
String.valueOf(possibleStatus.value()),
103-
new ApiResponse().description(
104-
possibleStatus.description())
138+
String.valueOf(possibleStatus.value()),
139+
response
105140
);
106141
}
107142
if(possibleStatusResponses.size()>0){
@@ -147,7 +182,6 @@ public String asJson(){
147182
if(api==null){
148183
swagger();
149184
}
150-
return new GsonBuilder().setPrettyPrinting().
151-
create().toJson(api);
185+
return Json31.pretty(api);
152186
}
153187
}

0 commit comments

Comments
 (0)