Skip to content

Commit 2bfc0b2

Browse files
committed
Ensure proper caching of the documentation and ensure the spec works alongside openmrs-core
1 parent 19d5975 commit 2bfc0b2

File tree

6 files changed

+84
-60
lines changed

6 files changed

+84
-60
lines changed

omod-2.0/src/test/java/org/openmrs/module/webservices/rest/doc/SwaggerSpecificationCreatorTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.swagger.v3.oas.models.PathItem;
1818
import io.swagger.v3.oas.models.info.Info;
1919
import io.swagger.v3.oas.models.media.Schema;
20-
import io.swagger.v3.oas.models.parameters.Parameter;
2120
import io.swagger.v3.oas.models.security.SecurityRequirement;
2221
import io.swagger.v3.oas.models.security.SecurityScheme;
2322
import org.dbunit.database.DatabaseConnection;
@@ -28,15 +27,10 @@
2827
import org.openmrs.Patient;
2928
import org.openmrs.api.context.Context;
3029
import org.openmrs.module.unrelatedtest.rest.resource.UnrelatedGenericChildResource;
30+
import org.openmrs.module.webservices.docs.swagger.SwaggerConstants;
3131
import org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator;
32-
import org.openmrs.module.webservices.rest.web.RestConstants;
3332
import org.openmrs.module.webservices.rest.web.api.RestService;
3433
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
35-
import org.apache.log4j.Logger;
36-
import org.apache.log4j.Level;
37-
import org.apache.log4j.PatternLayout;
38-
import org.apache.log4j.WriterAppender;
39-
import java.io.StringWriter;
4034

4135
import java.lang.reflect.Field;
4236
import java.lang.reflect.InvocationTargetException;
@@ -48,7 +42,6 @@
4842
import java.util.HashMap;
4943
import java.util.List;
5044
import java.util.Map;
51-
import java.util.stream.Collectors;
5245

