swagger-py-codegen icon indicating copy to clipboard operation
swagger-py-codegen copied to clipboard

Circular references should be allowed as per Swagger 2.0 API Spec.

Open SimplicityGuy opened this issue 8 years ago • 8 comments

Other codegen tools support circular references. See https://github.com/swagger-api/swagger-codegen/issues/728.

I this this issue with the following yml:

  Node:
    description: Generic node.
    type: object
    properties:
      type:
        description: Type or label of the node.
        type: string
      properties:
        description: Node specific dictionary of key/value pairs.
        type: object
        additionalProperties:
          type: string
      relationships:
        descriptions: Relationships this node has with other nodes.
        type: array
        items:
          $ref: '#/definitions/Relationship'
  Relationship:
    description: Relationship that one node has with another.
    type: object
    properties:
      type:
        description: Type of relationship between nodes.
        type: string
      properties:
        description: Relationship specific dictionary of key/value pairs.
        type: object
        additionalProperties:
          type: string
      node:
        $ref: '#/definitions/Node'

Exception:

Traceback (most recent call last):
  File "/usr/local/bin/swagger_py_codegen", line 11, in <module>
    sys.exit(generate())
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 716, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 696, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python2.7/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/swagger_py_codegen/command.py", line 77, in generate
    swagger = Swagger(data)
  File "/usr/local/lib/python2.7/site-packages/swagger_py_codegen/parser.py", line 29, in __init__
    self._references_sort()
  File "/usr/local/lib/python2.7/site-packages/swagger_py_codegen/parser.py", line 67, in _references_sort
    raise ValueError(msg)
ValueError: $ref circular references found!

SimplicityGuy avatar Apr 27 '16 23:04 SimplicityGuy

There is a test case test_swagger_ref_count_04 that I believe is erroneous. There is nothing in the Swagger spec that disallows nested definitions or definitions that refer back onto themselves.

SimplicityGuy avatar Apr 27 '16 23:04 SimplicityGuy

Another reference: https://github.com/swagger-api/swagger-codegen/issues/728

SimplicityGuy avatar Apr 27 '16 23:04 SimplicityGuy

I don't think developers are paying any more attention. You can get around this by first using json-refs (install from npm) to first resolve all refs, and then generate from the resulting json

ubyjvovk avatar Apr 28 '16 05:04 ubyjvovk

I believe I have a fix for both this issue and #57. I need to do some more testing on it as it changes a fundamental portion of the parser.

SimplicityGuy avatar Apr 28 '16 12:04 SimplicityGuy

Well, scratch that. That local fix passed unit tests, but failed when running the tool. I'm going to try to address #57 and #62 with looking at #63.

SimplicityGuy avatar Apr 28 '16 15:04 SimplicityGuy

On my own installation I edited parser.py at least to give some debugging information on the source of the circular reference to fix it. Giving a more meaningful error message helped me overcome this problem. Up for a pull request?

ciacicode avatar Aug 12 '17 17:08 ciacicode

I just wanted to add that the solution presented by Nicklas Møller Jepsen, solved the issues for me. I was working with scaffold entities (EF) with circular references and had the problem with freezing Swagger UI. I think Nicklas solution is really neat.

Fredell avatar Feb 14 '18 14:02 Fredell

I got a similar problem

 /employee/findById:
    get:
      tags:
      - "employee"
      summary: "Finds enployee by id"
      operationId: "findById"
      produces:
      - "application/json"
      parameters:
      - name: "id"
        in: "query"
        description: "Employee id"
        required: true
        type: "string"
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/Employee"
        400:
          description: "Invalid status value"
definitions:
  Employee:
    type: "object"
    properties:
      id:
        type: "integer"
        format: "int64"
      petId:
        type: "integer"
        format: "int64"
      quantity:
        type: "integer"
        format: "int32"
      shipDate:
        type: "string"
        format: "date-time"
      status:
        type: "string"
        description: "Order Status"
        enum:
        - "placed"
        - "approved"
        - "delivered"
      complete:
        type: "boolean"
        default: false
    xml:
      name: "Order"

this $ref not avivalid

lone1y-51 avatar Apr 16 '18 02:04 lone1y-51