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

Support templating more complex captures into JSON request bodies #2152

Open
colinwd opened this issue Nov 12, 2023 · 4 comments
Open

Support templating more complex captures into JSON request bodies #2152

colinwd opened this issue Nov 12, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@colinwd
Copy link

colinwd commented Nov 12, 2023

Problem to solve

While we can easily capture complex variables via jsonpath, the ability to use them is limited at the moment:

GET http://localhost:3000/car/1

HTTP 200
[Captures]
features: jsonpath "$.car.features" # Array of strings


PUT http://localhost:3000/car/2
{
    "features": {{features}}
}

HTTP 200

Output:

error: Unrenderable variable
  --> array-var.hurl:10:19
   |
10 |     "features": {{features}}
   |                   ^^^^^^^^ variable <features> with value [wheels engine doors] can not be rendered

Proposal

I have done some initial digging through the code to see what it might take to support this, and noted that it's likely not a trivial modification. The current templating code, as we can see in the error message above, outputs strings without any surrounding quotes, and modifying that would be a breaking change for that component's public API.

I'd love to support getting this into place, but wanted to make sure any changes are in line with existing project guidelines and direction. Some guidance on how this might fit into the project as it is today would be very welcome.

Tasks to complete

  1. Update Templating Body docs to accurately reflect that templating primitives does currently work for JSON bodies! Happy to submit a separate, initial PR for this.
  2. Modify existing (or add new?) JSON templating code to support complex types - mainly arrays and objects.
  3. Lots of unit tests.
  4. Update docs to reflect new feature.

Let me know if this is something that will fit into the current roadmap! Thanks!

@colinwd colinwd added the enhancement New feature or request label Nov 12, 2023
@fabricereix
Copy link
Collaborator

Hi @colinwd,
Thanks for your suggestion.
We could indeed support more rendering, specifically in a JSON body for arrays and objects.
As suggested, you can already submit a PR for the doc for the existing behaviour.

For this enhancement, we will have to make sure that:

  • this impacts only JSON body (and not other bodies)
  • the rendering errors are still correct. We will still have values that will never be renderable in a JSON such collections from XML.

Before implementing, we can update this issue with real usecase/concrete example that we will use for integration testing.

@janstrohschein
Copy link

I found this issue because we upgraded from Hurl version 2 to version 4 and now our response body content cant be rendered anymore. Have there been some changes in how the response is handled?

[Captures]
client_id: jsonpath "$[?(@.clientId=='{{client_id}}')].id"

Output in version 2: e654bfe8-1195-44e2-9f56-bb9ec83fe14e
Output in version 4: [e654bfe8-1195-44e2-9f56-bb9ec83fe14e]

@fabricereix
Copy link
Collaborator

This must be related to #1469
You now have to get explicitly the first element, simple add nth 0 after your jsonpath query

client_id: jsonpath "$[?(@.clientId=='{{client_id}}')].id" nth 0

@janstrohschein
Copy link

Thank you very much! That solved it 👍

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

No branches or pull requests

3 participants