Skip to content

Fix #701: quote YAML 1.1 number-form Strings under ALWAYS_QUOTE_NUMBERS_AS_STRINGS - #705

Merged
cowtowncoder merged 4 commits into
FasterXML:2.21from
seonwooj0810:fix/issue-701-minimize-quotes-number-forms
Jul 24, 2026
Merged

Fix #701: quote YAML 1.1 number-form Strings under ALWAYS_QUOTE_NUMBERS_AS_STRINGS#705
cowtowncoder merged 4 commits into
FasterXML:2.21from
seonwooj0810:fix/issue-701-minimize-quotes-number-forms

Conversation

@seonwooj0810

Copy link
Copy Markdown
Contributor

Fixes #701

Root cause

With MINIMIZE_QUOTES + ALWAYS_QUOTE_NUMBERS_AS_STRINGS enabled, YAMLGenerator.writeString decides whether a String must be quoted by matching it against PLAIN_NUMBER_P ([+-]?[0-9]*(\.[0-9]*)?). That regex only recognizes plain decimal integers/floats, so YAML 1.1 number forms it does not match — exponent (1e5), hex (0x1F), underscore (12_34) — were emitted unquoted. SnakeYAML (and every other YAML reader) then resolves those plain scalars back to numbers, so the value silently changes on a read/write cycle ("1e5" -> 100000.0). The feature is documented to keep number-like Strings distinguishable, but its detection was incomplete.

Change

Detection now also matches SnakeYAML's own implicit int/float resolver patterns (Resolver.INT / Resolver.FLOAT) — the exact patterns the parser uses to resolve plain scalars — in addition to the historical PLAIN_NUMBER_P (kept, so existing output is a strict superset and no previously-quoted value becomes unquoted). Any String that would resolve to a number is therefore quoted and round-trips. Scoped to the opt-in ALWAYS_QUOTE_NUMBERS_AS_STRINGS path; default MINIMIZE_QUOTES output is unchanged.

Test evidence

Added GeneratorWithMinimizeTest.testQuoteYAML11NumberFormsStoredAsString701: asserts 1e5, 0x1F, 12_34, 1.5e-3, 0b101 are now quoted and round-trip through readTree, while a genuine non-number version String (2.0.1.2.3) stays unquoted. The test fails before this change (1e5 emitted as key: 1e5) and passes after.

Verification done: full yaml module suite green (./mvnw -pl yaml test — 168 tests, 0 failures), including the pre-existing ALWAYS_QUOTE_NUMBERS_AS_STRINGS tests; the new test confirmed red-before/green-after.

@cowtowncoder

Copy link
Copy Markdown
Member

Could this be re-created against 2.21 branch?

…OTE_NUMBERS_AS_STRINGS

PLAIN_NUMBER_P did not match exponent (1e5), hex (0x1F) or underscore
(12_34) forms, so those Strings were emitted unquoted and silently
re-read as numbers. Also match against SnakeYAML's own int/float
resolver patterns so every String that would resolve to a number is
quoted and round-trips.
@seonwooj0810
seonwooj0810 force-pushed the fix/issue-701-minimize-quotes-number-forms branch from 8f15717 to 936f10f Compare July 23, 2026 13:08
@seonwooj0810
seonwooj0810 changed the base branch from 2.x to 2.21 July 23, 2026 13:08
@seonwooj0810

Copy link
Copy Markdown
Contributor Author

Done — rebased the fix onto 2.21 and re-targeted this PR's base branch to 2.21. Cherry-picked cleanly (only the two files: YAMLGenerator.java +34/-1 and GeneratorWithMinimizeTest.java +26), and GeneratorWithMinimizeTest passes on 2.21 (14/14). Let me know if you'd prefer a brand-new PR instead of the re-target.

@cowtowncoder
cowtowncoder merged commit 9532673 into FasterXML:2.21 Jul 24, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants