Skip to content

Commit

Permalink
fixes #264 add ignoreInvalidPath flag to OpenApiHandler config
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Dec 11, 2022
1 parent ef22d81 commit 038b297
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
break;
}
}
if(!found) {
if(!found && !config.isIgnoreInvalidPath()) {
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, p);
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
return;
Expand All @@ -193,8 +193,13 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
final NormalisedPath requestPath = new ApiNormalisedPath(exchange.getRequestURI(), helper.basePath);
final Optional<NormalisedPath> maybeApiPath = helper.findMatchingApiPath(requestPath);
if (!maybeApiPath.isPresent()) {
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, requestPath.normalised());
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
if(config.isIgnoreInvalidPath()) {
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with ignoreInvalidPath.");
Handler.next(exchange, next);
} else {
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH, requestPath.normalised());
if (logger.isDebugEnabled()) logger.debug("OpenApiHandler.handleRequest ends with an error.");
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public class OpenApiHandlerConfig {
private static final Logger logger = LoggerFactory.getLogger(OpenApiHandlerConfig.class);
public static final String CONFIG_NAME = "openapi-handler";
private static final String MULTIPLE_SPEC = "multipleSpec";
private static final String IGNORE_INVALID_PATH = "ignoreInvalidPath";
private static final String PATH_SPEC_MAPPING = "pathSpecMapping";

boolean multipleSpec;
boolean ignoreInvalidPath;
Map<String, Object> pathSpecMapping;

private Config config;
Expand Down Expand Up @@ -57,12 +59,16 @@ public Map<String, Object> getMappedConfig() {
public boolean isMultipleSpec() {
return multipleSpec;
}

public boolean isIgnoreInvalidPath() { return ignoreInvalidPath; }
private void setConfigData() {
Object object = mappedConfig.get(MULTIPLE_SPEC);
if (object != null && (Boolean) object) {
multipleSpec = (Boolean)object;
}
object = mappedConfig.get(IGNORE_INVALID_PATH);
if (object != null && (Boolean) object) {
ignoreInvalidPath = (Boolean)object;
}
}

public Map<String, Object> getPathSpecMapping() {
Expand Down
4 changes: 4 additions & 0 deletions openapi-meta/src/main/resources/config/openapi-handler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# This configuration file is used to support multiple OpenAPI specifications in the same light-rest-4j instance.
# An indicator to allow multiple openapi specifications. Default to false which only allow one spec named openapi.yml or openapi.yaml or openapi.json.
multipleSpec: ${openapi-handler.multipleSpec:false}
# When the OpenApiHandler is used in a shared gateway and some backend APIs have no specifications deployed on the gateway, the handler will return
# 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
# 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.
ignoreInvalidPath: ${openapi-handler.ignoreInvalidPath:false}
# 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.
# 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.
pathSpecMapping: ${openapi-handler.pathSpecMapping:}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class OpenApiHandlerConfigTest {
@Test
public void testLoadConfig() {
OpenApiHandlerConfig config = OpenApiHandlerConfig.load();
Assert.assertEquals(2, config.getMappedConfig().size());
Assert.assertEquals(3, config.getMappedConfig().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# This configuration file is used to support multiple OpenAPI specifications in the same light-rest-4j instance.
# An indicator to allow multiple openapi specifications. Default to false which only allow one spec named openapi.yml or openapi.yaml or openapi.json.
multipleSpec: ${openapi-handler.multipleSpec:true}
# When the OpenApiHandler is used in a shared gateway and some backend APIs have no specifications deployed on the gateway, the handler will return
# 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
# 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.
ignoreInvalidPath: ${openapi-handler.ignoreInvalidPath:false}
# 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.
# 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.
pathSpecMapping:
Expand Down

0 comments on commit 038b297

Please sign in to comment.