Skip to content
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

Detect JSON array in addition to JSON objects when including a file in the config. #6307

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ private String includeFileDirective(String text, String source) {
String directive = entry.getKey();
String fileText = loadFile(entry.getValue(), directive, source);

// If the insert text is a legal JSON object "[white-space]{ ... }[white-space]", then
// If the insert text is a legal JSON object or array, then
// ignore the optional quotes matched by the directive pattern
var json = fileText.trim();
if (json.startsWith("{") && json.endsWith("}")) {
if (
json.startsWith("{") && json.endsWith("}") || json.startsWith("[") && json.endsWith("]")
miklcct marked this conversation as resolved.
Show resolved Hide resolved
) {
text = text.replace(entry.getKey(), fileText);
} else {
// Add back quotes if matched part of directive pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ void includeFileWithQuotesAndProperJsonInput() throws IOException {
assertEquals(json("{ 'key' : \t {\n 'foo' : 'bar' \n }\n}"), result);
}

@Test
void includeFileWithQuotesAndJsonArrayInput() throws IOException {
savePartialFile(json("\t [\n 'foo', 'bar' \n ]\n"));
String result = IncludeFileDirective.includeFileDirective(
CONFIG_DIR,
json("{ 'key' : '${includeFile:" + PART_FILE_NAME + "}'}"),
PART_FILE_NAME
);
assertEquals(json("{ 'key' : \t [\n 'foo', 'bar' \n ]\n}"), result);
}

@Test
void includeFileWithQuotesWithNoJsonInput() throws IOException {
savePartialFile("value");
Expand Down
3 changes: 2 additions & 1 deletion doc/templates/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ directory. Relative paths are not supported.

To allow both files (the configuration file and the injected file) to be valid JSON files, a special
case is supported. If the include file directive is quoted, then the quotes are removed, if the
text inserted is valid JSON (starts with `{` and ends with `}`).
text inserted is valid JSON object (starts with `{` and ends with `}`) or valid JSON array
(starts with `[` and ends with `]`).

Variable substitution is performed on configuration file after the include file directive; Hence
variable substitution is also performed on the text in the injected file.
Expand Down
3 changes: 2 additions & 1 deletion doc/user/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ directory. Relative paths are not supported.

To allow both files (the configuration file and the injected file) to be valid JSON files, a special
case is supported. If the include file directive is quoted, then the quotes are removed, if the
text inserted is valid JSON (starts with `{` and ends with `}`).
text inserted is valid JSON object (starts with `{` and ends with `}`) or valid JSON array
(starts with `[` and ends with `]`).

Variable substitution is performed on configuration file after the include file directive; Hence
variable substitution is also performed on the text in the injected file.
Expand Down
Loading