Swagger-codegen: Editor not catching $ref error

Created on 25 Jul 2019  路  1Comment  路  Source: swagger-api/swagger-codegen

System information

  • OS: macOS 10.14
  • Browser: Chrome
  • Version: 75.0
  • Swagger/OpenAPI version: OpenAPI 3.0

Example yaml to reproduce the error

module:
    post:
      tags:
      - module
      summary: Create a module
      operationId: createModule
      security:
        - TokenAuth: []
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/BaseModule'
                - $ref: '#/components/schemas/FullModule'
      responses:
        200:
          description: "Success: module created"
...
components:
  schemas:
    BaseModule:
      description: Basic information about a model
      type: object
      properties:
        moduleId:
          type: integer
          format: int64
        moduleName:
          type: string
        details:
          type: string
        numberOfWords:
          type: integer
          format: int32
          example: 10
        parent:
          type: integer
          format: int64
        child:
          type: integer
          format: int64
      required:
        - moduleName

    FullModule:
      allOf:
        - $ref: '#/components/schemas/InfoModule'
        - type: object
          properties:
            words:
              type: array
              items:
                $ref: '#/components/schemas/Dictionary'
            quiz:
              type: array
              items:
                $ref: '#/components/schemas/Quiz'
      required:
        - words

Expected behavior

  • The code above is marked as valid by the online editor as it appears to cause errors with Codegen
  • utilizing the Codegen functionality to build a python-flask server stub I get the following error
There was an error generating the servers python-flask code. Please ensure that the definition and all external refs (inc. domains) are valid.
  • I tried to generate the code locally with swagger-codegen generate -i Documents/asltutor.ymal -l python-flask -o Desktop and got the following error:
java.lang.NullPointerException
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromProperty(DefaultCodegenConfig.java:1685)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromRequestBody(DefaultCodegenConfig.java:2589)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2102)
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:901)
    at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:828)
    at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:462)
    at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:776)
    at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
    at java.lang.Thread.run(Thread.java:748)
Exception in thread "Thread-1" java.lang.RuntimeException: Could not process operation:
  Tag: class Tag {
    name: module
    description: null
    externalDocs: null
}
  Operation: create_module
  Resource: post /modules
  Exception: null
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:926)
    at io.swagger.codegen.v3.DefaultGenerator.processPaths(DefaultGenerator.java:828)
    at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:462)
    at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:776)
    at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromProperty(DefaultCodegenConfig.java:1685)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromRequestBody(DefaultCodegenConfig.java:2589)
    at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromOperation(DefaultCodegenConfig.java:2102)
    at io.swagger.codegen.v3.DefaultGenerator.processOperation(DefaultGenerator.java:901)
    ... 5 more
  • The error appears to be coming from the following section:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/BaseModule'
                - $ref: '#/components/schemas/FullModule'
  • change to the following and code can be generated without error:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/FullModule'

Additional thoughts

  • I suspect the code generator is working as expected but the online editor is not catching the implementation error and states that the code is valid
  • strangely the code-gen error does not occur when building a spring, inflector or aspnetcore server stub. No idea why.

Most helpful comment

@shawntavious - this _is_ an issue with the codegen, actually. It should work, but it doesn't. As you've mentioned, it works with the other generators. Moving this to the codegen for them to look at.

>All comments

@shawntavious - this _is_ an issue with the codegen, actually. It should work, but it doesn't. As you've mentioned, it works with the other generators. Moving this to the codegen for them to look at.

Was this page helpful?
0 / 5 - 0 ratings