From efac4980d3d905303473c7a5c14139fd450315a3 Mon Sep 17 00:00:00 2001 From: Benjamin Benoist Date: Wed, 9 Sep 2020 19:11:07 +0200 Subject: [PATCH] Common: fix PathNotFoundException in PII enrichment (close #339) --- .../pii/PiiPseudonymizerEnrichment.scala | 22 +++++++++++-------- .../schemas/com.test/array/jsonschema/1-0-0 | 11 ++++++++++ .../pii/PiiPseudonymizerEnrichmentSpec.scala | 20 ++++++++++++++--- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/pii/PiiPseudonymizerEnrichment.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/pii/PiiPseudonymizerEnrichment.scala index 414a183ae..25f8286e4 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/pii/PiiPseudonymizerEnrichment.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/pii/PiiPseudonymizerEnrichment.scala @@ -265,15 +265,19 @@ final case class PiiJson( val objectNode = io.circe.jackson.mapper.valueToTree[ObjectNode](json) val documentContext = JJsonPath.using(JsonPathConf).parse(objectNode) val modifiedFields = MutableList[JsonModifiedField]() - val documentContext2 = documentContext.map( - jsonPath, - new ScrambleMapFunction(strategy, modifiedFields, fieldMutator.fieldName, jsonPath, schema) - ) - // make sure it is a structure preserving method, see #3636 - //val transformedJValue = JsonMethods.fromJsonNode(documentContext.json[JsonNode]()) - //val Diff(_, erroneouslyAdded, _) = jValue diff transformedJValue - //val Diff(_, withoutCruft, _) = erroneouslyAdded diff transformedJValue - (jacksonToCirce(documentContext2.json[JsonNode]()), modifiedFields.toList) + Option(documentContext.read[AnyRef](jsonPath)) match { // check that json object not null + case None => (jacksonToCirce(documentContext.json[JsonNode]()), modifiedFields.toList) + case _ => + val documentContext2 = documentContext.map( + jsonPath, + new ScrambleMapFunction(strategy, modifiedFields, fieldMutator.fieldName, jsonPath, schema) + ) + // make sure it is a structure preserving method, see #3636 + //val transformedJValue = JsonMethods.fromJsonNode(documentContext.json[JsonNode]()) + //val Diff(_, erroneouslyAdded, _) = jValue diff transformedJValue + //val Diff(_, withoutCruft, _) = erroneouslyAdded diff transformedJValue + (jacksonToCirce(documentContext2.json[JsonNode]()), modifiedFields.toList) + } } } diff --git a/modules/common/src/test/resources/iglu-schemas/schemas/com.test/array/jsonschema/1-0-0 b/modules/common/src/test/resources/iglu-schemas/schemas/com.test/array/jsonschema/1-0-0 index 2a342e2c0..97e2490a3 100644 --- a/modules/common/src/test/resources/iglu-schemas/schemas/com.test/array/jsonschema/1-0-0 +++ b/modules/common/src/test/resources/iglu-schemas/schemas/com.test/array/jsonschema/1-0-0 @@ -17,6 +17,17 @@ }, "field2": { "type": ["string", "null"] + }, + "field3": { + "type": ["object", "null"], + "properties": { + "a": { + "type": "string" + }, + "b": { + "type": "string" + } + } } }, "required": ["field"], diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/pii/PiiPseudonymizerEnrichmentSpec.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/pii/PiiPseudonymizerEnrichmentSpec.scala index 3c124570e..4837631f1 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/pii/PiiPseudonymizerEnrichmentSpec.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/pii/PiiPseudonymizerEnrichmentSpec.scala @@ -117,7 +117,8 @@ class PiiPseudonymizerEnrichmentSpec extends Specification with ValidatedMatcher | "schema": "iglu:com.test/array/jsonschema/1-0-0", | "data": { | "field" : ["hello", "world"], - | "field2" : null + | "field2" : null, + | "field3": null | } | } | ] @@ -353,6 +354,11 @@ class PiiPseudonymizerEnrichmentSpec extends Specification with ValidatedMatcher schemaCriterion = SchemaCriterion("com.test", "array", "jsonschema", 1, 0, 0), jsonPath = "$.field2" ), + PiiJson( + fieldMutator = JsonMutators("contexts"), + schemaCriterion = SchemaCriterion("com.test", "array", "jsonschema", 1, 0, 0), + jsonPath = "$.field3.a" + ), PiiJson( fieldMutator = JsonMutators("unstruct_event"), schemaCriterion = SchemaCriterion("com.mailgun", "message_clicked", "jsonschema", 1, 0, 0), @@ -424,14 +430,22 @@ class PiiPseudonymizerEnrichmentSpec extends Specification with ValidatedMatcher )) and (unstructEventJ.get[String]("myVar2") must beRight("awesome")) - val third = contextJThirdElement + val third = (contextJThirdElement .downField("data") .get[List[String]]("field") must beRight( List[String]("b62f3a2475ac957009088f9b8ab77ceb7b4ed7c5a6fd920daa204a1953334acb", "8ad32723b7435cbf535025e519cc94dbf1568e17ced2aeb4b9e7941f6346d7d0" ) - ) + )) and + (contextJThirdElement + .downField("data") + .downField("field2") + .focus must beSome.like { case json => json.isNull }) and + (contextJThirdElement + .downField("data") + .downField("field3") + .focus must beSome.like { case json => json.isNull }) first and second and third }