eemeli / yaml

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

$ref and values with hash (#) support #566

Closed olegsmetanin closed 1 month ago

olegsmetanin commented 3 months ago

Is your feature request related to a problem? Please describe. YAML.parse(...) for

test:
  foo:
    prop: hello
  bar:
    $ref: #/test/foo/prop

returns

{
  "test": {
    "foo": {
      "prop": "hello"
    },
    "bar": {
      "$ref": null
    }
  }
}

I have tried to parse openapi spec with $ref: #/foo/bar properties, it seems that # always acts as a comment and parse function always returns null for $ref property. Moreover, reviver function (key, value) => string already getting null for such values.

Describe the solution you'd like YAML.parse(str, reviver?, options = {}) could provide: 1) additional option to define the list of specific keys like ['$ref', ...] 2) additional optional key-value mapper "originalReviver" which will be called for every specific tag with original yaml value

Describe alternatives you've considered It seems that I have to preprocess yaml files, collect $refs values before YAML.parse() and patch result object with original values after YAML.parse()

eemeli commented 3 months ago

This sounds like an API layer you may build on top of the library, or find if one is already available for OpenAPI content. It's not a YAML feature, though, so not appropriate to include here directly.

Scalar values starting with # will, however, need to be quoted to not be parsed as comments.

eemeli commented 1 month ago

Closing as out of scope.