jeremyfa / yaml.js

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.
MIT License
887 stars 145 forks source link

Fails with explicit mapping keys #128

Open NathanHazout opened 5 years ago

NathanHazout commented 5 years ago

I tried to use "prettier.io" on my api-doc.yml.

Here is a simplified snippet of my original yml which works:

swagger: '2.0'
host: localhost:4000
basePath: /
info:
  title: Sample
  version: '3.0.0'
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
definitions:
  Error:
    additionalProperties: true
paths:
  /participant-organizations/{participantOrganizationId}/policies/validation/{recordType}: 
    parameters:
      - name: participantOrganizationId
        in: path
        type: string
        required: true
        description: OrganizationId
      - name: recordType
        in: path
        type: string
        required: true
        description: specified type of record
    put:
      tags:
        - Validation Policy
      description: Update validation policy for the record type
      operationId: updateValidationPolicy
      security:
        - Bearer: []
      parameters:
        - name: validation policy
          in: body
          schema:
            $ref: "#/definitions/ValidationPolicy"
      responses:
        201:
          description: validation policy was updated

After running the formatter, the yml looks like:

swagger: "2.0"
host: localhost:4000
basePath: /
info:
  title: Sample
  version: "3.0.0"
securityDefinitions:
  Bearer:
    type: apiKey
    name: Authorization
    in: header
definitions:
  Error:
    additionalProperties: true
paths:
  ? /participant-organizations/{participantOrganizationId}/policies/validation/{recordType}
  : parameters:
      - name: participantOrganizationId
        in: path
        type: string
        required: true
        description: OrganizationId
      - name: recordType
        in: path
        type: string
        required: true
        description: specified type of record
    put:
      tags:
        - Validation Policy
      description: Update validation policy for the record type
      operationId: updateValidationPolicy
      security:
        - Bearer: []
      parameters:
        - name: validation policy
          in: body
          schema:
            $ref: "#/definitions/ValidationPolicy"
      responses:
        201:
          description: validation policy was updated

Notice the syntax ? /participant-organizations/{participantOrganizationId}/policies/validation/{recordType}?

According to this thread, this is valid syntax: https://github.com/prettier/prettier/issues/5599#issuecomment-490083406

However, after these changes my server does not start:

/Users/nathanh/Git/my-project/services/core/node_modules/yamljs/lib/Parser.js:270
        throw new ParseException('Unable to parse.', this.getRealCurrentLineNb() + 1, this.currentLine);
        ^
Error: Unable to parse.
npm ERR! code ELIFECYCLE
kaylakantola commented 5 years ago

Hey, I just ran into this issue as well. For me it was a prettier/prettier-atom config issue. To solve it, I added an .eslintignore file to my project root, and included this to have prettier ignore yaml files :

**/*.yaml

I had to also ensure that in prettier-atom, I had set "respectEslintignore" to true in prettier-atom/config-schema.json. It defaults to true, but that's a place to look if the above solution doesn't work right away.

jeremyfa commented 5 years ago

This is indeed valid syntax, but yaml.js is not handling all the spec, and there is no plan to change this. You could use https://github.com/nodeca/js-yaml instead, which is more feature complete, if you do need this syntax to work.