Skip to content

Commit 562c823

Browse files
committed
added response examples and proper xml format
1 parent 111f3f6 commit 562c823

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public RoutingDefinition addRouting(final String documentation, final RoutingVer
3737
public void addObjectSchema(EntityDefinition entityDefn) {
3838
// TODO this should be an object schema rather than entityDefinition
3939
// because we don't want it to be editable
40+
// as single entity
4041
objectSchemas.put(entityDefn.getName(),entityDefn);
42+
// and as plural for array responses
43+
objectSchemas.put(entityDefn.getPlural(),entityDefn);
4144
}
4245

4346
public boolean hasObjectSchemaNamed(String aName){

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

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

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

70+
// add for the single entity
7071
defn.addObjectSchema(entityDefn);
7172

7273
String uniqueIdentifier="?";
@@ -103,7 +104,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
103104
).addPossibleStatus(RoutingStatus.returnValue(
104105
200, String.format("All the %s", entityDefn.getPlural()))
105106
).setAsFilterableFrom(entityDefn)
106-
.returnPayload(200, entityDefn.getName());
107+
.returnPayload(200, entityDefn.getPlural());
107108

108109
defn.addRouting(String.format("headers for all the instances of %s", entityDefn.getName()),
109110
RoutingVerb.HEAD, pluralUrl, RoutingStatus.returnedFromCall()).
@@ -123,8 +124,11 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
123124
String.format("we should be able to create %s without a %s using the field values in the body of the message. " + maxLimitInformation,
124125
entityDefn.getName(), uniqueIdFieldName.toUpperCase()),
125126
RoutingVerb.POST, pluralUrl, RoutingStatus.returnedFromCall()).
126-
addPossibleStatus(RoutingStatus.returnValue(
127-
201, String.format("Created a %s",entityDefn.getName()))).
127+
addPossibleStatus(
128+
RoutingStatus.returnValue(
129+
201, String.format("Created a %s",entityDefn.getName())
130+
)
131+
).returnPayload(201, entityDefn.getName()).
128132
addPossibleStatus(RoutingStatus.returnValue(
129133
400, String.format("Error when creating a %s", entityDefn.getName())));
130134

@@ -152,6 +156,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
152156
RoutingVerb.GET, aUrlWGuid, RoutingStatus.returnedFromCall()).
153157
addPossibleStatus(RoutingStatus.returnValue(
154158
200, String.format("A specific %s", entityDefn.getName()))).
159+
returnPayload(200, entityDefn.getName()).
155160
addPossibleStatus(RoutingStatus.returnValue(
156161
404, String.format("Could not find a specific %s", entityDefn.getName())));
157162

@@ -170,6 +175,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
170175
RoutingVerb.POST, aUrlWGuid, RoutingStatus.returnedFromCall()).
171176
addPossibleStatus(RoutingStatus.returnValue(
172177
200, String.format("Amended the specific %s", entityDefn.getName()))).
178+
returnPayload(200, entityDefn.getName()).
173179
addPossibleStatus(RoutingStatus.returnValue(
174180
404, String.format("Could not find a specific %s",entityDefn.getName())));;
175181

@@ -185,6 +191,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
185191
RoutingVerb.PUT, aUrlWGuid, RoutingStatus.returnedFromCall()).
186192
addPossibleStatus(RoutingStatus.returnValue(
187193
200, String.format("Replaced the specific %s details", entityDefn.getName()))).
194+
returnPayload(200, entityDefn.getName()).
188195
addPossibleStatus(RoutingStatus.returnValue(
189196
404, String.format("Could not find a specific %s", entityDefn.getName())));;
190197

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

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,12 @@ public OpenAPI swagger(){
6767
Components components = new Components();
6868
for(EntityDefinition objectSchemaDefinition : routingDefinitions.getObjectSchemas()){
6969

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-
70+
ObjectSchema object = asObjectSchema(objectSchemaDefinition);
8271
components.addSchemas(objectSchemaDefinition.getName(), object);
72+
73+
ArraySchema arrayObject = asArrayObjectSchema(objectSchemaDefinition);
74+
components.addSchemas(objectSchemaDefinition.getPlural(), arrayObject);
75+
8376
}
8477

8578
api.components(components);
@@ -122,16 +115,21 @@ public OpenAPI swagger(){
122115
ApiResponse response = new ApiResponse().description(
123116
possibleStatus.description()
124117
);
125-
if(route.hasReturnPayloadFor(possibleStatus.value())){
126-
if(routingDefinitions.hasObjectSchemaNamed(route.getReturnPayloadFor(possibleStatus.value()))){
127-
String ref = "#/components/schemas/" + route.getReturnPayloadFor(possibleStatus.value());
118+
if(subroute.hasReturnPayloadFor(possibleStatus.value())){
119+
// assume that all payloads are setup as components
120+
if(routingDefinitions.hasObjectSchemaNamed(subroute.getReturnPayloadFor(possibleStatus.value()))){
121+
String ref = "#/components/schemas/" + subroute.getReturnPayloadFor(possibleStatus.value());
128122

129123
Schema<String> object = new Schema<>();
130124
MediaType schema = new MediaType();
131125
schema.setSchema(object);
132126
object.set$ref(ref);
127+
133128
response.setContent(
134-
new Content().addMediaType("application/json", schema));
129+
new Content().
130+
addMediaType("application/json", schema).
131+
addMediaType("application/xml", schema)
132+
);
135133
}
136134
}
137135
responses.addApiResponse(
@@ -177,6 +175,47 @@ public OpenAPI swagger(){
177175
return api;
178176
}
179177

178+
private ArraySchema asArrayObjectSchema(EntityDefinition objectSchemaDefinition) {
179+
180+
ArraySchema arrayObject = new ArraySchema();
181+
arrayObject.setDescription(objectSchemaDefinition.getPlural());
182+
arrayObject.setTitle(objectSchemaDefinition.getPlural());
183+
//arrayObject.setItems(asObjectSchema(objectSchemaDefinition));
184+
185+
String ref = "#/components/schemas/" + objectSchemaDefinition.getName();
186+
187+
Schema<String> objectRef = new Schema<>();
188+
objectRef.set$ref(ref);
189+
190+
arrayObject.setItems(objectRef);
191+
192+
XML xml = new XML();
193+
xml.setWrapped(true);
194+
arrayObject.setXml(xml);
195+
196+
return arrayObject;
197+
}
198+
199+
private static ObjectSchema asObjectSchema(EntityDefinition objectSchemaDefinition) {
200+
ObjectSchema object = new ObjectSchema();
201+
object.setDescription(objectSchemaDefinition.getName());
202+
object.setTitle(objectSchemaDefinition.getName());
203+
204+
for(String propertyName : objectSchemaDefinition.getFieldNames()){
205+
Field propertyDefinition = objectSchemaDefinition.getField(propertyName);
206+
Schema<String> propertyItem = new Schema<String>();
207+
propertyItem.setExample(propertyDefinition.getExamples().get(0));
208+
object.addProperties(propertyName, propertyItem);
209+
}
210+
211+
XML xml = new XML();
212+
xml.setWrapped(true);
213+
xml.setName(objectSchemaDefinition.getName());
214+
215+
object.setXml(xml);
216+
return object;
217+
}
218+
180219
// TODO: the output from swaggerizer json could be cached
181220
public String asJson(){
182221
if(api==null){

0 commit comments

Comments
 (0)