Skip to content

Commit b9b0b4b

Browse files
authored
fixes #264 add ignoreInvalidPath flag to OpenApiHandler config (#265)
1 parent ef22d81 commit b9b0b4b

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

openapi-meta/src/main/java/com/networknt/openapi/OpenApiHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
184184
break;
185185
}
186186
}
187-
if(!found) {
187+
if(!found && !config.isIgnoreInvalidPath()) {
188188
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, p);
189189
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
190190
return;
@@ -193,8 +193,13 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
193193
final NormalisedPath requestPath = new ApiNormalisedPath(exchange.getRequestURI(), helper.basePath);
194194
final Optional<NormalisedPath> maybeApiPath = helper.findMatchingApiPath(requestPath);
195195
if (!maybeApiPath.isPresent()) {
196-
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, requestPath.normalised());
197-
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
196+
if(config.isIgnoreInvalidPath()) {
197+
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with ignoreInvalidPath.");
198+
Handler.next(exchange, next);
199+
} else {
200+
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, requestPath.normalised());
201+
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
202+
}
198203
return;
199204
}
200205

openapi-meta/src/main/java/com/networknt/openapi/OpenApiHandlerConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ public class OpenApiHandlerConfig {
1212
private static final Logger logger = LoggerFactory.getLogger(OpenApiHandlerConfig.class);
1313
public static final String CONFIG_NAME = "openapi-handler";
1414
private static final String MULTIPLE_SPEC = "multipleSpec";
15+
private static final String IGNORE_INVALID_PATH = "ignoreInvalidPath";
1516
private static final String PATH_SPEC_MAPPING = "pathSpecMapping";
1617

1718
boolean multipleSpec;
19+
boolean ignoreInvalidPath;
1820
Map<String, Object> pathSpecMapping;
1921

2022
private Config config;
@@ -57,12 +59,16 @@ public Map<String, Object> getMappedConfig() {
5759
public boolean isMultipleSpec() {
5860
return multipleSpec;
5961
}
60-
62+
public boolean isIgnoreInvalidPath() { return ignoreInvalidPath; }
6163
private void setConfigData() {
6264
Object object = mappedConfig.get(MULTIPLE_SPEC);
6365
if (object != null && (Boolean) object) {
6466
multipleSpec = (Boolean)object;
6567
}
68+
object = mappedConfig.get(IGNORE_INVALID_PATH);
69+
if (object != null && (Boolean) object) {
70+
ignoreInvalidPath = (Boolean)object;
71+
}
6672
}
6773

6874
public Map<String, Object> getPathSpecMapping() {

openapi-meta/src/main/resources/config/openapi-handler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# This configuration file is used to support multiple OpenAPI specifications in the same light-rest-4j instance.
33
# An indicator to allow multiple openapi specifications. Default to false which only allow one spec named openapi.yml or openapi.yaml or openapi.json.
44
multipleSpec: ${openapi-handler.multipleSpec:false}
5+
# When the OpenApiHandler is used in a shared gateway and some backend APIs have no specifications deployed on the gateway, the handler will return
6+
# an invalid request path error to the client. To allow the call to pass through the OpenApiHandler and route to the backend APIs, you can set this
7+
# flag to true. In this mode, the handler will only add the endpoint specification to the auditInfo if it can find it. Otherwise, it will pass through.
8+
ignoreInvalidPath: ${openapi-handler.ignoreInvalidPath:false}
59
# Path to spec mapping. One or more base paths can map to the same specifications. The key is the base path and the value is the specification name.
610
# If users want to use multiple specification files in the same instance, each specification must have a unique base path and it must be set as key.
711
pathSpecMapping: ${openapi-handler.pathSpecMapping:}

openapi-meta/src/test/java/com/networknt/openapi/OpenApiHandlerConfigTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ public class OpenApiHandlerConfigTest {
77
@Test
88
public void testLoadConfig() {
99
OpenApiHandlerConfig config = OpenApiHandlerConfig.load();
10-
Assert.assertEquals(2, config.getMappedConfig().size());
10+
Assert.assertEquals(3, config.getMappedConfig().size());
1111
}
1212
}

openapi-meta/src/test/resources/config/openapi-handler-multiple.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# This configuration file is used to support multiple OpenAPI specifications in the same light-rest-4j instance.
33
# An indicator to allow multiple openapi specifications. Default to false which only allow one spec named openapi.yml or openapi.yaml or openapi.json.
44
multipleSpec: ${openapi-handler.multipleSpec:true}
5+
# When the OpenApiHandler is used in a shared gateway and some backend APIs have no specifications deployed on the gateway, the handler will return
6+
# an invalid request path error to the client. To allow the call to pass through the OpenApiHandler and route to the backend APIs, you can set this
7+
# flag to true. In this mode, the handler will only add the endpoint specification to the auditInfo if it can find it. Otherwise, it will pass through.
8+
ignoreInvalidPath: ${openapi-handler.ignoreInvalidPath:false}
59
# Path to spec mapping. One or more base paths can map to the same specifications. The key is the base path and the value is the specification name.
610
# If users want to use multiple specification files in the same instance, each specification must have a unique base path and it must be set as key.
711
pathSpecMapping:

0 commit comments

Comments
 (0)