From f1ea4860254bebefb363d2fda2e5a75fc6226f3e Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Tue, 1 Feb 2022 13:10:55 +0300 Subject: [PATCH] Fix escaping of integer indexes with multiple backslashes (#1288) --- CHANGELOG.md | 4 ++++ httpie/cli/nested_json.py | 24 ++++++++++++------------ tests/test_json.py | 22 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 045d406c..b5801045 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ This document records all notable changes to [HTTPie](https://httpie.io). This project adheres to [Semantic Versioning](https://semver.org/). +## Unreleased + +- Fixed escaping of integer indexes with multiple backslashes in the nested JSON builder. ([#1285](https://github.com/httpie/httpie/issues/1285)) + ## [3.0.2](https://github.com/httpie/httpie/compare/3.0.1...3.0.2) (2022-01-24) [What’s new in HTTPie for Terminal 3.0 →](https://httpie.io/blog/httpie-3.0.0) diff --git a/httpie/cli/nested_json.py b/httpie/cli/nested_json.py index 501e3b59..beb52058 100644 --- a/httpie/cli/nested_json.py +++ b/httpie/cli/nested_json.py @@ -88,18 +88,18 @@ def tokenize(source: str) -> Iterator[Token]: return None value = ''.join(buffer) - for variation, kind in [ - (int, TokenKind.NUMBER), - (check_escaped_int, TokenKind.TEXT), - ]: - try: - value = variation(value) - except ValueError: - continue - else: - break - else: - kind = TokenKind.TEXT + kind = TokenKind.TEXT + if not backslashes: + for variation, kind in [ + (int, TokenKind.NUMBER), + (check_escaped_int, TokenKind.TEXT), + ]: + try: + value = variation(value) + except ValueError: + continue + else: + break yield Token( kind, value, start=cursor - (len(buffer) + backslashes), end=cursor diff --git a/tests/test_json.py b/tests/test_json.py index b454c034..7b4dff41 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -397,6 +397,28 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value): '2012': {'x': 2, '[3]': 4}, }, ), + ( + [ + r'a[\0]:=0', + r'a[\\1]:=1', + r'a[\\\2]:=2', + r'a[\\\\\3]:=3', + r'a[-1\\]:=-1', + r'a[-2\\\\]:=-2', + r'a[\\-3\\\\]:=-3', + ], + { + "a": { + "0": 0, + r"\1": 1, + r"\\2": 2, + r"\\\3": 3, + "-1\\": -1, + "-2\\\\": -2, + "\\-3\\\\": -3, + } + } + ), ], ) def test_nested_json_syntax(input_json, expected_json, httpbin):