Skip to content

Commit

Permalink
allow valid json array in include config
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Dec 5, 2024
1 parent 2050217 commit 72d5765
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
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("]")
) {
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

0 comments on commit 72d5765

Please sign in to comment.