From ce66121d03713250f281b477d05bd85cfcfe7ba2 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Wed, 9 Mar 2022 09:51:17 +0100 Subject: [PATCH] Allow different types in JSON array --- integration/tests_ok/post_json.curl | 2 +- integration/tests_ok/post_json.html | 3 +- integration/tests_ok/post_json.hurl | 3 +- integration/tests_ok/post_json.json | 2 +- integration/tests_ok/post_json.py | 3 +- packages/hurl_core/src/parser/json.rs | 58 +++------------------------ 6 files changed, 13 insertions(+), 58 deletions(-) diff --git a/integration/tests_ok/post_json.curl b/integration/tests_ok/post_json.curl index 8a231d1c6..4fe6ebfdf 100644 --- a/integration/tests_ok/post_json.curl +++ b/integration/tests_ok/post_json.curl @@ -1,4 +1,4 @@ -curl 'http://localhost:8000/post-json' -H '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}' +curl 'http://localhost:8000/post-json' -H '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}' curl 'http://localhost:8000/post-json-array' -H 'Content-Type: application/json' --data '[1,2,3]' curl 'http://localhost:8000/post-json-string' -H 'Content-Type: application/json' --data '"Hello"' curl 'http://localhost:8000/post-json-number' -H 'Content-Type: application/json' --data '100' diff --git a/integration/tests_ok/post_json.html b/integration/tests_ok/post_json.html index 6023c503a..b15c9ecfb 100644 --- a/integration/tests_ok/post_json.html +++ b/integration/tests_ok/post_json.html @@ -6,7 +6,8 @@ "age": 30, "strict": true, "spacing": "\n", - "g_clef": "\uD834\uDD1E" + "g_clef": "\uD834\uDD1E", + "items": [true, "true", 1] } HTTP/1.0 200 diff --git a/integration/tests_ok/post_json.hurl b/integration/tests_ok/post_json.hurl index d33d4e5d7..5ca579645 100644 --- a/integration/tests_ok/post_json.hurl +++ b/integration/tests_ok/post_json.hurl @@ -6,7 +6,8 @@ POST http://localhost:8000/post-json "age": 30, "strict": true, "spacing": "\n", - "g_clef": "\uD834\uDD1E" + "g_clef": "\uD834\uDD1E", + "items": [true, "true", 1] } HTTP/1.0 200 diff --git a/integration/tests_ok/post_json.json b/integration/tests_ok/post_json.json index 6f3f6f9d3..f49275829 100644 --- a/integration/tests_ok/post_json.json +++ b/integration/tests_ok/post_json.json @@ -1 +1 @@ -{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-json","body":{"type":"json","value":{"name":"Bob","password":"&secret<>","age":30,"strict":true,"spacing":"\n","g_clef":"ƻǿ"}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-array","body":{"type":"json","value":[1,2,3]}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-string","body":{"type":"json","value":"Hello"}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-number","body":{"type":"json","value":100}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-numbers","body":{"type":"json","value":{"natural":100,"negative":-1,"float":"3.333333333333333","exponent":100e100}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-boolean","body":{"type":"json","value":true}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/get-name"},"response":{"version":"HTTP/1.0","status":200,"captures":[{"name":"name","query":{"type":"body"}}]}},{"request":{"method":"POST","url":"http://localhost:8000/check_name","body":{"type":"json","value":{"name":"{{name}}"}}}}]} \ No newline at end of file +{"entries":[{"request":{"method":"POST","url":"http://localhost:8000/post-json","body":{"type":"json","value":{"name":"Bob","password":"&secret<>","age":30,"strict":true,"spacing":"\n","g_clef":"ƻǿ","items":[true,"true",1]}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-array","body":{"type":"json","value":[1,2,3]}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-string","body":{"type":"json","value":"Hello"}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-number","body":{"type":"json","value":100}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-numbers","body":{"type":"json","value":{"natural":100,"negative":-1,"float":"3.333333333333333","exponent":100e100}}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"POST","url":"http://localhost:8000/post-json-boolean","body":{"type":"json","value":true}},"response":{"version":"HTTP/1.0","status":200}},{"request":{"method":"GET","url":"http://localhost:8000/get-name"},"response":{"version":"HTTP/1.0","status":200,"captures":[{"name":"name","query":{"type":"body"}}]}},{"request":{"method":"POST","url":"http://localhost:8000/check_name","body":{"type":"json","value":{"name":"{{name}}"}}}}]} \ No newline at end of file diff --git a/integration/tests_ok/post_json.py b/integration/tests_ok/post_json.py index 5e058381c..a87bbc0e6 100644 --- a/integration/tests_ok/post_json.py +++ b/integration/tests_ok/post_json.py @@ -15,7 +15,8 @@ def post_json(): "age": 30, "strict": true, "spacing": "\\n", - "g_clef": "\\uD834\\uDD1E" + "g_clef": "\\uD834\\uDD1E", + "items": [true, \"true\", 1] }""" ) return "" diff --git a/packages/hurl_core/src/parser/json.rs b/packages/hurl_core/src/parser/json.rs index ec8d32021..4197769fd 100644 --- a/packages/hurl_core/src/parser/json.rs +++ b/packages/hurl_core/src/parser/json.rs @@ -245,8 +245,8 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> { // at least one element if !reader.remaining().starts_with(']') { - let first_element = list_element(None, reader)?; - elements.push(first_element.clone()); + let first_element = list_element(reader)?; + elements.push(first_element); loop { if reader.remaining().starts_with(']') { @@ -256,7 +256,7 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> { break; } literal(",", reader)?; - let element = list_element(Some(first_element.value._type()), reader)?; + let element = list_element(reader)?; elements.push(element); } } @@ -265,13 +265,9 @@ fn list_value(reader: &mut Reader) -> ParseResult<'static, JsonValue> { Ok(JsonValue::List { space0, elements }) } -fn list_element( - _type: Option, - reader: &mut Reader, -) -> ParseResult<'static, JsonListElement> { +fn list_element(reader: &mut Reader) -> ParseResult<'static, JsonListElement> { let save = reader.state.pos.clone(); let space0 = whitespace(reader); - let pos = reader.state.pos.clone(); let value = match parse(reader) { Ok(r) => r, Err(_) => { @@ -282,15 +278,6 @@ fn list_element( }) } }; - if let Some(t) = _type { - if t != value._type() { - return Err(error::Error { - pos, - recoverable: false, - inner: error::ParseError::Expecting { value: t }, - }); - } - } let space1 = whitespace(reader); Ok(JsonListElement { space0, @@ -776,17 +763,6 @@ mod tests { ); assert!(error.recoverable); - let mut reader = Reader::init("[1, true]"); - let error = list_value(&mut reader).err().unwrap(); - assert_eq!(error.pos, Pos { line: 1, column: 5 }); - assert_eq!( - error.inner, - error::ParseError::Expecting { - value: "number".to_string() - } - ); - assert!(!error.recoverable); - let mut reader = Reader::init("[1, 2,]"); let error = list_value(&mut reader).err().unwrap(); assert_eq!(error.pos, Pos { line: 1, column: 7 }); @@ -798,7 +774,7 @@ mod tests { fn test_list_element() { let mut reader = Reader::init("true"); assert_eq!( - list_element(None, &mut reader).unwrap(), + list_element(&mut reader).unwrap(), JsonListElement { space0: "".to_string(), value: JsonValue::Boolean(true), @@ -808,30 +784,6 @@ mod tests { assert_eq!(reader.state.cursor, 4); } - #[test] - fn test_list_element_error() { - let mut reader = Reader::init("true"); - let error = list_element(Some("number".to_string()), &mut reader) - .err() - .unwrap(); - assert_eq!(error.pos, Pos { line: 1, column: 1 }); - assert_eq!( - error.inner, - error::ParseError::Expecting { - value: "number".to_string() - } - ); - assert!(!error.recoverable); - - let mut reader = Reader::init("\n]"); - let error = list_element(Some("number".to_string()), &mut reader) - .err() - .unwrap(); - assert_eq!(error.pos, Pos { line: 1, column: 1 }); - assert_eq!(error.inner, error::ParseError::Json {}); - assert!(!error.recoverable); - } - #[test] fn test_object_value() { let mut reader = Reader::init("{}");