Skip to content

Commit

Permalink
Common: fix number deserialization in API Request Enrichment (close #383
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oguzhanunlu committed Oct 27, 2020
1 parent 2b7d9d9 commit 1857866
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
uses: actions/setup-java@v1
with:
java-version: 11
- name: Prepare Mock server for SCE ApiRequestEnrichmentIntegrationTest (launch in background)
run: python integration-tests/api-lookup-test.py 8002 &
- name: Prepare Mock server for SCE ApiRequestEnrichmentIntegrationTest (launch in background)
run: python integration-tests/sce-api-lookup-test.py 8001 &
- name: Prepare Mock server for BE ApiRequestEnrichmentIntegrationTest (launch in background)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/api-lookup-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def generate_response(self, method, auth=None):
}
}
else:
response = { "serviceName": "sp-api-request-enrichment" }
response = { "latitude": 33.2296 }
return json.dumps(response)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package enrichments.registry.apirequest

import cats.syntax.either._
import io.circe._
import io.circe.parser._
import io.circe.syntax._

import utils.JsonPath.{query, wrapArray}
Expand Down Expand Up @@ -117,6 +116,5 @@ final case class JsonOutput(jsonPath: String) extends ApiOutput[Json] {
case other => other.leftMap(JsonPathException.apply)
}

def parseResponse(response: String): Either[Throwable, Json] =
parse(response)
def parseResponse(response: String): Either[Throwable, Json] = io.circe.jackson.enrich.parse(response)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.circe.jackson.enrich

import java.io.File

import cats.data.ValidatedNel
import io.circe.{Decoder, Error, Json, Parser, ParsingFailure}

import scala.util.control.NonFatal

trait JacksonParser extends Parser { this: WithJacksonMapper =>
final def parse(input: String): Either[ParsingFailure, Json] =
try Right(mapper.readValue(jsonStringParser(input), classOf[Json]))
catch {
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error))
}

final def parseFile(file: File): Either[ParsingFailure, Json] =
try Right(mapper.readValue(jsonFileParser(file), classOf[Json]))
catch {
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error))
}

final def parseByteArray(bytes: Array[Byte]): Either[ParsingFailure, Json] =
try Right(mapper.readValue(jsonBytesParser(bytes), classOf[Json]))
catch {
case NonFatal(error) => Left(ParsingFailure(error.getMessage, error))
}

final def decodeByteArray[A: Decoder](bytes: Array[Byte]): Either[Error, A] =
finishDecode[A](parseByteArray(bytes))

final def decodeByteArrayAccumulating[A: Decoder](bytes: Array[Byte]): ValidatedNel[Error, A] =
finishDecodeAccumulating[A](parseByteArray(bytes))

final def decodeFile[A: Decoder](file: File): Either[Error, A] =
finishDecode[A](parseFile(file))

final def decodeFileAccumulating[A: Decoder](file: File): ValidatedNel[Error, A] =
finishDecodeAccumulating[A](parseFile(file))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.circe.jackson.enrich

import java.io.{File, Writer}

import com.fasterxml.jackson.core.{JsonFactory, JsonParser}
import com.fasterxml.jackson.databind.ObjectMapper
import com.snowplowanalytics.snowplow.enrich.common.utils.CirceUtils

class WithJacksonMapper {
final val mapper: ObjectMapper = CirceUtils.mapper
private[this] final val jsonFactory: JsonFactory = new JsonFactory(mapper)

protected final def jsonStringParser(input: String): JsonParser = jsonFactory.createParser(input)
protected final def jsonFileParser(file: File): JsonParser = jsonFactory.createParser(file)
protected final def jsonBytesParser(bytes: Array[Byte]): JsonParser =
jsonFactory.createParser(bytes)
protected final def jsonGenerator(out: Writer) = jsonFactory.createGenerator(out)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.circe.jackson

package object enrich extends WithJacksonMapper with JacksonParser with JacksonCompat {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
"description": "Schema for acme stuff",
"self": {
"vendor": "com.acme",
"name": "geo",
"format": "jsonschema",
"version": "1-0-0"
},
"type": "object",
"properties": {
"latitude": {
"type": "number"
}
},
"required": ["latitude"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,17 @@ class ApiRequestEnrichmentIntegrationTest extends Specification {
"name": "api_request_enrichment_config",
"enabled": true,
"parameters": {
"inputs": [
{
"key": "user",
"pojo": {
"field": "user_id"
}
},
{
"key": "client",
"pojo": {
"field": "app_id"
}
}
],
"inputs": [],
"api": {
"http": {
"method": "GET",
"uri": "http://localhost:8001/guest/api/{{client}}/{{user}}?format=json",
"timeout": 5000,
"uri": "http://localhost:8002/guest",
"timeout": 5000,
"authentication": {}
}
},
"outputs": [{
"schema": "iglu:com.acme/unauth/jsonschema/1-0-0",
"schema": "iglu:com.acme/geo/jsonschema/1-0-0",
"json": {
"jsonPath": "$$"
}
Expand All @@ -86,8 +73,7 @@ class ApiRequestEnrichmentIntegrationTest extends Specification {

val correctResultContext =
SelfDescribingData(
SchemaKey("com.acme", "unauth", "jsonschema", SchemaVer.Full(1, 0, 0)),
json"""{"path": "/guest/api/lookup-test/snowplower?format=json", "message": "unauthorized", "method": "GET"}"""
SchemaKey("com.acme", "geo", "jsonschema", SchemaVer.Full(1, 0, 0)), json"""{"latitude": 33.2296 }"""
)

val configuration2 = json"""{
Expand Down

0 comments on commit 1857866

Please sign in to comment.