Skip to content

Commit

Permalink
Common: fix PathNotFoundException in PII enrichment (close #339)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjben committed Sep 9, 2020
1 parent eaa7c9d commit efac498
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
},
"field2": {
"type": ["string", "null"]
},
"field3": {
"type": ["object", "null"],
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
}
}
}
},
"required": ["field"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
| }
| }
| ]
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit efac498

Please sign in to comment.