5346
import static junit.framework.TestCase.assertFalse;
5447
import static junit.framework.TestCase.assertNotNull;
@@ -132,7 +125,7 @@ public void init() throws Exception {
132125
Context.getService(RestService.class).initialize();
133126

134127
Context.getAdministrationService().saveGlobalProperty(
135-
new GlobalProperty(RestConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true"));
128+
new GlobalProperty(SwaggerConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true"));
136129

137130
// ensure GP is written to database before we count the rows
138131
Context.flushSession();

omod-common/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
</exclusions>
4747
</dependency>
4848
<dependency>
49-
<groupId>io.swagger</groupId>
49+
<groupId>io.swagger.core.v3</groupId>
5050
<artifactId>swagger-annotations</artifactId>
51-
<version>1.6.14</version>
51+
<version>2.2.23</version>
5252
<scope>compile</scope>
5353
</dependency>
5454
</dependencies>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.docs.swagger;
11+
12+
import static org.openmrs.module.webservices.rest.web.RestConstants.MODULE_ID;
13+
14+
/**
15+
* Constants used when generating the Swagger specification
16+
*/
17+
public class SwaggerConstants {
18+
19+
public static String SWAGGER_IMPOSSIBLE_UNIQUE_ID = "a--b";
20+
21+
public static String SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME = MODULE_ID + ".quietDocs";
22+
23+
public static final boolean SWAGGER_LOGS_ON = true;
24+
25+
public static final boolean SWAGGER_LOGS_OFF = false;
26+
27+
}

omod-common/src/main/java/org/openmrs/module/webservices/docs/swagger/SwaggerSpecificationCreator.java

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
package org.openmrs.module.webservices.docs.swagger;
1111

12-
import io.swagger.annotations.SwaggerDefinition;
1312
import io.swagger.v3.oas.models.OpenAPI;
1413
import io.swagger.v3.oas.models.Operation;
1514
import io.swagger.v3.oas.models.info.Info;
@@ -77,11 +76,13 @@ public class SwaggerSpecificationCreator {
7776

7877
private static OpenAPI openAPI;
7978

79+
private static String cachedJson;
80+
8081
private String host;
8182

8283
private String basePath;
8384

84-
private List<SwaggerDefinition.Scheme> schemes;
85+
private List<Scheme> schemes;
8586

8687
private String baseUrl;
8788

@@ -114,22 +115,37 @@ public SwaggerSpecificationCreator basePath(String basePath) {
114115
return this;
115116
}
116117

117-
public SwaggerSpecificationCreator scheme(SwaggerDefinition.Scheme scheme) {
118+
public SwaggerSpecificationCreator scheme(Scheme scheme) {
118119
if (schemes == null) {
119-
this.schemes = new ArrayList<SwaggerDefinition.Scheme>();
120+
this.schemes = new ArrayList<Scheme>();
120121
}
121122
if (!schemes.contains(scheme)) {
122123
this.schemes.add(scheme);
123124
}
124125
return this;
125126
}
126127

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+
127143
/**
128144
* Regenerate the swagger spec from scratch
129145
*/
130146
private void BuildJSON() {
131147
log.info("Initiating Swagger specification creation");
132-
toggleLogs(RestConstants.SWAGGER_LOGS_OFF);
148+
toggleLogs(SwaggerConstants.SWAGGER_LOGS_OFF);
133149
try {
134150
initOpenAPI();
135151
addPaths();
@@ -140,19 +156,20 @@ private void BuildJSON() {
140156
log.error("Error while creating Swagger specification", e);
141157
}
142158
finally {
143-
toggleLogs(RestConstants.SWAGGER_LOGS_ON);
159+
toggleLogs(SwaggerConstants.SWAGGER_LOGS_ON);
144160
}
145161
}
146162

147163
public String getJSON() {
148-
if (isCached()) {
164+
if (isCached() && cachedJson != null) {
149165
log.info("Returning a cached copy of Swagger specification");
150-
initOpenAPI();
151-
} else {
152-
openAPI = new OpenAPI();
153-
BuildJSON();
166+
return cachedJson;
154167
}
155-
return createJSON();
168+
169+
openAPI = new OpenAPI();
170+
BuildJSON();
171+
cachedJson = createJSON(); // Cache the JSON string
172+
return cachedJson;
156173
}
157174

158175
private void addDefaultDefinitions() {
@@ -173,9 +190,9 @@ private void addDefaultDefinitions() {
173190
}
174191

175192
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)
177194
.equals("true")) {
178-
if (targetState == RestConstants.SWAGGER_LOGS_OFF) {
195+
if (targetState == SwaggerConstants.SWAGGER_LOGS_OFF) {
179196
// turn off the log4j loggers
180197
List<Logger> loggers = Collections.<Logger> list(LogManager.getCurrentLoggers());
181198
loggers.add(LogManager.getRootLogger());
@@ -200,7 +217,7 @@ public void write(int b) {
200217
// noop
201218
}
202219
}));
203-
} else if (targetState == RestConstants.SWAGGER_LOGS_ON) {
220+
} else if (targetState == SwaggerConstants.SWAGGER_LOGS_ON) {
204221
List<Logger> loggers = Collections.<Logger> list(LogManager.getCurrentLoggers());
205222
loggers.add(LogManager.getRootLogger());
206223
for (Logger logger : loggers) {
@@ -222,6 +239,7 @@ private void initOpenAPI() {
222239
.license(new License().name("MPL-2.0 w/ HD").url("http://openmrs.org/license"));
223240

224241
openAPI = new OpenAPI()
242+
.openapi("3.0.0")
225243
.info(info)
226244
.addServersItem(new Server().url(this.host + this.basePath))
227245
.components(new Components().addSecuritySchemes("basic_auth", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
@@ -253,7 +271,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
253271
if (method == null) {
254272
return false;
255273
} else {
256-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
274+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
257275
}
258276

259277
break;
@@ -264,7 +282,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
264282
if (method == null) {
265283
return false;
266284
} else {
267-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
285+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
268286
}
269287

270288
break;
@@ -275,7 +293,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
275293
if (method == null) {
276294
return false;
277295
} else {
278-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID);
296+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID);
279297
}
280298

281299
break;
@@ -320,7 +338,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
320338
} else {
321339
try {
322340
// 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,
324342
new RequestContext());
325343
}
326344
catch (InvocationTargetException e) {
@@ -344,7 +362,7 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
344362
if (method == null) {
345363
return false;
346364
} else {
347-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
365+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID,
348366
buildPOSTUpdateSimpleObject(resourceHandler), new RequestContext());
349367
}
350368

@@ -356,8 +374,8 @@ private boolean testOperationImplemented(OperationEnum operation, DelegatingReso
356374
if (method == null) {
357375
return false;
358376
} 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),
361379
new RequestContext());
362380
}
363381

@@ -369,7 +387,7 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
369387
if (method == null) {
370388
return false;
371389
} else {
372-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, "",
390+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, "",
373391
new RequestContext());
374392
}
375393

@@ -381,8 +399,8 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
381399
if (method == null) {
382400
return false;
383401
} 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());
386404
}
387405
break;
388406
case purge:
@@ -392,7 +410,7 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
392410
if (method == null) {
393411
return false;
394412
} else {
395-
method.invoke(resourceHandler, RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
413+
method.invoke(resourceHandler, SwaggerConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, new RequestContext());
396414
}
397415

398416
break;
@@ -403,8 +421,8 @@ RestConstants.SWAGGER_IMPOSSIBLE_UNIQUE_ID, buildPOSTUpdateSimpleObject(resource
403421
if (method == null) {
404422
return false;
405423
} 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());
408426
}
409427
}
410428
return true;
@@ -1234,11 +1252,14 @@ public OpenAPI getOpenAPI() {
12341252
* @return true if and only if openAPI is not null, and its paths are also set.
12351253
*/
12361254
public static boolean isCached() {
1237-
return openAPI != null && openAPI.getPaths() != null;
1255+
return openAPI != null &&
1256+
openAPI.getPaths() != null &&
1257+
!openAPI.getPaths().isEmpty();
12381258
}
12391259

12401260
public static void clearCache() {
12411261
openAPI = null;
1262+
cachedJson = null;
12421263
}
12431264

12441265
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,6 @@ public class RestConstants {
190190
SPECIAL_REQUEST_PARAMETERS.add(REQUEST_PROPERTY_FOR_SEARCH_ID);
191191
SPECIAL_REQUEST_PARAMETERS.add(REQUEST_PROPERTY_FOR_TYPE);
192192
}
193-
194-
/**
195-
* Constants used when generating the Swagger specification
196-
*/
197-
public static String SWAGGER_IMPOSSIBLE_UNIQUE_ID = "a--b";
198-
199-
public static String SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME = MODULE_ID + ".quietDocs";
200-
201-
public static boolean SWAGGER_LOGS_ON = true;
202-
203-
public static boolean SWAGGER_LOGS_OFF = false;
204193

205194
/**
206195
* Constants used for the Server Log REST Service privilege checking

pom.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@
125125
</exclusions>
126126
</dependency>
127127
<dependency>
128-
<groupId>io.swagger</groupId>
128+
<groupId>io.swagger.core.v3</groupId>
129129
<artifactId>swagger-annotations</artifactId>
130-
<version>1.6.14</version>
130+
<version>2.2.23</version>
131131
<scope>provided</scope>
132132
</dependency>
133-
133+
134134
<dependency>
135135
<groupId>com.fasterxml.jackson.core</groupId>
136136
<artifactId>jackson-core</artifactId>
@@ -155,12 +155,6 @@
155155
<scope>provided</scope>
156156
<version>2.18.0</version>
157157
</dependency>
158-
<dependency>
159-
<groupId>com.fasterxml.jackson.dataformat</groupId>
160-
<artifactId>jackson-dataformat-yaml</artifactId>
161-
<scope>provided</scope>
162-
<version>2.18.0</version>
163-
</dependency>
164158
</dependencies>
165159
</dependencyManagement>
166160

0 commit comments

Comments
 (0)