pact-jvm icon indicating copy to clipboard operation
pact-jvm copied to clipboard

JsonParser does not handle encoding properly on pactbroker.consumerversionselectors.rawjson

Open ksawerykarwacki opened this issue 5 months ago • 4 comments

I tried to pass pactbroker.consumerversionselectors.rawjson parameter to Gradle Test task as system property for my junit tests.

I tried raw string without success, I tried building in with Gson like this:

internal fun getRawPactVersionSelector(): String {
    val jsonArray = JsonArray()

    val mainBranch = JsonObject().apply { addProperty("mainBranch", true) }
    val deployedOrReleased = JsonObject().apply { addProperty("deployedOrReleased", true) }
    val matchingBranch = JsonObject().apply { addProperty("matchingBranch", true) }

    jsonArray.add(mainBranch)
    jsonArray.add(deployedOrReleased)
    jsonArray.add(matchingBranch)

    return Gson().toJson(jsonArray)
}

Finally, after some more debugging it seems it does not properly handle escaping acrost multiple systems. Currently the only working solution looks like this:

internal fun getRawPactVersionSelector(): String {
    if(Os.isFamily(Os.FAMILY_WINDOWS)) {
        return "[{\\\"mainBranch\\\": true}, {\\\"deployedOrReleased\\\": true}, {\\\"matchingBranch\\\": true}]"
    }
    return "[{\"mainBranch\": true}, {\"deployedOrReleased\": true}, {\"matchingBranch\": true}]"
}

If I use windows one then Linux will keep complain about unrecognized \ character

au.com.dius.pact.core.support.json.JsonException: Invalid JSON (1:4), found unexpected character '\'
	at app//au.com.dius.pact.core.support.json.JsonLexer.unexpectedCharacter(JsonParser.kt:87)
	at app//au.com.dius.pact.core.support.json.JsonLexer.nextToken(JsonParser.kt:78)
	at app//au.com.dius.pact.core.support.json.JsonParser.nextTokenOrThrow(JsonParser.kt:278)
	at app//au.com.dius.pact.core.support.json.JsonParser.parseObject(JsonParser.kt:183)
	at app//au.com.dius.pact.core.support.json.JsonParser.parseArray(JsonParser.kt:254)
	at app//au.com.dius.pact.core.support.json.JsonParser.parse(JsonParser.kt:158)
	at app//au.com.dius.pact.core.support.json.JsonParser.parseString(JsonParser.kt:130)

if I use linux version (single ) or any standard lib implementation (like Gson) windows will complain:

 (1:4), found unexpected character 'm'
au.com.dius.pact.core.support.json.JsonException: Invalid JSON (1:4), found unexpected character 'm'
	at au.com.dius.pact.core.support.json.JsonLexer.unexpectedCharacter(JsonParser.kt:87)
	at au.com.dius.pact.core.support.json.JsonLexer.nextToken(JsonParser.kt:78)
	at au.com.dius.pact.core.support.json.JsonParser.nextTokenOrThrow(JsonParser.kt:278)
	at au.com.dius.pact.core.support.json.JsonParser.parseObject(JsonParser.kt:183)
	at au.com.dius.pact.core.support.json.JsonParser.parseArray(JsonParser.kt:254)
	at au.com.dius.pact.core.support.json.JsonParser.parse(JsonParser.kt:158)
	at au.com.dius.pact.core.support.json.JsonParser.parseString(JsonParser.kt:130)

ksawerykarwacki avatar Sep 21 '24 16:09 ksawerykarwacki