agturley commented on code in PR #11355:
URL: https://github.com/apache/nifi/pull/11355#discussion_r3454291139


##########
nifi-extension-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/java/org/apache/nifi/processors/elasticsearch/PutElasticsearchJsonTest.java:
##########
@@ -1110,4 +1110,170 @@ void testIndexFieldNullValueFallsBackConsistently() {
         runner.run();
         assertEquals("test_index", ops.getFirst().getIndex(), "NDJSON null -> 
fallback (not literal \"null\")");
     }
+
+    // 
-------------------------------------------------------------------------
+    // Nested field paths (e.g. @metadata/index)
+    // 
-------------------------------------------------------------------------
+
+    @Test
+    void testNestedIndexFieldNdjsonPrunesEmptyParent() {
+        runner.setProperty(PutElasticsearchJson.INPUT_FORMAT, 
InputFormat.NDJSON.getValue());
+        runner.setProperty(PutElasticsearchJson.INDEX_FIELD, 
"@metadata/index");
+        runner.setProperty(PutElasticsearchJson.RETAIN_INDEX_FIELD, "false");
+        runner.assertValid();
+
+        final List<IndexOperationRequest> ops = captureOperations();
+        
runner.enqueue("{\"@metadata\":{\"index\":\"docs-2026\"},\"msg\":\"hello\"}\n");
+        runner.run();
+
+        assertEquals("docs-2026", ops.getFirst().getIndex());
+        final String content = docContent(ops.getFirst());
+        assertFalse(content.contains("@metadata"), "now-empty parent should be 
pruned");
+        assertTrue(content.contains("hello"));
+    }
+
+    @Test
+    void testNestedIndexFieldJsonArrayPrunesEmptyParent() {
+        runner.setProperty(PutElasticsearchJson.INPUT_FORMAT, 
InputFormat.JSON_ARRAY.getValue());
+        runner.setProperty(PutElasticsearchJson.INDEX_FIELD, 
"@metadata/index");
+        runner.setProperty(PutElasticsearchJson.RETAIN_INDEX_FIELD, "false");
+        runner.assertValid();
+
+        final List<IndexOperationRequest> ops = captureOperations();
+        
runner.enqueue("[{\"@metadata\":{\"index\":\"docs-a\"},\"msg\":\"x\"}]");
+        runner.run();
+
+        assertEquals("docs-a", ops.getFirst().getIndex());
+        assertFalse(docContent(ops.getFirst()).contains("@metadata"));
+    }
+
+    @Test
+    void testNestedIndexFieldSingleJsonPrunesEmptyParent() {
+        runner.setProperty(PutElasticsearchJson.INDEX_FIELD, 
"@metadata/index");
+        runner.setProperty(PutElasticsearchJson.RETAIN_INDEX_FIELD, "false");
+        runner.assertValid();
+
+        final List<IndexOperationRequest> ops = captureOperations();
+        
runner.enqueue("{\"@metadata\":{\"index\":\"docs-single\"},\"msg\":\"x\"}");
+        runner.run();
+
+        assertEquals("docs-single", ops.getFirst().getIndex());
+        assertFalse(ops.getFirst().getFields().containsKey("@metadata"));
+    }
+
+    @Test
+    void testNestedIndexFieldKeepsParentWithRemainingSiblings() {
+        // @metadata still holds another field after the index leaf is 
removed, so it must NOT be pruned
+        runner.setProperty(PutElasticsearchJson.INPUT_FORMAT, 
InputFormat.NDJSON.getValue());
+        runner.setProperty(PutElasticsearchJson.INDEX_FIELD, 
"@metadata/index");
+        runner.setProperty(PutElasticsearchJson.RETAIN_INDEX_FIELD, "false");
+        runner.assertValid();
+
+        final List<IndexOperationRequest> ops = captureOperations();
+        
runner.enqueue("{\"@metadata\":{\"index\":\"docs-2026\",\"keep\":\"yes\"},\"msg\":\"hello\"}\n");
+        runner.run();
+
+        assertEquals("docs-2026", ops.getFirst().getIndex());
+        final String content = docContent(ops.getFirst());
+        assertTrue(content.contains("@metadata"), "parent kept because it 
still has siblings");
+        assertTrue(content.contains("keep"));
+        assertFalse(content.contains("\"index\""));
+    }
+
+    @Test
+    void testNestedIndexFieldRetainedByDefault() {
+        runner.setProperty(PutElasticsearchJson.INPUT_FORMAT, 
InputFormat.NDJSON.getValue());
+        runner.setProperty(PutElasticsearchJson.INDEX_FIELD, 
"@metadata/index");
+        runner.assertValid();
+
+        final List<IndexOperationRequest> ops = captureOperations();
+        
runner.enqueue("{\"@metadata\":{\"index\":\"docs-2026\"},\"msg\":\"hello\"}\n");
+        runner.run();
+
+        assertEquals("docs-2026", ops.getFirst().getIndex());
+        assertTrue(docContent(ops.getFirst()).contains("@metadata"), "nested 
field retained by default");
+    }
+
+    @Test
+    void testNestedIdentifierFieldNdjsonPrunesEmptyParent() {

Review Comment:
   Added testNestedIdentifierFieldUpdateOperationResolvesAndPrunes — it covers 
the resolveId nested path via an Update op (@metadata/id), verifying the nested 
ID resolves and the empty parent is pruned. Also added an escaped-slash test on 
the same path.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to