Skip to content

Commit eb5cb5b

Browse files
authored
fix: round-trip integers that stringify in exponential notation (#771)
Integers at or above 1e21 (and Number.MAX_VALUE) stringify via Number.prototype.toString(10) in exponential notation, e.g. 1e21 -> "1e+21". The int tags identified these numbers (1e21 % 1 === 0) and represented them with that exponential text, producing output like `!!int '1e+21'`. That text matches no `!!int` resolution pattern, so loading the library's own dump output threw "cannot resolve a node with !<tag:yaml.org,2002:int> explicit tag". Per YAML 1.2.2 the Core (10.3) and JSON (10.2) `!!int` resolution accepts only [-+]?[0-9]+ (plus 0o/0x forms); exponential text is a float, not an int. Make the int tags reject numbers whose decimal stringification contains an exponent, and let the float tags claim them instead. The float `represent` already emits a valid float form (1.e+21) for such values, so they now round-trip. Restores the "numbers survive round-trip" invariant (#737) for large integers across the default, Core, JSON and YAML 1.1 schemas.
1 parent 89024c4 commit eb5cb5b

7 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎src/tag/scalar/float_core.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ const floatCoreTag = defineScalarTag('tag:yaml.org,2002:float', {
5151
implicitFirstChars: ['-', '+', '.', ...'0123456789'],
5252
resolve: resolveYamlFloat,
5353
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
54-
(object % 1 !== 0 || Object.is(object, -0)),
54+
// Also claim integer-valued numbers that stringify in exponential notation
55+
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
56+
// valid float form for them.
57+
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
5558
represent: representYamlFloat
5659
})
5760

‎src/tag/scalar/float_json.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ const floatJsonTag = defineScalarTag('tag:yaml.org,2002:float', {
5757
implicitFirstChars: ['-', ...'0123456789'],
5858
resolve: resolveYamlFloat,
5959
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
60-
(object % 1 !== 0 || Object.is(object, -0)),
60+
// Also claim integer-valued numbers that stringify in exponential notation
61+
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
62+
// valid float form for them.
63+
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
6164
represent: representYamlFloat
6265
})
6366

‎src/tag/scalar/float_yaml11.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ const floatYaml11Tag = defineScalarTag('tag:yaml.org,2002:float', {
5858
implicitFirstChars: ['-', '+', '.', ...'0123456789'],
5959
resolve: resolveYamlFloat,
6060
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
61-
(object % 1 !== 0 || Object.is(object, -0)),
61+
// Also claim integer-valued numbers that stringify in exponential notation
62+
// (>= 1e21), since their `!!int` text would be invalid; `represent` emits a
63+
// valid float form for them.
64+
(object % 1 !== 0 || Object.is(object, -0) || /e/i.test(object.toString(10))),
6265
represent: representYamlFloat
6366
})
6467

‎src/tag/scalar/int_core.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ const intCoreTag = defineScalarTag('tag:yaml.org,2002:int', {
5454
implicitFirstChars: ['-', '+', ...'0123456789'],
5555
resolve: resolveYamlInteger,
5656
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
57-
(object % 1 === 0 && !Object.is(object, -0)),
57+
// Large integers (>= 1e21) stringify in exponential notation, which is not
58+
// valid `!!int` text. Reject them here so they fall through to the float tag.
59+
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
5860
represent: (object: number) => object.toString(10)
5961
})
6062

‎src/tag/scalar/int_json.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ const intJsonTag = defineScalarTag('tag:yaml.org,2002:int', {
4949
implicitFirstChars: ['-', ...'0123456789'],
5050
resolve: resolveYamlInteger,
5151
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
52-
(object % 1 === 0 && !Object.is(object, -0)),
52+
// Large integers (>= 1e21) stringify in exponential notation, which is not
53+
// valid `!!int` text. Reject them here so they fall through to the float tag.
54+
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
5355
represent: (object: number) => object.toString(10)
5456
})
5557

‎src/tag/scalar/int_yaml11.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ const intYaml11Tag = defineScalarTag('tag:yaml.org,2002:int', {
4848
implicitFirstChars: ['-', '+', ...'0123456789'],
4949
resolve: resolveYamlInteger,
5050
identify: (object) => Object.prototype.toString.call(object) === '[object Number]' &&
51-
(object % 1 === 0 && !Object.is(object, -0)),
51+
// Large integers (>= 1e21) stringify in exponential notation, which is not
52+
// valid `!!int` text. Reject them here so they fall through to the float tag.
53+
(object % 1 === 0 && !Object.is(object, -0) && !/e/i.test(object.toString(10))),
5254
represent: (object: number) => object.toString(10)
5355
})
5456

‎test/core/tags/int.test.mjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ describe('tags/int', () => {
3939
assert.deepStrictEqual(load(dump(expected, { schema }), { schema }), expected)
4040
})
4141

42+
it(`${name} round-trip of large integers`, () => {
43+
// Integers at or above 1e21 stringify in exponential notation
44+
// ('1e+21'), which is not valid `!!int` text. They must round-trip
45+
// through the float tag rather than being dumped as `!!int '1e+21'`.
46+
const large = [1e21, 1.5e21, -1e21, 1e100, Number.MAX_VALUE]
47+
48+
for (const value of large) {
49+
assert.strictEqual(load(dump(value, { schema }), { schema }), value)
50+
}
51+
})
52+
4253
it(`${name} fail explicit tag`, () => {
4354
assert.throws(() => load('!!int 1.5', { schema }), /cannot resolve/)
4455
})

0 commit comments

Comments
 (0)