Skip to content

Commit

Permalink
Eval template in JSON object key
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Jan 26, 2025
1 parent 2d14abe commit 0268b08
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion integration/hurl/tests_ok/post_json.curl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
curl --header 'Content-Type: application/json' --data $'{\n "name": "Bob",\n "password": "&secret\\\\\'<>",\n "age": 30,\n "strict": true,\n "spacing": "\\n",\n "g_clef": "\\uD834\\uDD1E",\n "items": [true, "true", 1],\n "variable": "\\\\",\n "": "empty"\n}' 'http://localhost:8000/post-json'
curl --header 'Content-Type: application/json' --data $'{\n "name": "Bob",\n "password": "&secret\\\\\'<>",\n "age": 30,\n "strict": true,\n "spacing": "\\n",\n "g_clef": "\\uD834\\uDD1E",\n "items": [true, "true", 1],\n "variable": "\\\\",\n "": "empty",\n "dynamic_key": "dynamic_key"\n}' 'http://localhost:8000/post-json'
curl --header 'Content-Type: application/json' --data '[1,2,3]' 'http://localhost:8000/post-json-array'
curl --header 'Content-Type: application/json' --data '[]' 'http://localhost:8000/post-json-array-empty'
curl --header 'Content-Type: application/json' --data '"Hello"' 'http://localhost:8000/post-json-string'
Expand Down
3 changes: 2 additions & 1 deletion integration/hurl/tests_ok/post_json.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ POST http://localhost:8000/post-json
"g_clef": "\uD834\uDD1E",
"items": [true, "true", 1],
"variable": "{{string_variable}}",
"": "empty"
"": "empty",
"{{key}}": "dynamic_key"
}
HTTP 200

Expand Down
2 changes: 1 addition & 1 deletion integration/hurl/tests_ok/post_json.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl --variable age=30 --variable strict=true --variable string_variable=`\ tests_ok/post_json.hurl
hurl --variable age=30 --variable strict=true --variable string_variable=`\ --variable key=dynamic_key tests_ok/post_json.hurl
3 changes: 2 additions & 1 deletion integration/hurl/tests_ok/post_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def post_json():
"g_clef": "\\uD834\\uDD1E",
"items": [true, \"true\", 1],
"variable": "\\\\",
"": "empty"
"": "empty",
"dynamic_key": "dynamic_key"
}"""
)
return ""
Expand Down
2 changes: 1 addition & 1 deletion integration/hurl/tests_ok/post_json.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl --variable age=30 --variable strict=true --variable string_variable=\\ tests_ok/post_json.hurl
hurl --variable age=30 --variable strict=true --variable string_variable=\\ --variable key=dynamic_key tests_ok/post_json.hurl
5 changes: 4 additions & 1 deletion packages/hurl/src/runner/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use hurl_core::reader::Reader;
use crate::runner::error::{RunnerError, RunnerErrorKind};
use crate::runner::{expr, VariableSet};

use super::template::eval_template;

/// Evaluates a JSON value to a string given a set of `variables`.
/// If `keep_whitespace` is true, whitespace is preserved from the JSonValue, otherwise
/// it is trimmed.
Expand Down Expand Up @@ -112,11 +114,12 @@ fn eval_json_object_element(
variables: &VariableSet,
keep_whitespace: bool,
) -> Result<String, RunnerError> {
let name = eval_template(&element.name, variables)?;
let value = eval_json_value(&element.value, variables, keep_whitespace)?;
if keep_whitespace {
Ok(format!(
"{}\"{}\"{}:{}{}{}",
element.space0, element.name, element.space1, element.space2, value, element.space3
element.space0, name, element.space1, element.space2, value, element.space3
))
} else {
Ok(format!("\"{}\":{}", element.name, value))
Expand Down

0 comments on commit 0268b08

Please sign in to comment.