Skip to content

"Invalid ref" error when generating server with cross-file reference when "--keep-spec-order" specified #2216

@mdelvecchio-wasabi

Description

@mdelvecchio-wasabi

Problem statement

When I generate a server from a complete YAML file, I am able to validate the document and generate the server.

When I move an object definition to a separate YAML file in the same directory, and use a cross-file reference to the definition, validation passes. But server generation fails when the "--keep-spec-order" option is specified. It succeeds when it is not.

Please remove the sections that don't apply

Swagger specification

First, the complete document without external references, which works:

swagger: '2.0'
info:
  title: Test API
  description: Test
  version: v1
host: host
schemes:
- https
- http
basePath: /path
produces:
- application/json

paths:
  /obj:
    get:
      summary: Get an object
      operationId: getObject
      responses:
        200:
          description: The object
          schema:
            $ref: '#/definitions/MyObject'

definitions:
  MyObject:
    title: My object
    type: object
    properties:
      string1:
        type: string
        title: A string
      bool1:
        type: boolean
        title: A boolean
    default:
      string1: "first"
      bool1:   false

Next, the document split into two, starting with the main document with an external reference:

swagger: '2.0'
info:
  title: Test API
  description: Test
  version: v1
host: host
schemes:
- https
- http
basePath: /path
produces:
- application/json

paths:
  /obj:
    get:
      summary: Get an object
      operationId: getObject
      responses:
        200:
          description: The object
          schema:
            $ref: './test-definitions.yaml#/definitions/MyObject'

And next, the test-definitions.yaml file, which is located in the same directory:

definitions:
  MyObject:
    title: My object
    type: object
    properties:
      string1:
        type: string
        title: A string
      bool1:
        type: boolean
        title: A boolean
    default:
      string1: "first"
      bool1:   false

Steps to reproduce

Validation succeeds in both cases:

$ ./go-swagger validate test.yaml
2020/02/06 08:43:13 
The swagger spec at "test.yaml" is valid against swagger specification 2.0

And basic server generation also succeeds in both cases:

$ ./go-swagger generate server -f test.yaml -t ./testout
...
2020/02/06 08:47:11 Generation completed!

But when I add the --keep-spec-order flag, it fails on the external reference:

$ ./go-swagger generate server -f test.yaml -t ./testout --keep-spec-order
2020/02/06 08:48:21 validating spec /tmp/test.yaml940091833
The swagger spec at "/tmp/test.yaml940091833" is invalid against swagger specification 2.0. see errors :
- invalid ref "./test-definitions.yaml#/definitions/MyObject"

The error message says the swagger spec is in the /tmp directory, so I assume that the relative reference is not being resolved properly to the current directory. If I copy test-definitions.yaml to /tmp, generation succeeds.

Environment

swagger version: v0.21.0
go version: go1.11.5 linux/amd64
OS: Ubuntu Linux 16

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions