-
Notifications
You must be signed in to change notification settings - Fork 19
Add ms.hdfs.reader.parse.json.strings for inlining JSON strings #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
avinas-kumar
merged 6 commits into
linkedin:master
from
vigy321:hdfs-reader-inline-json-strings
Apr 30, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7022804
Add ms.hdfs.reader.parse.json.strings for inlining JSON strings
96ef2c3
Use legacy JsonParser API for Gson 2.6.2 compatibility
2548f16
Use gson.fromJson with JsonElement.class for style consistency
a35187d
Address PR review: register property in allProperties; cover UNION sc…
9e7bc51
Address PR review: rename looksLikeJson helper
4c2a768
Pin build-info-extractor-gradle to 5.2.5 to fix Gradle 6.8.1 build
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
154 changes: 154 additions & 0 deletions
154
cdi-core/src/test/java/com/linkedin/cdi/util/HdfsReaderTest.java
This file contains hidden or 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,154 @@ | ||
| // Copyright 2021 LinkedIn Corporation. All rights reserved. | ||
| // Licensed under the BSD-2 Clause license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| package com.linkedin.cdi.util; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.SchemaBuilder; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.gobblin.configuration.SourceState; | ||
| import org.powermock.reflect.Whitebox; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static com.linkedin.cdi.configuration.PropertyCollection.*; | ||
|
|
||
|
|
||
| public class HdfsReaderTest { | ||
| private static final String FIELD = "data"; | ||
| private static final List<String> FIELDS = Collections.singletonList(FIELD); | ||
|
|
||
| private SourceState state; | ||
| private HdfsReader reader; | ||
|
|
||
| @BeforeMethod | ||
| public void setUp() { | ||
| state = new SourceState(); | ||
| reader = new HdfsReader(state); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsJsonObject_inlinesAsJsonObject() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, "{\"key\":\"value\"}"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonObject()); | ||
| Assert.assertEquals(result.get(FIELD).getAsJsonObject().get("key").getAsString(), "value"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsJsonArray_inlinesAsJsonArray() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, "[{\"@context\":\"schema.org\"}]"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonArray()); | ||
| Assert.assertEquals( | ||
| result.get(FIELD).getAsJsonArray().get(0).getAsJsonObject().get("@context").getAsString(), | ||
| "schema.org"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsPlainText_keepsAsJsonPrimitive() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, "hello world"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonPrimitive()); | ||
| Assert.assertEquals(result.get(FIELD).getAsString(), "hello world"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsMalformedJson_fallsBackToJsonPrimitive() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, "[{incomplete]"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonPrimitive()); | ||
| Assert.assertEquals(result.get(FIELD).getAsString(), "[{incomplete]"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOff_preservesLegacyBehaviorForJsonContent() throws Exception { | ||
| GenericRecord record = recordWithStringField(FIELD, "{\"key\":\"value\"}"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonPrimitive()); | ||
| Assert.assertEquals(result.get(FIELD).getAsString(), "{\"key\":\"value\"}"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOff_preservesLegacyBehaviorForPlainText() throws Exception { | ||
| GenericRecord record = recordWithStringField(FIELD, "hello"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertEquals(result.get(FIELD).getAsString(), "hello"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsNull_returnsJsonNull() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, null); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonNull()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testStringField_whenFlagOn_andValueIsEmptyString_keepsAsJsonPrimitive() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithStringField(FIELD, ""); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonPrimitive()); | ||
| Assert.assertEquals(result.get(FIELD).getAsString(), ""); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullableStringField_whenFlagOn_andValueIsJsonArray_inlinesAsJsonArray() throws Exception { | ||
| state.setProp(MSTAGE_HDFS_READER_PARSE_JSON_STRINGS.getConfig(), "true"); | ||
| GenericRecord record = recordWithNullableStringField(FIELD, "[{\"@context\":\"schema.org\"}]"); | ||
|
|
||
| JsonObject result = Whitebox.invokeMethod(reader, "selectFieldsFromGenericRecord", record, FIELDS); | ||
|
|
||
| Assert.assertTrue(result.get(FIELD).isJsonArray()); | ||
| Assert.assertEquals( | ||
| result.get(FIELD).getAsJsonArray().get(0).getAsJsonObject().get("@context").getAsString(), | ||
| "schema.org"); | ||
| } | ||
|
|
||
| private GenericRecord recordWithStringField(String key, String val) { | ||
|
vigy321 marked this conversation as resolved.
|
||
| Schema schema = SchemaBuilder.record("Test").namespace("com.linkedin.test") | ||
| .doc("Test record").fields() | ||
| .name(key).doc("test").type().stringType() | ||
| .noDefault().endRecord(); | ||
| GenericRecord record = new GenericData.Record(schema); | ||
| record.put(key, val); | ||
| return record; | ||
| } | ||
|
|
||
| private GenericRecord recordWithNullableStringField(String key, String val) { | ||
| Schema schema = SchemaBuilder.record("Test").namespace("com.linkedin.test") | ||
| .doc("Test record").fields() | ||
| .name(key).doc("test").type().nullable().stringType() | ||
| .noDefault().endRecord(); | ||
| GenericRecord record = new GenericData.Record(schema); | ||
| record.put(key, val); | ||
| return record; | ||
| } | ||
| } | ||
This file contains hidden or 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,37 @@ | ||
| # ms.hdfs.reader.parse.json.strings | ||
|
|
||
| **Tags**: | ||
|
|
||
| **Type**: boolean | ||
|
|
||
| **Format**: true/false | ||
|
|
||
| **Default value**: false | ||
|
|
||
| ## Related | ||
| - [ms.secondary.input](ms.secondary.input.md) | ||
|
|
||
| ## Description | ||
|
|
||
| When set to `true`, `HdfsReader` detects string-typed fields whose values parse as valid JSON (objects or | ||
| arrays) and inlines them as JSON elements in the outgoing payload, rather than escaping them as JSON string | ||
| primitives. | ||
|
|
||
| This is useful when a pre-serialized JSON document (including JSON-LD, which uses `@`-prefixed field names | ||
| that Avro schemas cannot express) is stored as an Avro `string` field and needs to be sent inline in an HTTP | ||
| POST body. | ||
|
|
||
| ### Behavior | ||
|
|
||
| - `false` (default): string fields are always serialized as JSON string primitives. This is the existing | ||
| behavior and is preserved for backward compatibility. | ||
| - `true`: for each string field, if the trimmed value begins with `{`/`[` and ends with `}`/`]` and parses | ||
| as valid JSON, the parsed JSON element replaces the string primitive. Parse failures fall back to the | ||
| default string-primitive behavior. | ||
|
|
||
| ### Example | ||
|
|
||
| Given an Avro record with `data: string` containing `[{"@context":"https://schema.org"}]`: | ||
|
|
||
| - Flag off: `{"data":"[{\"@context\":\"https://schema.org\"}]"}` (escaped) | ||
| - Flag on: `{"data":[{"@context":"https://schema.org"}]}` (inlined) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.