Swagger-editor: Parser: YAML anchors & aliases depend on declaration order

Created on 27 Aug 2015  路  2Comments  路  Source: swagger-api/swagger-editor

Hi

This following example works:

    swagger: '2.0'

    info:
      version: "0.0.1"
      title: API

    definitions:      
      OperationType:
        type: string
        enum: &OperationType
        - registration

    # Describe your paths here
    paths:
      /checker:
        get:
          parameters:
            - name: operations
              in: query
              type: array
              items:
                type: string
                enum: *OperationType
              default: [registration]
          responses:
            200:
              description: OK
              schema:
                $ref: '#/definitions/OperationType'

But if the order of paths: and definitions: is reversed, it results in the following error:

YAML Syntax Error
Unidentified alias "OperationType" at line 17, column 37: enum: *OperationType ^

This is probably an error in the YAML Parsing dependency, but I'm not sure.

Thanks!

question

Most helpful comment

Yes, in YAML you should define an alias before using it. If you put definitions after paths the OperationType anchor is used before being defined. I recommend defining all of your anchors on top under a free-form swagger extension

swagger: '2.0'

info:
  version: "0.0.1"
  title: API

x-types:
  OperationType: &OperationType
    - registration

# Describe your paths here
paths:
  /checker:
    get:
      parameters:
        - name: operations
          in: query
          type: array
          items:
            type: string
            enum: *OperationType
          default: [registration]
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/OperationType'

definitions:      
  OperationType:
    type: string
    enum: *OperationType

All 2 comments

Yes, in YAML you should define an alias before using it. If you put definitions after paths the OperationType anchor is used before being defined. I recommend defining all of your anchors on top under a free-form swagger extension

swagger: '2.0'

info:
  version: "0.0.1"
  title: API

x-types:
  OperationType: &OperationType
    - registration

# Describe your paths here
paths:
  /checker:
    get:
      parameters:
        - name: operations
          in: query
          type: array
          items:
            type: string
            enum: *OperationType
          default: [registration]
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/OperationType'

definitions:      
  OperationType:
    type: string
    enum: *OperationType

Ah, thanks! I wasn't aware this was a YAML limitation (even though the examples I've found all declared the anchors first). Will follow your example and declare some lists up top.

Was this page helpful?
0 / 5 - 0 ratings