riok / Kreya

Kreya is a GUI client for gRPC and REST APIs with innovative features for environments, authorizations and more.
https://kreya.app
279 stars 5 forks source link

Referencing user variables not working #204

Closed DominicMCN closed 2 months ago

DominicMCN commented 4 months ago

Describe the bug

Referencing a stored user variables in grpc payload is not working

To Reproduce Steps to reproduce the behavior:

  1. Make a GRPC request and store a field as variable
  kreya.variables.set('example', msg.content.example);
});
  1. Make another GRPC request, attempting to use stored field in body

    {
    "example": {{vars.example}}
    }
  2. The request is not valid JSON

Expected behavior {{vars.example}} is replaced by content of the stored variable

Environment (if possible, copy the information from the error dialog or the About menu):

Additional context I tried different variable type: object, string... None worked

ni507 commented 4 months ago

You need to add double quotes around the variable for the json to be valid.

{
  "example": "{{vars.example}}"
}
DominicMCN commented 4 months ago

Also does not work for me, still get JSON error

'F' is invalid after a value. Expected either ',', '}', or ']'. Path: $ | LineNumber: 35 | BytePositionInLine: 44.

That's for example of type string. What if i want to set the whole object?

ni507 commented 4 months ago

You can store an object in a user variable, but you must convert it to a valid json format yourself.

Script: kreya.variables.set("example", { total: 42, average: 23, message: 'hej', title: "test"});

Request json:

{
  "example-string": "{{vars.example.message}}",
  "example-number": {{vars.example.total}}, 
  "example-object": {
    "average": {{vars.example.average}},
    "title": "{{vars.example.title}}"
  }
}

Note: On the line "example-number", the editor displays an error because it does not know if a number is stored in the variable, but the execution works.

Does this help? Otherwise, you can provide us the stored values in your variable so we can help you further.

If your variable contains a quote " and you want to apply this in a template like this { "my-message": "{{ vars.my_string }}" }, this leads to an error since the JSON is no longer valid. You could work around this by replacing the quote: { "my-message": "{{ vars.my_string }} | string.replace '\"' '\\u0022'" }

DominicMCN commented 4 months ago

Hmm. Yeah I do have quotes in my variable. But it seems to still fail with the work-around. Is there a way to view the rendered json/request after templating so I can debug it?

Also, would it be possible to use the whole object in templating? Given your example

kreya.variables.set("example", { total: 42, average: 23, message: 'hej', title: "test"});

I would want to do something like

{
  "example-object": {{vars.example}}

}

as many of my requests contain the whole output of previous requests

CommonGuy commented 4 months ago

Sadly, viewing the rendered request is currently not possible. I added it to our backlog.

For sending the whole object as JSON, maybe something like this could help?

{
  "example-object": {{vars.example | object.to_json}}
}

This the object.to_json function is documented here: https://github.com/scriban/scriban/blob/master/doc/builtins.md#objectto_json

DominicMCN commented 3 months ago

I got Scriban template error

<input>(12,32) : error : The function `object.to_json` was not found

Seems to be in a recent version of Scriban. I guess I'll have to wait for your dependency update.

Also not sure why string.replace is not working for me, I tried | string.replace '\"' 'gibberish' and the quote seems to still be there

CommonGuy commented 3 months ago

True, the newer Scriban version might not be yet included in your Kreya version.

Very strange that string.replace doesn't work... As a further debugging tool, you can try to log the variable content in Scripting with kreya.trace(JSON.stringify(kreya.variables.get('example'))). This shows up in the Trace tab

DominicMCN commented 3 months ago

Well the variable displays correctly as a string contain a JSON object

"{\"example\": 1}

Now it's working if i directly replace it in script like

 kreya.variables.set('example', msg.content.example.replaceAll('\"', '\\u0022'))

But not with the scriban string.replace thingey.

DominicMCN commented 3 months ago

Ah @CommonGuy , I see. From your example it should have been

{ "my-message": "{{ vars.my_string  | string.replace '\"' '\\u0022'}}" }

Just misplaced }}. Anw it works now, waiting for the Scriban update. ty!

CommonGuy commented 3 months ago

Ah nice that you got it to work!

I'll add "show rendered request" to our roadmap, since that would've helped to debug this

DominicMCN commented 3 months ago

@CommonGuy will there be a version with a newer Scriban version full (probably in beta / alpha?). I'm keen to write some tests using the new collection feature but it's pretty pointless without chaining requests. Many of our requests depend on the whole response of previous requests and it's a bit cumbersome just taking each field out.

CommonGuy commented 2 months ago

A new alpha version was just released, which includes the latest Scriban version. A beta release is expected next week, with the stable release following shortly after

DominicMCN commented 2 months ago

Actually the new object functions are not in the latest release of scriban yet. Back to waiting. Thanks for the effort!


THey just pushed a new tag (5.10.0). Would be really grateful if you up the dep again

DominicMCN commented 2 months ago

@CommonGuy just saw a new alpha but still older Scriban. Sorry for too much tagging but just in case the issue get lost :)

CommonGuy commented 2 months ago

@DominicMCN No problem, we just released a new beta version for you, which includes the latest Scriban tag :)

DominicMCN commented 2 months ago

@CommonGuy holy crap all my end-to-end testing script suddenly works now. Thanks a lot guys! Script is actually really useful now

CommonGuy commented 2 months ago

Glad it works for you!