Skip to content

Commit ca633f9

Browse files
committed
TIKA-4834: allow comments in json configs throughout
1 parent 26381aa commit ca633f9

7 files changed

Lines changed: 95 additions & 0 deletions

File tree

CHANGES.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Release 4.1.0 - unreleased
22

3+
* tika-server and tika-async-cli now start from a config that contains
4+
// or /* */ comments, as the configuration docs have always said they
5+
may. The main loader accepted them; the steps that re-read the user's
6+
file to merge in server/CLI overrides (ConfigMerger, ensurePluginRoots)
7+
used a strict parser and refused the whole file (TIKA-4834).
8+
39
* The Kafka pipes iterator no longer stops at the first empty poll. A newly
410
subscribed consumer spends its first poll(s) joining the group and returns
511
empty even when the topic has a backlog, so the iterator could enqueue zero

tika-pipes/tika-async-cli/src/main/java/org/apache/tika/async/cli/TikaAsyncCLI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Optional;
2626
import java.util.concurrent.TimeoutException;
2727

28+
import com.fasterxml.jackson.core.JsonParser;
2829
import com.fasterxml.jackson.databind.JsonNode;
2930
import com.fasterxml.jackson.databind.ObjectMapper;
3031
import com.fasterxml.jackson.databind.node.ObjectNode;
@@ -445,6 +446,8 @@ static String resolveDefaultPluginsDir() {
445446
*/
446447
static Path ensurePluginRoots(Path originalConfigPath, String pluginsDir) throws IOException {
447448
ObjectMapper mapper = new ObjectMapper();
449+
// The user's file may carry the // and /* */ comments the config docs permit.
450+
mapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
448451
JsonNode rootNode = mapper.readTree(originalConfigPath.toFile());
449452

450453
if (rootNode.has("plugin-roots")) {

tika-pipes/tika-async-cli/src/test/java/org/apache/tika/async/cli/AsyncCliParserTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ public void testFileListDefaultsOutputDir(@TempDir Path tmp) throws Exception {
159159
@TempDir
160160
Path tempDir;
161161

162+
/** TIKA-4834: the config docs permit comments. */
163+
@Test
164+
public void testEnsurePluginRootsAcceptsComments() throws Exception {
165+
Path configPath = tempDir.resolve("config-comments.json");
166+
Files.writeString(configPath, """
167+
// leading comment
168+
{
169+
/* block */
170+
"pipes": { "numClients": 3 } // trailing
171+
}
172+
""");
173+
Path result = TikaAsyncCLI.ensurePluginRoots(configPath, null);
174+
JsonNode root = new ObjectMapper().readTree(result.toFile());
175+
assertTrue(root.has("plugin-roots"));
176+
assertEquals(3, root.get("pipes").get("numClients").asInt());
177+
}
178+
162179
@Test
163180
public void testEnsurePluginRootsAddsDefault() throws Exception {
164181
// Create a config without plugin-roots

tika-pipes/tika-pipes-core/src/main/java/org/apache/tika/pipes/core/config/ConfigMerger.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.UUID;
2727

28+
import com.fasterxml.jackson.core.JsonParser;
2829
import com.fasterxml.jackson.databind.JsonNode;
2930
import com.fasterxml.jackson.databind.ObjectMapper;
3031
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -92,6 +93,8 @@ public static MergeResult mergeOrCreate(Path existingConfig, ConfigOverrides ove
9293
throws IOException {
9394
ObjectMapper mapper = new ObjectMapper();
9495
mapper.enable(SerializationFeature.INDENT_OUTPUT);
96+
// The user's file may carry the // and /* */ comments the config docs permit.
97+
mapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
9598

9699
ObjectNode root;
97100
if (existingConfig != null && Files.exists(existingConfig)) {

tika-pipes/tika-pipes-core/src/test/java/org/apache/tika/pipes/core/config/ConfigMergerTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,40 @@ public void testCreateNewConfig() throws IOException {
8282
Files.deleteIfExists(result.configPath());
8383
}
8484

85+
/** TIKA-4834: the config docs permit comments; the merge step must not reject them. */
86+
@Test
87+
public void testMergeWithCommentedConfig() throws IOException {
88+
String existingConfig = """
89+
// leading line comment
90+
{
91+
/* block comment */
92+
"fetchers": {
93+
"existing-fetcher": { // trailing comment
94+
"file-system-fetcher": {
95+
"basePath": "/existing/path"
96+
}
97+
}
98+
},
99+
"plugin-roots": "existing-plugins"
100+
}
101+
""";
102+
Path existingPath = tempDir.resolve("commented-config.json");
103+
Files.writeString(existingPath, existingConfig);
104+
105+
ConfigOverrides overrides = ConfigOverrides.builder()
106+
.addFetcher("new-fetcher", "file-system-fetcher", Map.of("basePath", "/new/path"))
107+
.build();
108+
ConfigMerger.MergeResult result = ConfigMerger.mergeOrCreate(existingPath, overrides);
109+
110+
// The merged file is plain JSON: readable by a strict mapper, content intact.
111+
JsonNode root = new ObjectMapper().readTree(result.configPath().toFile());
112+
assertEquals("/existing/path", root.get("fetchers").get("existing-fetcher")
113+
.get("file-system-fetcher").get("basePath").asText());
114+
assertTrue(root.get("fetchers").has("new-fetcher"));
115+
assertEquals("existing-plugins", root.get("plugin-roots").asText());
116+
Files.deleteIfExists(result.configPath());
117+
}
118+
85119
@Test
86120
public void testMergeWithExistingConfig() throws IOException {
87121
// Create existing config

tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TikaServerIntegrationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ public void testBasic() throws Exception {
100100
testBaseline();
101101
}
102102

103+
/** TIKA-4834: the docs permit comments in the config; the server must start from one. */
104+
@Test
105+
public void testCommentedConfig() throws Exception {
106+
startProcess(new String[]{"-config", getConfig("tika-config-server-comments.json")});
107+
awaitServerStartup();
108+
Response response = WebClient
109+
.create(endPoint + RMETA_PATH)
110+
.accept("application/json")
111+
.put(ClassLoader.getSystemResourceAsStream(TEST_HELLO_WORLD));
112+
assertEquals(200, response.getStatus());
113+
}
114+
103115
@Test
104116
public void testBasicWithPipes() throws Exception {
105117
// Test that pipes-based parsing works for normal documents
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TIKA-4834: comments everywhere the docs say they are allowed
2+
{
3+
/* block comment before a key */
4+
"server": {
5+
"port": 9999, // trailing comment
6+
"allowPipes": true,
7+
"endpoints": [
8+
"rmeta",
9+
// comment inside an array
10+
"tika"
11+
]
12+
},
13+
"pipes": {
14+
"numClients": 1,
15+
"forkedJvmArgs": [
16+
"-Xmx256m"
17+
]
18+
},
19+
"plugin-roots": "target/plugins"
20+
}

0 commit comments

Comments
 (0)