pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.59k stars 343 forks source link

ProviderStateParameters - are defaulting to String #1110

Open lushilling opened 12 months ago

lushilling commented 12 months ago

In the Provider test, I am setting a number value in the request body. However when the test runs its converts it from an Integer to String.

In my consumer everything works and contract generated correctly: I have the following line in my request body accountId: fromProviderState('${accountId}', accountId)

In the debug logs I can see it populating correctly with correct type

Calling match_values for path $.accountId
Comparing '1686819389' to '1686819389' using Type -> Ok(())
Comparing 'Number([1686819389](tel:1686819389))' to 'Number([1686819389](tel:1686819389))' at path '$.accountId' -> Ok(())
body: '{……..  ,"accountId":1686819389}'

In my provider:

int accountId = 2;
providerStateParameters.put(ACCOUNT_ID, accountId);

In the debug logs:

****I set my account id as: 1689080143238***
body: PRESENT({………..,"accountId":"1689080143238"})

Both consumer (pact-js) and provider (pact-jvm) are on the latest version

rholshausen commented 10 months ago

I am unable to reproduce this. Pact-JVM keeps the types, so this issue must exist in Pact-JS. I'll move the issue there.

Are you able to supply the Pact file as well as the test?

mefellows commented 10 months ago

I think this relates to https://github.com/pact-foundation/pact-reference/issues/298.

lushilling commented 4 months ago

This is still an issue with Pact-JVM too

my consumer tests are written in Pact-JS it logs the value as the correct type pact_matching::json: compare_values: Calling match_values for path $.userId pact_matching::json: JSON -> JSON: Comparing '1' to '1' using Type -> Ok(()) pact_matching::json: compare_values: Comparing 'Number(1)' to 'Number(1)' at path '$.userId' -> Ok(())

Pact file created

          "body": {
            "$.userId": {
              "expression": "${userId}",
              "type": "ProviderState"
            }
          }
        }

Provider written in Java -> logs

2024-02-26 17:12:18,369 DEBUG ll=DEBUG c=a.c.d.p.p.ProviderClient t=main    method: POST
path: /login
    query: {}
    headers: {Accept=[application/json], Content-Type=[application/json]}
    matchers: MatchingRules(rules={body=MatchingRuleCategory(name=body, matchingRules={$.userId=MatchingRuleGroup(rules=[au.com.dius.pact.core.model.matchingrules.TypeMatcher@346330b6], ruleLogic=AND, cascaded=false)}), header=MatchingRuleCategory(name=header, matchingRules={}), path=MatchingRuleCategory(name=path, matchingRules={}), query=MatchingRuleCategory(name=query, matchingRules={})})
    generators: Generators(categories={BODY={$.smUserId=ProviderStateGenerator(expression=${smUserId}, dataType=RAW)}})
    body: PRESENT({"userId":"829152765"})