Skip to content

Commit 536a733

Browse files
authored
fixes #39 handle multiple schemas to support multiple specs in light-rest-4j (#41)
1 parent 4a3b082 commit 536a733

File tree

6 files changed

+47
-65
lines changed

6 files changed

+47
-65
lines changed

src/main/java/com/networknt/openapi/ApiNormalisedPath.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ public class ApiNormalisedPath implements NormalisedPath {
3535
private final String original;
3636
private final String normalised;
3737

38-
@Deprecated
39-
public ApiNormalisedPath(final String path) {
40-
if (logger.isDebugEnabled()) logger.debug("path =" + path);
41-
this.original = requireNonNull(path, "A path is required");
42-
this.normalised = normalise(path, null);
43-
if (logger.isDebugEnabled()) logger.debug("normalised = " + this.normalised);
44-
this.pathParts = List.of(normalised.split("/"));
45-
}
46-
4738
/**
4839
* Construct normalized path
4940
* @param path original path
@@ -98,9 +89,6 @@ public String normalised() {
9889
// case basePath == null, use OpenApiHelper.basePath
9990
// case basePath != null, remove the basePath
10091
private String normalise(String requestPath, String basePath) {
101-
if (basePath == null) {
102-
basePath = OpenApiHelper.openApi3 == null ? "" : OpenApiHelper.basePath;
103-
}
10492
requestPath = requestPath.replaceFirst(basePath, "");
10593

10694
if (!requestPath.startsWith("/")) {

src/main/java/com/networknt/openapi/OpenApiHelper.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,22 @@
3838
* This handler supports openapi.yml, openapi.yaml and openapi.json and above
3939
* is the loading sequence.
4040
*
41+
* Due to the high demand, we have changed this class from singleton to normal
42+
* class to support multiple instances of specifications in light-rest-4j.
43+
*
44+
* Some of our users want to use one light-gateway to support multiple backend
45+
* services to reduce the resource utilization.
46+
*
4147
* @author Steve Hu
4248
*/
4349
public class OpenApiHelper {
4450
static final Logger logger = LoggerFactory.getLogger(OpenApiHelper.class);
4551

46-
public static OpenApi3 openApi3;
47-
public static List<String> oauth2Names;
48-
public static String basePath;
49-
private static OpenApiHelper INSTANCE = null;
52+
public OpenApi3 openApi3;
53+
public List<String> oauth2Names;
54+
public String basePath;
5055

51-
private OpenApiHelper(String spec) {
56+
public OpenApiHelper(String spec) {
5257
try {
5358
openApi3 = (OpenApi3) new OpenApiParser().parse(spec, new URL("https://oas.lightapi.net/"));
5459
} catch (MalformedURLException e) {
@@ -64,25 +69,6 @@ private OpenApiHelper(String spec) {
6469

6570
}
6671

67-
public static OpenApiHelper getInstance() {
68-
if(INSTANCE == null) {
69-
return null;
70-
}
71-
return INSTANCE;
72-
}
73-
74-
public synchronized static OpenApiHelper init(String spec) {
75-
if(INSTANCE != null) {
76-
return INSTANCE;
77-
}
78-
INSTANCE = new OpenApiHelper(spec);
79-
return INSTANCE;
80-
}
81-
82-
public synchronized static OpenApiHelper reset(String spec) {
83-
INSTANCE = new OpenApiHelper(spec);
84-
return INSTANCE;
85-
}
8672

8773
/**
8874
* merge inject map to openapi map
@@ -117,10 +103,10 @@ public Object apply(Object o, Object i) {
117103
}
118104

119105
public Optional<NormalisedPath> findMatchingApiPath(final NormalisedPath requestPath) {
120-
if(OpenApiHelper.openApi3 != null) {
121-
return OpenApiHelper.openApi3.getPaths().keySet()
106+
if(openApi3 != null) {
107+
return openApi3.getPaths().keySet()
122108
.stream()
123-
.map(p -> (NormalisedPath) new ApiNormalisedPath(p))
109+
.map(p -> (NormalisedPath) new ApiNormalisedPath(p, basePath))
124110
.filter(p -> pathMatches(requestPath, p))
125111
.findFirst();
126112
} else {
@@ -165,6 +151,17 @@ private String getBasePath() {
165151
return basePath;
166152
}
167153

154+
/**
155+
* This method should only be called in the multiple specification use case to set the base path
156+
* based on the OpenApiHandler config with the pathSpecMapping. For single specification use case,
157+
* this method should be called to overwrite the basePath from the handler.yml configuration in
158+
* case the openapi.yaml cannot derive the basePath from the server section.
159+
* @param basePath string
160+
*/
161+
public void setBasePath(String basePath) {
162+
this.basePath = basePath;
163+
}
164+
168165
private boolean pathMatches(final NormalisedPath requestPath, final NormalisedPath apiPath) {
169166
if (requestPath.parts().size() != apiPath.parts().size()) {
170167
return false;

src/test/java/com/networknt/openapi/BasePathRewriteTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@
1111
import java.util.Scanner;
1212

1313
public class BasePathRewriteTest {
14+
OpenApiHelper helper = null;
1415
@Before
1516
public void testOAuth2Name() throws Exception {
1617
URL url = Resources.getResource("models/openapi-server-url-rewrite.yaml");
1718
String spec = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
18-
OpenApiHelper.init(spec);
19+
helper = new OpenApiHelper(spec);
1920
}
2021

2122
@After
2223
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
23-
Field instance = OpenApiHelper.class.getDeclaredField("INSTANCE");
24-
instance.setAccessible(true);
25-
instance.set(null, null);
24+
helper = null;
2625
}
2726

2827
@Test
2928
public void testBasePath() {
30-
Assert.assertEquals("/namespace/application/v1", OpenApiHelper.basePath);
29+
Assert.assertEquals("/namespace/application/v1", helper.basePath);
3130
}
3231

3332
@Test

src/test/java/com/networknt/openapi/EndpointTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public class EndpointTest {
1414
public void testJson() throws Exception {
1515
URL url = Resources.getResource("models/petstore.json");
1616
String spec = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
17-
OpenApiHelper.init(spec);
17+
OpenApiHelper helper = new OpenApiHelper(spec);
1818

19-
String basePath = OpenApiHelper.basePath;
19+
String basePath = helper.basePath;
2020
Map<String, Object> endpoints = new HashMap<>();
21-
Map<String, Path> paths = OpenApiHelper.openApi3.getPaths();
21+
Map<String, Path> paths = helper.openApi3.getPaths();
2222
for (Map.Entry<String, Path> pathPair : paths.entrySet()) {
2323
String path = pathPair.getKey();
2424
for (Map.Entry<String, Operation> entries : pathPair.getValue().getOperations().entrySet()) {
@@ -30,7 +30,7 @@ public void testJson() throws Exception {
3030
if(securityRequirements != null) {
3131
for(SecurityRequirement requirement: securityRequirements) {
3232
SecurityParameter securityParameter = null;
33-
for(String oauth2Name: OpenApiHelper.oauth2Names) {
33+
for(String oauth2Name: helper.oauth2Names) {
3434
securityParameter = requirement.getRequirement(oauth2Name);
3535
if(securityParameter != null) break;
3636
}
@@ -52,11 +52,11 @@ public void testJson() throws Exception {
5252
public void testYaml() throws Exception {
5353
URL url = Resources.getResource("models/petstore.yaml");
5454
String spec = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
55-
OpenApiHelper.init(spec);
55+
OpenApiHelper helper = new OpenApiHelper(spec);
5656

57-
String basePath = OpenApiHelper.basePath;
57+
String basePath = helper.basePath;
5858
Map<String, Object> endpoints = new HashMap<>();
59-
Map<String, Path> paths = OpenApiHelper.openApi3.getPaths();
59+
Map<String, Path> paths = helper.openApi3.getPaths();
6060
for (Map.Entry<String, Path> pathPair : paths.entrySet()) {
6161
String path = pathPair.getKey();
6262
for (Map.Entry<String, Operation> entries : pathPair.getValue().getOperations().entrySet()) {
@@ -68,7 +68,7 @@ public void testYaml() throws Exception {
6868
if(securityRequirements != null) {
6969
for(SecurityRequirement requirement: securityRequirements) {
7070
SecurityParameter securityParameter = null;
71-
for(String oauth2Name: OpenApiHelper.oauth2Names) {
71+
for(String oauth2Name: helper.oauth2Names) {
7272
securityParameter = requirement.getRequirement(oauth2Name);
7373
if(securityParameter != null) break;
7474
}

src/test/java/com/networknt/openapi/OpenApiHelperTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,24 @@
3232
* Created by steve on 23/09/16.
3333
*/
3434
public class OpenApiHelperTest {
35+
OpenApiHelper helper = null;
3536
@Before
3637
public void testOAuth2Name() throws Exception {
3738
URL url = Resources.getResource("models/petstore.yaml");
3839
String spec = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
39-
OpenApiHelper.init(spec);
40-
Assert.assertEquals(1, OpenApiHelper.oauth2Names.size());
41-
Assert.assertEquals("petstore_auth", OpenApiHelper.oauth2Names.get(0));
40+
helper = new OpenApiHelper(spec);
41+
Assert.assertEquals(1, helper.oauth2Names.size());
42+
Assert.assertEquals("petstore_auth", helper.oauth2Names.get(0));
4243
}
4344

4445
@After
4546
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
46-
Field instance = OpenApiHelper.class.getDeclaredField("INSTANCE");
47-
instance.setAccessible(true);
48-
instance.set(null, null);
47+
helper = null;
4948
}
5049

5150
@Test
5251
public void testBasePath() {
53-
Assert.assertEquals("/v1", OpenApiHelper.basePath);
52+
Assert.assertEquals("/v1", helper.basePath);
5453
}
5554

5655
@Test

src/test/java/com/networknt/openapi/RelativeBasePathTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,27 @@
1111
import java.util.Scanner;
1212

1313
public class RelativeBasePathTest {
14+
OpenApiHelper helper = null;
1415
@Before
1516
public void testOAuth2Name() throws Exception {
1617
URL url = Resources.getResource("models/openapi-relative-server-url.yaml");
1718
String spec = new Scanner(url.openStream(), "UTF-8").useDelimiter("\\A").next();
18-
OpenApiHelper.init(spec);
19+
helper = new OpenApiHelper(spec);
1920
}
2021

2122
@After
2223
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
23-
Field instance = OpenApiHelper.class.getDeclaredField("INSTANCE");
24-
instance.setAccessible(true);
25-
instance.set(null, null);
24+
helper = null;
2625
}
2726

2827
@Test
2928
public void testBasePath() {
30-
Assert.assertEquals("/v1", OpenApiHelper.basePath);
29+
Assert.assertEquals("/v1", helper.basePath);
3130
}
3231

3332
@Test
3433
public void testApiNormalized() {
35-
ApiNormalisedPath normalisedPath = new ApiNormalisedPath("/v1/pets/26", null);
34+
ApiNormalisedPath normalisedPath = new ApiNormalisedPath("/v1/pets/26", helper.basePath);
3635
Assert.assertEquals("/pets/26", normalisedPath.normalised());
3736
}
3837
}

0 commit comments

Comments
 (0)