9
9
*/
10
10
package org .openmrs .module .webservices .docs .swagger ;
11
11
12
- import io .swagger .annotations .SwaggerDefinition ;
13
12
import io .swagger .v3 .oas .models .OpenAPI ;
14
13
import io .swagger .v3 .oas .models .Operation ;
15
14
import io .swagger .v3 .oas .models .info .Info ;
@@ -77,11 +76,13 @@ public class SwaggerSpecificationCreator {
77
76
78
77
private static OpenAPI openAPI ;
79
78
79
+ private static String cachedJson ;
80
+
80
81
private String host ;
81
82
82
83
private String basePath ;
83
84
84
- private List <SwaggerDefinition . Scheme > schemes ;
85
+ private List <Scheme > schemes ;
85
86
86
87
private String baseUrl ;
87
88
@@ -114,22 +115,37 @@ public SwaggerSpecificationCreator basePath(String basePath) {
114
115
return this ;
115
116
}
116
117
117
- public SwaggerSpecificationCreator scheme (SwaggerDefinition . Scheme scheme ) {
118
+ public SwaggerSpecificationCreator scheme (Scheme scheme ) {
118
119
if (schemes == null ) {
119
- this .schemes = new ArrayList <SwaggerDefinition . Scheme >();
120
+ this .schemes = new ArrayList <Scheme >();
120
121
}
121
122
if (!schemes .contains (scheme )) {
122
123
this .schemes .add (scheme );
123
124
}
124
125
return this ;
125
126
}
126
127
128
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Scheme type for API communication" )
129
+ public enum Scheme {
130
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Hypertext Transfer Protocol" )
131
+ HTTP ,
132
+
133
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "Hypertext Transfer Protocol Secure" )
134
+ HTTPS ,
135
+
136
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "WebSocket Protocol" )
137
+ WS ,
138
+
139
+ @ io .swagger .v3 .oas .annotations .media .Schema (description = "WebSocket Secure Protocol" )
140
+ WSS
141
+ }
142
+
127
143
/**
128
144
* Regenerate the swagger spec from scratch
129
145
*/
130
146
private void BuildJSON () {
131
147
log .info ("Initiating Swagger specification creation" );
132
- toggleLogs (RestConstants .SWAGGER_LOGS_OFF );
148
+ toggleLogs (SwaggerConstants .SWAGGER_LOGS_OFF );
133
149
try {
134
150
initOpenAPI ();
135
151
addPaths ();
@@ -140,19 +156,20 @@ private void BuildJSON() {
140
156
log .error ("Error while creating Swagger specification" , e );
141
157
}
142
158
finally {
143
- toggleLogs (RestConstants .SWAGGER_LOGS_ON );
159
+ toggleLogs (SwaggerConstants .SWAGGER_LOGS_ON );
144
160
}
145
161
}
146
162
147
163
public String getJSON () {
148
- if (isCached ()) {
164
+ if (isCached () && cachedJson != null ) {
149
165
log .info ("Returning a cached copy of Swagger specification" );
150
- initOpenAPI ();
151
- } else {
152
- openAPI = new OpenAPI ();
153
- BuildJSON ();
166
+ return cachedJson ;
154
167
}
155
- return createJSON ();
168
+
169
+ openAPI = new OpenAPI ();
170
+ BuildJSON ();
171
+ cachedJson = createJSON (); // Cache the JSON string
172
+ return cachedJson ;
156
173
}
157
174
158
175
private void addDefaultDefinitions () {
@@ -173,9 +190,9 @@ private void addDefaultDefinitions() {
173
190
}
174
191
175
192
private void toggleLogs (boolean targetState ) {
176
- if (Context .getAdministrationService ().getGlobalProperty (RestConstants .SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME )
193
+ if (Context .getAdministrationService ().getGlobalProperty (SwaggerConstants .SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME )
177
194
.equals ("true" )) {
178
- if (targetState == RestConstants .SWAGGER_LOGS_OFF ) {
195
+ if (targetState == SwaggerConstants .SWAGGER_LOGS_OFF ) {
179
196
// turn off the log4j loggers
180
197
List <Logger > loggers = Collections .<Logger > list (LogManager .getCurrentLoggers ());
181
198
loggers .add (LogManager .getRootLogger ());
@@ -200,7 +217,7 @@ public void write(int b) {
200
217
// noop
201
218
}
202
219
}));
203
- } else if (targetState == RestConstants .SWAGGER_LOGS_ON ) {
220
+ } else if (targetState == SwaggerConstants .SWAGGER_LOGS_ON ) {
204
221
List <Logger > loggers = Collections .<Logger > list (LogManager .getCurrentLoggers ());
205
222
loggers .add (LogManager .getRootLogger ());
206
223
for (Logger logger : loggers ) {
@@ -222,6 +239,7 @@ private void initOpenAPI() {
222
239
.license (new License ().name ("MPL-2.0 w/ HD" ).url ("http://openmrs.org/license" ));
223
240
224
241
openAPI = new OpenAPI ()
242
+ .openapi ("3.0.0" )
225
243
.info (info )
226
244
.addServersItem (new Server ().url (this .host + this .basePath ))
227
245
.components (new Components ().addSecuritySchemes ("basic_auth" , new SecurityScheme ().type (SecurityScheme .Type .HTTP ).scheme ("basic" )))
@@ -253,7 +271,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
253
271
if (method == null ) {
254
272
return false ;
255
273
} else {
256
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
274
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
257
275
}
258
276
259
277
break ;
@@ -264,7 +282,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
264
282
if (method == null ) {
265
283
return false ;
266
284
} else {
267
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
285
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
268
286
}
269
287
270
288
break ;
@@ -275,7 +293,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
275
293
if (method == null ) {
276
294
return false ;
277
295
} else {
278
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID );
296
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID );
279
297
}
280
298
281
299
break ;
@@ -320,7 +338,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
320
338
} else {
321
339
try {
322
340
// to avoid saving data to the database, we pass a null SimpleObject
323
- method .invoke (resourceHandler , null , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
341
+ method .invoke (resourceHandler , null , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
324
342
new RequestContext ());
325
343
}
326
344
catch (InvocationTargetException e ) {
@@ -344,7 +362,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
344
362
if (method == null ) {
345
363
return false ;
346
364
} else {
347
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
365
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
348
366
buildPOSTUpdateSimpleObject (resourceHandler ), new RequestContext ());
349
367
}
350
368
@@ -356,8 +374,8 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
356
374
if (method == null ) {
357
375
return false ;
358
376
} else {
359
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
360
- RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , buildPOSTUpdateSimpleObject (resourceHandler ),
377
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
378
+ SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , buildPOSTUpdateSimpleObject (resourceHandler ),
361
379
new RequestContext ());
362
380
}
363
381
@@ -369,7 +387,7 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
369
387
if (method == null ) {
370
388
return false ;
371
389
} else {
372
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , "" ,
390
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , "" ,
373
391
new RequestContext ());
374
392
}
375
393
@@ -381,8 +399,8 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
381
399
if (method == null ) {
382
400
return false ;
383
401
} else {
384
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
385
- RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , "" , new RequestContext ());
402
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
403
+ SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , "" , new RequestContext ());
386
404
}
387
405
break ;
388
406
case purge :
@@ -392,7 +410,7 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
392
410
if (method == null ) {
393
411
return false ;
394
412
} else {
395
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
413
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
396
414
}
397
415
398
416
break ;
@@ -403,8 +421,8 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
403
421
if (method == null ) {
404
422
return false ;
405
423
} else {
406
- method .invoke (resourceHandler , RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
407
- RestConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
424
+ method .invoke (resourceHandler , SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID ,
425
+ SwaggerConstants .SWAGGER_IMPOSSIBLE_UNIQUE_ID , new RequestContext ());
408
426
}
409
427
}
410
428
return true ;
@@ -1234,11 +1252,14 @@ public OpenAPI getOpenAPI() {
1234
1252
* @return true if and only if openAPI is not null, and its paths are also set.
1235
1253
*/
1236
1254
public static boolean isCached () {
1237
- return openAPI != null && openAPI .getPaths () != null ;
1255
+ return openAPI != null &&
1256
+ openAPI .getPaths () != null &&
1257
+ !openAPI .getPaths ().isEmpty ();
1238
1258
}
1239
1259
1240
1260
public static void clearCache () {
1241
1261
openAPI = null ;
1262
+ cachedJson = null ;
1242
1263
}
1243
1264
1244
1265
}
0 commit comments