forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parsing of flat object fields with dots in keys
We have a bug where a flat object field with inner fields that contain dots will "push" the dotted name onto the dot-path from the root, but then would just "pop" off the last part of the dotted name. This change adds more robust support for flat object keys and subkeys that contain dots (i.e. it pops off the entirety of the latest key, regardless of how many dots it contains). Fixes opensearch-project#11402 Signed-off-by: Michael Froh <[email protected]>
- Loading branch information
Showing
3 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
server/src/test/java/org/opensearch/common/xcontent/JsonToStringXContentParserTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.xcontent; | ||
|
||
import org.opensearch.common.xcontent.json.JsonXContent; | ||
import org.opensearch.core.xcontent.DeprecationHandler; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
public class JsonToStringXContentParserTests extends OpenSearchTestCase { | ||
|
||
private String flattenJsonString(String fieldName, String in) throws IOException { | ||
String transformed; | ||
try (XContentParser parser = | ||
JsonXContent.jsonXContent.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, in)) { | ||
JsonToStringXContentParser jsonToStringXContentParser = new JsonToStringXContentParser(xContentRegistry(), | ||
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, parser, fieldName); | ||
// Skip the START_OBJECT token: | ||
jsonToStringXContentParser.nextToken(); | ||
|
||
XContentParser transformedParser = jsonToStringXContentParser.parseObject(); | ||
try (XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()) { | ||
jsonBuilder.copyCurrentStructure(transformedParser); | ||
return jsonBuilder.toString(); | ||
} | ||
} | ||
} | ||
|
||
|
||
public void testNestedObjects() throws IOException { | ||
String jsonExample = "{" + | ||
"\"first\" : \"1\"," + | ||
"\"second\" : {" + | ||
" \"inner\": \"2.0\"" + | ||
"}," + | ||
"\"third\": \"three\"" + | ||
"}"; | ||
|
||
assertEquals("{" + | ||
"\"flat\":[\"first\",\"second\",\"inner\",\"third\"]," + | ||
"\"flat._value\":[\"1\",\"2.0\",\"three\"]," + | ||
"\"flat._valueAndPath\":[\"flat.first=1\",\"flat.second.inner=2.0\",\"flat.third=three\"]" + | ||
"}", flattenJsonString("flat", jsonExample)); | ||
} | ||
|
||
public void testChildHasDots() throws IOException { | ||
// This should be exactly the same as testNestedObjects. We're just using the "flat" notation for the inner | ||
// object. | ||
String jsonExample = "{" + | ||
"\"first\" : \"1\"," + | ||
"\"second.inner\" : \"2.0\"," + | ||
"\"third\": \"three\"" + | ||
"}"; | ||
|
||
assertEquals("{" + | ||
"\"flat\":[\"first\",\"second\",\"inner\",\"third\"]," + | ||
"\"flat._value\":[\"1\",\"2.0\",\"three\"]," + | ||
"\"flat._valueAndPath\":[\"flat.first=1\",\"flat.second.inner=2.0\",\"flat.third=three\"]" + | ||
"}", flattenJsonString("flat", jsonExample)); | ||
} | ||
|
||
public void testNestChildObjectWithDots() throws IOException { | ||
String jsonExample = "{" + | ||
"\"first\" : \"1\"," + | ||
"\"second.inner\" : {" + | ||
" \"really_inner\" : \"2.0\"" + | ||
"}," + | ||
"\"third\": \"three\"" + | ||
"}"; | ||
|
||
assertEquals("{" + | ||
"\"flat\":[\"first\",\"second\",\"inner\",\"really_inner\",\"third\"]," + | ||
"\"flat._value\":[\"1\",\"2.0\",\"three\"]," + | ||
"\"flat._valueAndPath\":[\"flat.first=1\",\"flat.second.inner.really_inner=2.0\",\"flat.third=three\"]" + | ||
"}", flattenJsonString("flat", jsonExample)); | ||
} | ||
|
||
public void testNestChildObjectWithDotsAndFieldWithDots() throws IOException { | ||
String jsonExample = "{" + | ||
"\"first\" : \"1\"," + | ||
"\"second.inner\" : {" + | ||
" \"totally.absolutely.inner\" : \"2.0\"" + | ||
"}," + | ||
"\"third\": \"three\"" + | ||
"}"; | ||
|
||
assertEquals("{" + | ||
"\"flat\":[\"first\",\"second\",\"inner\",\"totally\",\"absolutely\",\"inner\",\"third\"]," + | ||
"\"flat._value\":[\"1\",\"2.0\",\"three\"]," + | ||
"\"flat._valueAndPath\":[\"flat.first=1\",\"flat.second.inner.totally.absolutely.inner=2.0\",\"flat.third=three\"]" + | ||
"}", flattenJsonString("flat", jsonExample)); | ||
} | ||
|
||
} |