From 72d5765af8599eeff2cb01983f71dfe24a9ecdfd Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Thu, 5 Dec 2024 14:35:25 +0000 Subject: [PATCH 1/2] allow valid json array in include config --- .../config/framework/file/IncludeFileDirective.java | 6 ++++-- .../framework/file/IncludeFileDirectiveTest.java | 11 +++++++++++ doc/templates/Configuration.md | 3 ++- doc/user/Configuration.md | 3 ++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java b/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java index f864bc24d99..05295c8993f 100644 --- a/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java +++ b/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java @@ -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 diff --git a/application/src/test/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirectiveTest.java b/application/src/test/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirectiveTest.java index 0fc1199236d..0628b610216 100644 --- a/application/src/test/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirectiveTest.java +++ b/application/src/test/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirectiveTest.java @@ -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"); diff --git a/doc/templates/Configuration.md b/doc/templates/Configuration.md index 45b2c36c67b..ca89be6dc2a 100644 --- a/doc/templates/Configuration.md +++ b/doc/templates/Configuration.md @@ -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. diff --git a/doc/user/Configuration.md b/doc/user/Configuration.md index f80966b8cb3..53c0955a211 100644 --- a/doc/user/Configuration.md +++ b/doc/user/Configuration.md @@ -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. From e5f1f2ff778ccb87046816b12d10008c6b554427 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Thu, 12 Dec 2024 13:21:12 +0000 Subject: [PATCH 2/2] apply review suggestion by @leonardehrenfried --- .../standalone/config/framework/file/IncludeFileDirective.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java b/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java index 05295c8993f..7389babd7fe 100644 --- a/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java +++ b/application/src/main/java/org/opentripplanner/standalone/config/framework/file/IncludeFileDirective.java @@ -99,7 +99,7 @@ private String includeFileDirective(String text, String source) { // ignore the optional quotes matched by the directive pattern var json = fileText.trim(); if ( - json.startsWith("{") && json.endsWith("}") || json.startsWith("[") && json.endsWith("]") + (json.startsWith("{") && json.endsWith("}")) || (json.startsWith("[") && json.endsWith("]")) ) { text = text.replace(entry.getKey(), fileText); } else {