From 81a128e212b2e00e5ab7649519b73dfea71a5c42 Mon Sep 17 00:00:00 2001 From: rpanchyk Date: Mon, 8 Feb 2021 16:38:51 +0200 Subject: [PATCH] Fix (user,site,app).data to be merged to (user,site,app).ext.data (#1141) --- .../server/auction/OrtbTypesResolver.java | 6 ++--- .../org/prebid/server/json/JsonMerger.java | 7 ++++-- .../server/auction/OrtbTypesResolverTest.java | 23 ++++++++++--------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/prebid/server/auction/OrtbTypesResolver.java b/src/main/java/org/prebid/server/auction/OrtbTypesResolver.java index 547ca7735e8..0e3adf889c3 100644 --- a/src/main/java/org/prebid/server/auction/OrtbTypesResolver.java +++ b/src/main/java/org/prebid/server/auction/OrtbTypesResolver.java @@ -245,8 +245,8 @@ private JsonNode toCommaSeparatedTextNode(ObjectNode containerNode, String field } } - public void normalizeDataExtension(ObjectNode containerNode, String containerName, String nodePrefix, - List warnings) { + private void normalizeDataExtension(ObjectNode containerNode, String containerName, String nodePrefix, + List warnings) { final JsonNode data = containerNode.get(DATA); if (data == null || !data.isObject()) { return; @@ -254,7 +254,7 @@ public void normalizeDataExtension(ObjectNode containerNode, String containerNam 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); diff --git a/src/main/java/org/prebid/server/json/JsonMerger.java b/src/main/java/org/prebid/server/json/JsonMerger.java index b46010eb5a8..cd6b8aceee7 100644 --- a/src/main/java/org/prebid/server/json/JsonMerger.java +++ b/src/main/java/org/prebid/server/json/JsonMerger.java @@ -65,9 +65,12 @@ public T merge(T originalObject, T mergingObject, Class 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"); } diff --git a/src/test/java/org/prebid/server/auction/OrtbTypesResolverTest.java b/src/test/java/org/prebid/server/auction/OrtbTypesResolverTest.java index 1e3d2aee20c..7d9f86c5725 100644 --- a/src/test/java/org/prebid/server/auction/OrtbTypesResolverTest.java +++ b/src/test/java/org/prebid/server/auction/OrtbTypesResolverTest.java @@ -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