Skip to content

Commit 729dcbd

Browse files
committed
custom header experiment, now push to correct place
1 parent 578a708 commit 729dcbd

File tree

4 files changed

+151
-84
lines changed

4 files changed

+151
-84
lines changed

challenger/src/main/java/uk/co/compendiumdev/challenge/challengesrouting/ChallengerTrackingRoutes.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ public void configure(final Challengers challengers,
237237
RoutingStatus.returnedFromCall(),
238238
null).
239239
addDocumentation("Create a challenger using the X-CHALLENGER guid header.").
240-
addPossibleStatuses(200,400,405));
240+
addPossibleStatuses(200,400,405).
241+
addCustomHeader("X-CHALLENGER","guid")
242+
);
241243

242244
SimpleSparkRouteCreator.routeStatusWhenNot(405, "/challenger", List.of("post", "options"));
243245

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

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

3+
import uk.co.compendiumdev.thingifier.api.ThingifierRestAPIHandler;
34
import uk.co.compendiumdev.thingifier.api.response.ResponseHeader;
45
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition;
56
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field;
67

78
import java.util.ArrayList;
9+
import java.util.Collection;
810
import java.util.HashMap;
911
import java.util.List;
1012

@@ -20,6 +22,7 @@ public class RoutingDefinition {
2022
private HashMap<Integer,String> returnPayload;
2123
private String requestPayload;
2224
private List<Field> requestUrlParams;
25+
private HashMap<String,String> customHeaders;
2326

2427
public RoutingDefinition(RoutingVerb verb, String url, RoutingStatus routingStatus, ResponseHeader header) {
2528
this.verb = verb;
@@ -36,8 +39,9 @@ public RoutingDefinition(RoutingVerb verb, String url, RoutingStatus routingStat
3639
filterableEntityDefn=null;
3740
possibleStatusResponses= new ArrayList<>();
3841
requestUrlParams = new ArrayList<>();
39-
returnPayload=new HashMap<Integer, String>();
42+
returnPayload=new HashMap<>();
4043
requestPayload=null;
44+
customHeaders = new HashMap<>();
4145
}
4246

4347
private List<RoutingStatus> getDefaultPossibleStatusResponses() {
@@ -157,4 +161,20 @@ public List<Field> getRequestUrlParams(){
157161
return new ArrayList<>(requestUrlParams);
158162
}
159163

164+
public RoutingDefinition addCustomHeader(String headerName, String headerType) {
165+
customHeaders.put(headerName,headerType);
166+
return this;
167+
}
168+
169+
public boolean hasCustomHeaders() {
170+
return !customHeaders.keySet().isEmpty();
171+
}
172+
173+
public Collection<String> getCustomHeaderNames() {
174+
return customHeaders.keySet();
175+
}
176+
177+
public String getCustomHeaderType(String headerName) {
178+
return customHeaders.get(headerName);
179+
}
160180
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public String getPathPrefix() {
104104
return pathPrefix;
105105
}
106106

107+
107108
public class ApiServer{
108109
public final String url;
109110
public final String description;

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

Lines changed: 126 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,31 @@ public OpenAPI swagger(){
117117

118118
final Operation operation = new Operation();
119119
operation.setDescription(subroute.getDocumentation());
120+
121+
List<Parameter> operationParameters = new ArrayList<>();
122+
123+
120124
// TODO: need to build up examples and status in the automated route generation
121125
if(!subroute.status().isReturnedFromCall()){
126+
122127
operation.setResponses(
123128
new ApiResponses().addApiResponse(
124129
String.valueOf(subroute.status().value()),
125130
new ApiResponse().description(
126131
subroute.status().description())
127132
));
128-
}else{
133+
134+
}else {
129135
final ApiResponses responses = new ApiResponses();
130-
final List<RoutingStatus> possibleStatusResponses = subroute.getPossibleStatusReponses();
131-
for(RoutingStatus possibleStatus : possibleStatusResponses){
136+
List<RoutingStatus> possibleStatusResponses = subroute.getPossibleStatusReponses();
137+
for (RoutingStatus possibleStatus : possibleStatusResponses) {
132138

133139
ApiResponse response = new ApiResponse().description(
134140
possibleStatus.description()
135141
);
136-
if(subroute.hasReturnPayloadFor(possibleStatus.value())){
142+
if (subroute.hasReturnPayloadFor(possibleStatus.value())) {
137143
// assume that all payloads are setup as components
138-
if(routingDefinitions.hasObjectSchemaNamed(subroute.getReturnPayloadFor(possibleStatus.value()))){
144+
if (routingDefinitions.hasObjectSchemaNamed(subroute.getReturnPayloadFor(possibleStatus.value()))) {
139145
String ref = "#/components/schemas/" + subroute.getReturnPayloadFor(possibleStatus.value());
140146

141147
Schema<String> object = new Schema<>();
@@ -144,106 +150,116 @@ public OpenAPI swagger(){
144150
object.set$ref(ref);
145151

146152
response.setContent(
147-
new Content().
148-
addMediaType("application/json", schema).
149-
addMediaType("application/xml", schema)
153+
new Content().
154+
addMediaType("application/json", schema).
155+
addMediaType("application/xml", schema)
150156
);
151157
}
152158
}
153159

154160
responses.addApiResponse(
155-
String.valueOf(possibleStatus.value()),
156-
response
161+
String.valueOf(possibleStatus.value()),
162+
response
157163
);
158164

159165

160-
if(subroute.hasRequestPayload()){
166+
}
167+
168+
if(!possibleStatusResponses.isEmpty()){
169+
operation.setResponses(responses);
170+
}
171+
}
161172

162-
RequestBody requestBody = new RequestBody();
163-
requestBody.setRequired(true);
173+
if(subroute.hasRequestPayload()){
164174

165-
// assume that all payloads are already setup as components
166-
String ref = "#/components/schemas/" + subroute.getRequestPayload();
175+
RequestBody requestBody = new RequestBody();
176+
requestBody.setRequired(true);
167177

168-
Schema<String> object = new Schema<>();
169-
MediaType schema = new MediaType();
170-
schema.setSchema(object);
171-
object.set$ref(ref);
178+
// assume that all payloads are already setup as components
179+
String ref = "#/components/schemas/" + subroute.getRequestPayload();
172180

173-
requestBody.setContent(
174-
new Content().
175-
addMediaType("application/json", schema).
176-
addMediaType("application/xml", schema)
177-
);
181+
Schema<String> object = new Schema<>();
182+
MediaType schema = new MediaType();
183+
schema.setSchema(object);
184+
object.set$ref(ref);
178185

179-
operation.setRequestBody(requestBody);
180-
}
186+
requestBody.setContent(
187+
new Content().
188+
addMediaType("application/json", schema).
189+
addMediaType("application/xml", schema)
190+
);
181191

182-
if(subroute.hasRequestUrlParams()){
183-
184-
List<Parameter> urlParameters = new ArrayList<>();
185-
186-
// TODO: create a Field to Swaggerizer param method/class
187-
List<Field> paramFields = subroute.getRequestUrlParams();
188-
for(Field aField : paramFields){
189-
Parameter param = new Parameter();
190-
param.
191-
in("path").
192-
name(aField.getName()).
193-
required(true).
194-
example(aField.getRandomExampleValue());
195-
196-
Schema<String> schema = new Schema<>();
197-
198-
switch (aField.getType()){
199-
case AUTO_INCREMENT:
200-
case INTEGER:
201-
schema.addType("integer");
202-
break;
203-
204-
case FLOAT:
205-
schema.addType("number");
206-
break;
207-
case BOOLEAN:
208-
schema.addType("boolean");
209-
break;
210-
case AUTO_GUID:
211-
case STRING:
212-
case DATE:
213-
case ENUM: // TODO: properly do Enums
214-
schema.addType("string");
215-
break;
216-
default:
217-
schema.addType("string");
218-
}
192+
operation.setRequestBody(requestBody);
193+
}
219194

220-
param.setSchema(schema);
221-
urlParameters.add(param);
222-
}
195+
if(subroute.hasRequestUrlParams()) {
196+
197+
List<Parameter> urlParameters = new ArrayList<>();
198+
199+
// TODO: create a Field to Swaggerizer param method/class
200+
List<Field> paramFields = subroute.getRequestUrlParams();
201+
for (Field aField : paramFields) {
202+
Parameter param = new Parameter();
203+
param.
204+
in("path").
205+
name(aField.getName()).
206+
required(true).
207+
example(aField.getRandomExampleValue());
208+
209+
Schema<String> schema = new Schema<>();
210+
211+
switch (aField.getType()) {
212+
case AUTO_INCREMENT:
213+
case INTEGER:
214+
schema.addType("integer");
215+
break;
216+
217+
case FLOAT:
218+
schema.addType("number");
219+
break;
220+
case BOOLEAN:
221+
schema.addType("boolean");
222+
break;
223+
case AUTO_GUID:
224+
case STRING:
225+
case DATE:
226+
case ENUM: // TODO: properly do Enums
227+
schema.addType("string");
228+
break;
229+
default:
230+
schema.addType("string");
231+
}
223232

224-
for(Parameter param : urlParameters){
225-
Boolean exists = false;
226-
if(path.getParameters()!=null){
227-
for(Parameter existingParam : path.getParameters()){
228-
if(existingParam.getName().equals(param.getName())){
229-
exists = true;
230-
}
231-
}
232-
}
233-
if(!exists) {
234-
path.addParametersItem(param);
233+
param.setSchema(schema);
234+
urlParameters.add(param);
235+
}
236+
237+
for(Parameter param : urlParameters){
238+
Boolean exists = false;
239+
if(path.getParameters()!=null){
240+
for(Parameter existingParam : path.getParameters()){
241+
if(existingParam.getName().equals(param.getName())){
242+
exists = true;
235243
}
236244
}
237-
//operation.setParameters(urlParameters);
238245
}
246+
if(!exists) {
247+
path.addParametersItem(param);
248+
}
249+
}
250+
}
239251

240252

241-
}
242-
if(possibleStatusResponses.size()>0){
243-
operation.setResponses(responses);
244-
}
253+
addRouteCustomHeaders(subroute, operationParameters);
254+
255+
256+
257+
258+
if(!operationParameters.isEmpty()){
259+
operation.setParameters(operationParameters);
245260
}
246261

262+
247263
switch(subroute.verb()){
248264
case GET:
249265
path.setGet(operation);
@@ -277,6 +293,34 @@ public OpenAPI swagger(){
277293
return api;
278294
}
279295

296+
private void addRouteCustomHeaders(RoutingDefinition subroute, List<Parameter> operationParameters) {
297+
if(subroute.hasCustomHeaders()){
298+
for(String headerName : subroute.getCustomHeaderNames()){
299+
String headerType = subroute.getCustomHeaderType(headerName);
300+
if(headerType != null){
301+
302+
Parameter param = new Parameter();
303+
param.
304+
in("header").
305+
name(headerName).
306+
required(true);
307+
308+
Schema<String> schema = new Schema<>();
309+
310+
switch (headerType){
311+
case "guid":
312+
break;
313+
default:
314+
schema.addType(headerType);
315+
}
316+
317+
param.setSchema(schema);
318+
operationParameters.add(param);
319+
}
320+
}
321+
}
322+
}
323+
280324
private ArraySchema asArrayObjectSchema(EntityDefinition objectSchemaDefinition) {
281325

282326
ArraySchema arrayObject = new ArraySchema();

0 commit comments

Comments
 (0)