From 724223f184c0888c9a145360cdd4333fa6d21acc Mon Sep 17 00:00:00 2001 From: Synvoya <16019863+Synvoya@users.noreply.github.com> Date: Thu, 2 Jul 2026 01:41:50 +1000 Subject: [PATCH] Fix lexer selection priority in get_lexer() When a response's structured-syntax MIME type has a `+` suffix (e.g. `application/yaml+python`) and both the subtype name and the suffix resolve to a Pygments lexer, the subtype name should win: it is added to `lexer_names` first and tried first, mirroring the sibling MIME-type loop above. The lexer-name loop had no `break`, so it kept overwriting `lexer` and returned the last match (the suffix) instead of the first. Add the missing `break`. Realistic structured-suffix types (`+json`, `+xml`, `+yaml`) were unaffected because their name part is not a lexer, so last- and first-match coincided; the bug only surfaces when both parts are distinct valid lexers. --- httpie/output/formatters/colors.py | 1 + tests/test_output.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/httpie/output/formatters/colors.py b/httpie/output/formatters/colors.py index 0d9bd12e2f..70797ea2eb 100644 --- a/httpie/output/formatters/colors.py +++ b/httpie/output/formatters/colors.py @@ -174,6 +174,7 @@ def get_lexer( for name in lexer_names: try: lexer = pygments.lexers.get_lexer_by_name(name) + break except ClassNotFound: pass diff --git a/tests/test_output.py b/tests/test_output.py index 2242177dbc..ad2075d899 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -235,6 +235,10 @@ class TestColors: ('foo/json-foo', False, None, 'JSON'), ('foo/x-json', False, None, 'JSON'), ('application/vnd.comverge.grid+hal+json', False, None, 'JSON'), + # When both the subtype name and the `+`-suffix resolve to a lexer, + # the name must win (it is tried first). Previously the lexer-name + # loop had no break, so the last match -- the suffix -- won instead. + ('application/yaml+python', False, None, 'YAML'), ('text/plain', True, '{}', 'JSON'), ('text/plain', True, 'foo', 'Text only'), ]