@@ -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