Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format response field as decimal number instead of string when using JsonPath. #855

Closed
robertmircea opened this issue Dec 2, 2022 · 7 comments
Assignees
Labels

Comments

@robertmircea
Copy link

robertmircea commented Dec 2, 2022

I am using the following mapping JSON file:

{
  "Guid": "42c42a10-897e-44e1-a50c-f3662193823d",
  "Title": "XYZ",
  "Priority": 100,
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "ExactMatcher",
          "Pattern": "/tpcm/v1/events/oneShot"
        }
      ]
    },
    "Methods": [
      "POST"
    ],
    "Headers": [
      {
        "Name": "Content-Type",
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "application/json",
            "IgnoreCase": true
          }
        ]
      }
    ],
    "Body": {
      "Matcher": {
        "Name": "JsonPartialWildcardMatcher",
        "Pattern": {
          "applicationId": "*",
          "currency": "EUR",
          "msisdn": "*",
          "externalId": "*",
          "transactionDescription": "*"
        },
        "IgnoreCase": false
      }
    }
  },
  "Response": {
    "StatusCode": 200,
    "UseTransformer": true,
    "BodyAsJson": {
      "reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}",
      "transactionId": "{{Random type='Guid' Format='N'}}"      
    },
    "Headers": {
      "Expires": "0",
      "Content-Type": "application/json; charset=UTF-8",
      "Cache-Control": "no-store, must-revalidate, no-cache, max-age=0",
      "Pragma": "no-cache",
      "Transfer-Encoding": "chunked"
    }
  }
}

Please note the following transformation expression for "reservedAmount" field in the response: "reservedAmount": "{{JsonPath.SelectToken request.bodyAsJson \"price\"}}",

The transformer generates the following json response given that I send the following request to Wiremock standalone server:

{
  "applicationId": "ABA",
  "currency": "EUR",
  "msisdn": "07777",
  "price": 0.005,
  "externalId": "uLGpSXnsCghn",
  "transactionDescription": "ABC"
}

Response:

{
  "reservedAmount": "0,005",
  "transactionId": "6447291E73BD44A78260940456AEDB0E"
}

Two questions:

  1. How can I return the reservedAmount field as JSON decimal number and not as a string?
  2. How can I format the returned number with a decimal dot instead of a comma?
@StefH StefH self-assigned this Dec 3, 2022
@StefH
Copy link
Collaborator

StefH commented Dec 3, 2022

@robertmircea
You don't need to use JsonPath.SelectToken, you can just access the properties from that request.bodyAsJson.

So:

"BodyAsJson": {
      "reservedAmount": "{{request.bodyAsJson.price}}",
      "transactionId": "{{Random type='Guid' Format='N'}}"      
    },

@StefH
Copy link
Collaborator

StefH commented Dec 4, 2022

#858

@robertmircea
Copy link
Author

Your recommendation works but the price field is still formatted incorrectly as a string instead of a number and it uses a comma instead of a decimal dot:

{
"reservedAmount":"0,005",
"transactionId":"B1359682B67647D6A7C96C9CD8D2F37F"
}

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2022

I'll take a look.

@StefH
Copy link
Collaborator

StefH commented Dec 7, 2022

@robertmircea
Copy link
Author

It works! Looks good! Can you include the fix please in a nuget release?

@StefH
Copy link
Collaborator

StefH commented Dec 11, 2022

Yes

I will release a new nuget soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants