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

[python-flask] codegen server generator stopped working

Open michaverhagen opened this issue 6 years ago • 4 comments

Description

I generated a python-flask server stub using the online swagger editor. This has worked perfectly fine for me for several times (> 10). Recently I needed to make a small change to my API, so I loaded the swagger file into the online swagger editor to re-generate a python-flash server stub, but now it consistently fails.

This is the error message I get when I used codegen:

Exception in thread "Thread-0" java.lang.RuntimeException: Could not process model 'calc_input'.Please make sure that your schema is correct!
	at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:404)
	at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:783)
	at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:360)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
	at io.swagger.codegen.v3.generators.util.OpenAPIUtil.getSchemaFromName(OpenAPIUtil.java:40)
	at io.swagger.codegen.v3.generators.util.OpenAPIUtil.getSchemaFromRefSchema(OpenAPIUtil.java:55)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.isObjectSchema(DefaultCodegenConfig.java:4191)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromProperty(DefaultCodegenConfig.java:1723)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromProperty(DefaultCodegenConfig.java:1693)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.addVars(DefaultCodegenConfig.java:3084)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.addVars(DefaultCodegenConfig.java:3054)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.addVars(DefaultCodegenConfig.java:3042)
	at io.swagger.codegen.v3.generators.DefaultCodegenConfig.fromModel(DefaultCodegenConfig.java:1426)
	at io.swagger.codegen.v3.DefaultGenerator.processModels(DefaultGenerator.java:1015)
	at io.swagger.codegen.v3.DefaultGenerator.generateModels(DefaultGenerator.java:375)
	... 3 more

And this is the message I see in the console window when I use the online swagger editor:

generate:1 POST https://generator3.swagger.io/api/generate 500 (Internal Server Error)
index.js:581 Uncaught (in promise) Error: Internal Server Error
    at index.js:581
    at s (runtime.js:45)
    at Generator._invoke (runtime.js:271)
    at Generator.forEach.t.<computed> [as next] (runtime.js:97)
    at o (asyncToGenerator.js:5)
    at u (asyncToGenerator.js:27)

I ran the swagger file (see below) through two different API validators and both indicate that the file is ok.

Swagger-codegen version

I think I use version 3.0.0 - I just checked out the master repo and followed the steps in the manual.

Swagger declaration file content or url
openapi: 3.0.0
info:
  title: Quick Quote
  description: Quick Quote API
  version: 1.0.0
servers:
- url: http://localhost/v1
  description: testing server
security:
- basicAuth: []
paths:
  /calculate:
    post:
      summary: Returns a json with the result of the calculation
      operationId: calculate_post
      requestBody:
        description: input parameters for the calculation
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/calc_input'
        required: true
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/calc_output'
        "401":
          description: Authentication information is missing or invalid
          headers:
            WWW_Authenticate:
              style: simple
              explode: false
              schema:
                type: string
      security:
      - basicAuth: []
      x-openapi-router-controller: swagger_server.controllers.default_controller
components:
  schemas:
    calc_input:
      type: object
      properties:
        inside_sales_quote_number:
          type: integer
        inside_sales:
          type: string
        engineer:
          type: string
        estimated_annual_usage:
          type: integer
        customer_name:
          type: string
        customer_part_number:
          type: string
        customer_drawing_number:
          type: string
        company_part_number:
          type: string
        company_drawing_number:
          type: string
        company_description:
          type: string
        eo_number:
          type: integer
        date:
          type: string
        business_unit:
          type: string
        product:
          type: string
        fabric:
          type: string
        unit:
          type: string
        diameter:
          type: number
        spring:
          type: integer
        coordinatepoints:
          type: array
          items:
            $ref: '#/components/schemas/calc_input_coordinatepoints'
    calc_output:
      properties:
        calculationresult:
          $ref: '#/components/schemas/calc_output_calculationresult'
      example:
        calculationresult:
          material: material
          price: 0.8
    calc_input_coordinatepoints:
      type: object
      properties:
        X:
          type: number
        Y:
          type: number
        Z:
          type: number
        R:
          type: number
    calc_output_calculationresult:
      type: object
      properties:
        cost:
          type: number
        materialcost:
          type: number
        labourcost:
          type: number
        hardwarecost:
          type: number
        material:
          type: string
        errors:
          type: array
          items:
            type: string
      example:
        material: material
        cost: 4
        materialcost: 1
        labourcost: 1
        hardwarecost: 1
  responses:
    UnauthorizedError:
      description: Authentication information is missing or invalid
      headers:
        WWW_Authenticate:
          style: simple
          explode: false
          schema:
            type: string
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
      x-basicInfoFunc: swagger_server.controllers.authorization_controller.check_basicAuth
Command line used for generation

I have a small script that should generate the server:

#!/usr/bin/env bash
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
  -i swagger.yaml \
  -l python-flask \
  -o flaskConnexion
Steps to reproduce

As long as the swagger.yaml file is in the local directory and the flaskConnexion directory exists, it should just be to execute the above script.

Related issues/PRs

Yes, I believe this issue is related:

https://github.com/swagger-api/swagger-codegen/issues/9990

It only refers to the pet store - I can also replicate this error. But I now have the same problem with my own file, which is a file that is generated by the codegen itself.

Suggest a fix/enhancement

Sorry, I am out of my depth here. Any suggestions would be greatly appreciated.

michaverhagen avatar Jan 21 '20 14:01 michaverhagen

I just checked the release, I think I use 3.0.16

michaverhagen avatar Jan 21 '20 17:01 michaverhagen

3.0.11 is the latest version that works for me.

brew uninstall swagger-codegen
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/97a2d874969562f687e3e3b004abe64c197278aa/Formula/swagger-codegen.rb

zhanwenchen avatar Jan 22 '20 21:01 zhanwenchen

this was fixed on this commit going to close it but please, if NPE is still present let me know, i'll check asap.

HugoMario avatar Jan 30 '20 07:01 HugoMario

Try code gen 3.0.11. It worked for me.

SmadusankaB avatar Mar 16 '20 05:03 SmadusankaB