Skip to content

fixes #2136 update pathPrefixAuth to support JSON string in ApiKeyConfig #2137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions api-key/src/main/java/com/networknt/apikey/ApiKeyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,33 @@ private void setConfigList() {
if(s.startsWith("[")) {
// json format
try {
pathPrefixAuths = Config.getInstance().getMapper().readValue(s, new TypeReference<List<ApiKey>>() {});
List<Map<String, Object>> values = Config.getInstance().getMapper().readValue(s, new TypeReference<>() {});
pathPrefixAuths = populatePathPrefixAuths(values);
} catch (Exception e) {
logger.error("Exception:", e);
throw new ConfigException("could not parse the pathPrefixAuth json with a list of string and object.");
}
} else {
throw new ConfigException("pathPrefixAuth must be a list of string object map.");
}
} else if (object instanceof List) {
// the object is a list of map, we need convert it to PathPrefixAuth object.
List<Map<String, Object>> values = (List<Map<String, Object>>)object;
for(Map<String, Object> value: values) {
ApiKey apiKey = new ApiKey();
apiKey.setPathPrefix((String)value.get(PATH_PREFIX));
apiKey.setHeaderName((String)value.get(HEADER_NAME));
apiKey.setApiKey((String)value.get(API_KEY));
pathPrefixAuths.add(apiKey);
}
pathPrefixAuths = populatePathPrefixAuths((List<Map<String, Object>>)object);
} else {
throw new ConfigException("pathPrefixAuth must be a list of string object map.");
}
}
}

public static List<ApiKey> populatePathPrefixAuths(List<Map<String, Object>> values) {
List<ApiKey> pathPrefixAuths = new ArrayList<>();
for(Map<String, Object> value: values) {
ApiKey apiKey = new ApiKey();
apiKey.setPathPrefix((String)value.get(PATH_PREFIX));
apiKey.setHeaderName((String)value.get(HEADER_NAME));
apiKey.setApiKey((String)value.get(API_KEY));
pathPrefixAuths.add(apiKey);
}
return pathPrefixAuths;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<version.jsr305>3.0.2</version.jsr305>
<version.json-schema-validator>1.0.65</version.json-schema-validator>
<version.yaml-rule>1.0.3</version.yaml-rule>
<version.http-client>1.0.7</version.http-client>
<version.http-client>1.0.8</version.http-client>
<version.snakeyaml>2.2</version.snakeyaml>
<version.caffeine>3.1.5</version.caffeine>
<version.prometheus>0.16.0</version.prometheus>
Expand Down