Skip to content

Commit

Permalink
Fix (user,site,app).data to be merged to (user,site,app).ext.data (#1141
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rpanchyk authored and nickluck8 committed Aug 10, 2021
1 parent b7fa184 commit 81a128e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,16 @@ private JsonNode toCommaSeparatedTextNode(ObjectNode containerNode, String field
}
}

public void normalizeDataExtension(ObjectNode containerNode, String containerName, String nodePrefix,
List<String> warnings) {
private void normalizeDataExtension(ObjectNode containerNode, String containerName, String nodePrefix,
List<String> warnings) {
final JsonNode data = containerNode.get(DATA);
if (data == null || !data.isObject()) {
return;
}
final JsonNode extData = containerNode.path(EXT).path(DATA);
final JsonNode ext = containerNode.get(EXT);
if (!extData.isNull() && !extData.isMissingNode()) {
final JsonNode resolvedExtData = jsonMerger.merge(extData, data);
final JsonNode resolvedExtData = jsonMerger.merge(data, extData);
((ObjectNode) ext).set(DATA, resolvedExtData);
} else {
copyDataToExtData(containerNode, containerName, nodePrefix, warnings, data);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/prebid/server/json/JsonMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ public <T> T merge(T originalObject, T mergingObject, Class<T> classToCast) {
}
}

public JsonNode merge(JsonNode originalObject, JsonNode mergingObject) {
/**
* Returns 'toNode' with merged properties from 'fromNode'.
*/
public JsonNode merge(JsonNode fromNode, JsonNode toNode) {
try {
return JsonMergePatch.fromJson(originalObject).apply(mergingObject);
return JsonMergePatch.fromJson(fromNode).apply(toNode);
} catch (JsonPatchException e) {
throw new InvalidRequestException("Couldn't create merge patch for json nodes");
}
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/org/prebid/server/auction/OrtbTypesResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,45 +218,46 @@ public void normalizeTargetingShouldTolerateIncorrectTypeUserFieldAndRemoveIt()
@Test
public void normalizeBidRequestShouldMergeUserDataToUserExtDataAndRemoveData() {
// given
final ObjectNode containerNode = obj("user", obj("data", obj("dataField", "dataValue"))
.set("ext", obj("data", obj("extDataField", "extDataValue"))));
final ObjectNode containerNode = obj("user", obj("data", obj("dataField", "dataValue1"))
.set("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue2"))));

// when
ortbTypesResolver.normalizeBidRequest(containerNode, new ArrayList<>(), "referer");

// then

assertThat(containerNode).isEqualTo(obj("user", obj("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue")))));
.put("dataField", "dataValue1")))));
}

@Test
public void normalizeBidRequestShouldMergeSiteDataToSiteExtDataAndRemoveData() {
// given
final ObjectNode containerNode = obj("site", obj("data", obj("dataField", "dataValue"))
.set("ext", obj("data", obj("extDataField", "extDataValue"))));
final ObjectNode containerNode = obj("site", obj("data", obj("dataField", "dataValue1"))
.set("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue2"))));

// when
ortbTypesResolver.normalizeBidRequest(containerNode, new ArrayList<>(), "referer");

// then

assertThat(containerNode).isEqualTo(obj("site", obj("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue")))));
.put("dataField", "dataValue1")))));
}

@Test
public void normalizeBidRequestShouldMergeAppDataToAppExtDataAndRemoveData() {
// given
final ObjectNode containerNode = obj("app", obj("data", obj("dataField", "dataValue"))
.set("ext", obj("data", obj("extDataField", "extDataValue"))));
final ObjectNode containerNode = obj("app", obj("data", obj("dataField", "dataValue1"))
.set("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue2"))));

// when
ortbTypesResolver.normalizeBidRequest(containerNode, new ArrayList<>(), "referer");

// then
assertThat(containerNode).isEqualTo(obj("app", obj("ext", obj("data", obj("extDataField", "extDataValue")
.put("dataField", "dataValue")))));
.put("dataField", "dataValue1")))));
}

@Test
Expand Down

0 comments on commit 81a128e

Please sign in to comment.