go-swagger: Client generation fails with `object has no key "UserBase"`

Problem statement

Client generation fails with object has no key "UserBase"

Swagger specification

2.0

Steps to reproduce

  1. Create two files first.yml
swagger: '2.0'

info:
  version: '2.0'
  title: test
  description: test
  license:
    name: N/A

basePath: /v2

schemes:
  - http
  - https

consumes:
  - application/json
produces:
  - application/json

paths:
  /user:
    get:
      operationId: getUser
      description: test
      responses:
        200:
          description: succes
          schema:
            $ref: 'second.yml#/definitions/User'

/second.yml

swagger: '2.0'

info:
  version: '2.0'
  title: test
  description: test
  license:
    name: N/A

basePath: /v2

schemes:
  - http
  - https

consumes:
  - application/json
produces:
  - application/json

definitions:
  UserBase:
    type: object
    properties:
      id:
        type: integer
        format: uint64

  User:
    allOf:
    - $ref: '#/definitions/UserBase'
    - properties:
        createdAt:
          type: string
          format: date-time
        updatedAt:
          type: string
          format: date-time
  1. run swagger generate client -f /Users/andrew/Workspace/go/src/github.com/go-swagger/example/first.yml -t /Users/andrew/Workspace/go/src/github.com/go-swagger/example/gen --skip-validation
  2. swagger returns object has no key "UserBase"

Environment

swagger version: latest from the master 220a68f31437616b53c032a5f82384450f82cf4e go version: go version go1.8.3 darwin/amd64 OS: MacOS Sierra 10.12.5

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@reidrac : After some more digging, I now see what is happening inside the code. The current binary does not resolve any local reference inside the external reference (external ref --> Poll has a local ref --> Choice under Choices array)

The solution to this problem seems non-trivial and might take some thinking and time to fix. The best work around I can suggest currently is to inline the Choice (which you did) and flatten the spec.

The expansion of spec before generation also works perfect. The number of models generated are huge because references are used in multiple operations(That is how swagger tool behaves currently). Ideally , the AddPollParamsBodyChoicesItems and UpdatePollParamsBodyChoicesItems are the same structures but as far as I know the swagger binary does this so as to accomodate changes caused by complex definitions involving allof , anyof etc…

It would be helpful if you can think/share a solution or design to handle these recursive references distributed within and in external files.

@reidrac : I will look into it. Will try to fix or share findings by eow. Thanks for sharing.