eemeli / yaml

YAML parser and stringifier for JavaScript
https://eemeli.org/yaml
ISC License
1.3k stars 113 forks source link

YAML 1.1 Pair, Key is `y`, get parsed to `true` (bool) #574

Closed sharky98 closed 3 weeks ago

sharky98 commented 1 month ago

Describe the bug When parsing a Unity YAML file, one object is defined with key-value pairs, including one with a y in it (like in X,Y coordinates). When parsed, the key is transformed to a boolean true.

To Reproduce

Using the Playground, parse the following YAML

%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!213 &21300000
Sprite:
  m_Rect:
    x: 0
    y: 128
    width: 128
    height: 128

gives the following result:

[
  Pair {
    key: Scalar { value: 'x', range: [Array], source: 'x', type: 'PLAIN' },
    value: Scalar { value: 0, range: [Array], source: '0', type: 'PLAIN' }
  },
  Pair {
    key: Scalar { value: true, range: [Array], source: 'y', type: 'PLAIN' },
    value: Scalar { value: 128, range: [Array], source: '128', type: 'PLAIN' }
  },
  Pair {
    key: Scalar { value: 'width', range: [Array], source: 'width', type: 'PLAIN' },
    value: Scalar { value: 128, range: [Array], source: '128', type: 'PLAIN' }
  },
  Pair {
    key: Scalar { value: 'height', range: [Array], source: 'height', type: 'PLAIN' },
    value: Scalar { value: 128, range: [Array], source: '128', type: 'PLAIN' }
  }
]

Expected behaviour The key of a key-value pair, should not be transformed according to the boolean rule.

Versions (please complete the following information):

Additional context

I found the schema used, but I have no clues where it get used when parsing the key.

https://github.com/eemeli/yaml/blob/5adbb605b64094b57bc95cbc24587e6f36b3f9a8/src/schema/yaml-1.1/bool.ts

sharky98 commented 1 month ago

TIL, this is by design for YAML 1.1 that the keys are parsed like anything else. Damn YAML is complex!

https://ruudvanasseldonk.com/2023/01/11/the-yaml-document-from-hell#non-string-keys

eemeli commented 1 month ago

Yeah, this seems to be an uncomfortably common stumbling block. It might make sense to add an option like stringKeys to enforce their parsing as such.