Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping keys do not have to be a string, and may be a number #34

Closed
BigBlueHat opened this issue Jan 13, 2020 · 8 comments
Closed

Mapping keys do not have to be a string, and may be a number #34

BigBlueHat opened this issue Jan 13, 2020 · 8 comments
Labels
bug Something isn't working

Comments

@BigBlueHat
Copy link

BigBlueHat commented Jan 13, 2020

Describe the bug
It looks like a JSON requirement has snuk into this YAML parser.

On line 335 of parseWithPointers.ts the following error is thrown:

mapping key must be a string scalar rather than a number

However, I'm not able to find that requirement in the YAML 1.2 spec, nor in various other YAML parsers I've tried.

JSON can't handle {5: "five"}, but YAML seems happy to deal with 5: five (for example), so this feels like a "leak" from JSON-land back "up" into YAML land.

Numbers as keys show up frequently in OpenAPI documents...and really shouldn't be throwing errors (per YAML):

paths:
  /:
    get:
      responses:
        200:
          description: woot!

Expected Behavior
YAML allows numbers as mapping keys, so this library shouldn't throw an error.

Cheers!
🎩

@BigBlueHat BigBlueHat added the bug Something isn't working label Jan 13, 2020
@P0lip
Copy link
Contributor

P0lip commented Jan 15, 2020

@BigBlueHat

However, I'm not able to find that requirement in the YAML 1.2 spec, nor in various other YAML parsers I've tried.

You are right. That's not a requirement in YAML. In fact, a mappings with string scalar and a mapping with a numeric scalar are two different properties. YAML can even take complex values as a mapping key, so it get can even more quirky.

JSON, on the other hand, can only take strings as keys.

JSON can't handle {5: "five"}, but YAML seems happy to deal with 5: five (for example), so this feels like a "leak" from JSON-land back "up" into YAML land.

Yeah, we treat YAML in a JSON-ish way by default inside of Studio.

@BigBlueHat
Copy link
Author

Gotcha. Might be good to note that caveat in this package as it makes it less of a "pure" YAML parser/processor and more of a JSON-focused YAML wrangler thing. 😃

Thanks for the explanation!

@Ravikumar109
Copy link

Is there a possibility of overwriting the error messages for yaml when linting and customizing the rules using spectral

@P0lip
Copy link
Contributor

P0lip commented Jun 30, 2022

That is not possible at the moment.

@Ravikumar109
Copy link

Ravikumar109 commented Jun 30, 2022

@P0lip i have added '' for the http codes in the raml specs and it is not throwing any error at line 64 but if it not used like having 201 at 76th line it is throwing error due to the validation done by spectral but with the error it is still a valid yaml so from my understand from the above statement to resolve the error messages i need to use quote as customization is not possible. Is that correct statement
Number Error
raml specs

@P0lip
Copy link
Contributor

P0lip commented Jun 30, 2022

Is that correct statement

Correct, you need to use quotes everywhere to resolve the errors.

@dcal
Copy link

dcal commented Mar 23, 2023

Is this something that could be fixed? It feels weird to have to adapt valid YAML in projects just for the sake of satisfying a quirk of the parser.

@publicvirtualvoid
Copy link

I have quite a lot of external $refs and can't really go and update everything - is there a way to turn this behaviour off? Given that this is valid OpenAPI spec for HTTP responses it's kind of a big use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants