Skip to content

Commit

Permalink
Exclude json-smart transitive dependency by implementing JsonProvider…
Browse files Browse the repository at this point in the history
… via Moshi
  • Loading branch information
ygree committed Jul 12, 2024
1 parent 1e180f3 commit 17eca2f
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 13 deletions.
6 changes: 5 additions & 1 deletion dd-trace-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ dependencies {
implementation libs.slf4j
implementation libs.moshi
implementation libs.jctools
implementation libs.jsonPath
implementation (libs.jsonPath) {
// exclude to reduce the size of the resulting jar
// it's possible by using MoshiJsonProvider
exclude group: 'net.minidev', module: 'json-smart'
}

implementation group: 'com.datadoghq', name: 'sketches-java', version: '0.8.3'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package datadog.trace.payloadtags;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.PathNotFoundException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -82,6 +84,8 @@ public JsonToTags build() {
private final int limitDeepness;
private final String tagPrefix;

private final ParseContext parseContext;

private JsonToTags(
List<JsonPath> expansionRules,
List<JsonPath> redactionRules,
Expand All @@ -93,6 +97,14 @@ private JsonToTags(
this.limitTags = limitTags;
this.limitDeepness = limitDeepness;
this.tagPrefix = tagPrefix;

Configuration configuration =
Configuration.builder()
.jsonProvider(new MoshiJsonProvider())
.mappingProvider(new MoshiMappingProvider())
.build();

parseContext = JsonPath.using(configuration);
}

public Map<String, Object> process(String str) {
Expand All @@ -104,7 +116,7 @@ public Map<String, Object> process(InputStream is) {
DocumentContext dc;

try {
dc = JsonPath.parse(is);
dc = parseContext.parse(is);
} catch (Exception ex) {
log.debug("Failed to parse JSON body for tag extraction", ex);
return Collections.emptyMap();
Expand Down Expand Up @@ -149,7 +161,7 @@ public Map<String, Object> process(InputStream is) {
return tags;
}

private static Object expandInnerJson(JsonPath jp, Object obj) {
private Object expandInnerJson(JsonPath jp, Object obj) {
if (obj instanceof String) {
String str = (String) obj;
if (!str.startsWith("{") && !str.startsWith("[")) {
Expand All @@ -160,7 +172,7 @@ private static Object expandInnerJson(JsonPath jp, Object obj) {
return str;
}
try {
return JsonPath.parse((String) obj).json();
return parseContext.parse((String) obj).json();
} catch (InvalidJsonException ex) {
log.debug("Failed to parse inner JSON for path: {}", jp.getPath(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package datadog.trace.payloadtags;

import com.jayway.jsonpath.InvalidJsonException;
import com.jayway.jsonpath.spi.json.AbstractJsonProvider;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import okio.BufferedSource;
import okio.Okio;

public class MoshiJsonProvider extends AbstractJsonProvider {
private final Moshi moshi = new Moshi.Builder().build();
private final JsonAdapter<Object> jsonAdapter = moshi.adapter(Object.class).lenient();

@Override
public Object parse(String s) throws InvalidJsonException {
try {
return jsonAdapter.fromJson(s);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}

@Override
public Object parse(InputStream inputStream, String s) throws InvalidJsonException {
try (BufferedSource source = Okio.buffer(Okio.source(inputStream))) {
return jsonAdapter.fromJson(source);
} catch (IOException e) {
throw new InvalidJsonException(e);
}
}

@Override
public String toJson(Object o) {
return jsonAdapter.toJson(o);
}

public List<Object> createArray() {
return new LinkedList<>();
}

public Object createMap() {
return new LinkedHashMap<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package datadog.trace.payloadtags;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

public class MoshiMappingProvider implements MappingProvider {
@Override
public <T> T map(Object o, Class<T> aClass, Configuration configuration) {
return null;
}

@Override
public <T> T map(Object o, TypeRef<T> typeRef, Configuration configuration) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class JsonToTagsTest extends Specification {
JsonToTags jsonToTags = new JsonToTags.Builder().build()

def json = """{
"a": [[ 1 ], [ 2, 3 ]],
"a": [[ 1 ], [ 2, 3 ]]
}"""

expect:
Expand All @@ -89,7 +89,7 @@ class JsonToTagsTest extends Specification {
JsonToTags jsonToTags = new JsonToTags.Builder().build()

def json = """{
"a": { "b": { "c": { "d": "e" } } },
"a": { "b": { "c": { "d": "e" } } }
}"""

expect:
Expand All @@ -102,7 +102,7 @@ class JsonToTagsTest extends Specification {
JsonToTags jsonToTags = new JsonToTags.Builder().build()

def json = """{
"a": [ "b", { "c": [ { "d": "e"} ] } ],
"a": [ "b", { "c": [ { "d": "e"} ] } ]
}"""

expect:
Expand All @@ -126,7 +126,7 @@ class JsonToTagsTest extends Specification {
"f": 6,
"g": 7,
"h": 8,
"i": 9,
"i": 9
}"""

expect:
Expand All @@ -145,7 +145,7 @@ class JsonToTagsTest extends Specification {

def json = """{
"a.b": 1,
"c.d": 2,
"c.d": 2
}"""

expect:
Expand Down Expand Up @@ -184,7 +184,7 @@ class JsonToTagsTest extends Specification {

def json = """{
"a": 1,
"b": 2,
"b": 2
}"""

expect:
Expand Down Expand Up @@ -216,7 +216,7 @@ class JsonToTagsTest extends Specification {
.build()

def json = """{
"Message": "${invalidInnerJson}",
"Message": "${invalidInnerJson}"
}"""

expect:
Expand All @@ -243,7 +243,7 @@ class JsonToTagsTest extends Specification {
String inner = "{ 'a: 1.15, 'password': 'my-secret-password' }"

def json = """{
"Message": "${inner}",
"Message": "${inner}"
}"""

expect: "Message attribute neither expanded nor redacted because of invalid rules"
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ commons = "3.2"
mockito = '4.4.0'
jctools = '3.3.0'
moshi = '1.11.0'
jsonPath = '2.8.0'
jsonPath = '2.9.0'
testcontainers = '1.19.3'
jmc = "8.1.0"
autoservice = "1.0-rc7"
Expand Down

0 comments on commit 17eca2f

Please sign in to comment.