openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][PYTHON] Generated files with syntax errors - try to reference nonexistent "null" name

Open syntaxaire opened this issue 2 years ago • 4 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
Description

The Python files produced by this OpenAPI spec have syntax errors that make them impossible to import without manually editing the files. They attempt to access a nonexistent name called null.

openapi-generator version

Latest

OpenAPI declaration file content or url

Minimal reproduction: https://gist.github.com/syntaxaire/a35713fb0b93afc7eaae4621e8c5d1a8

Generation Details

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:latest generate --global-property skipFormModel=false -i /local/openapi.json -g python -o /local/openapi-client

Steps to reproduce
  1. Download the minimal reproduction from the gist link.
  2. Run the command in "Generation Details". (${PWD} is a PowerShell construction, substitute your own working directory if necessary.)
  3. Inspect openapi-client/openapi_client/api/sample_api.py
  4. Line 66 has a syntax error:
                'allowed_values': {
                    ('status',): {

                        "NULL": null,
                        "NULL": null,
                        "NULL": null,
                        "NULL": null
                    },
                },

This is not valid Python. The name null does not exist and has not been declared.

syntaxaire avatar Sep 07 '22 20:09 syntaxaire

Your status values are invalid. The are string values but the schema requires that the type is integer.

spacether avatar Sep 16 '22 05:09 spacether

This minimal reproduction is from a much larger upstream spec. I am reporting this to them. However. shouldn't openapi-generator throw an error at the validation stage, instead of producing code with syntax errors?

Also, the minimal reproduction passes validation at the suggested link. Is there a better validator?

syntaxaire avatar Sep 19 '22 22:09 syntaxaire

I am not aware of a better validator. You can look around and try validating the below spec If you want this feature added, how about you write a PR adding it?

openapi: 3.0.3
info:
  title: Sample API
  description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  version: 0.1.9
servers:
  - url: http://api.example.com/v1
    description: Optional server description, e.g. Main (production) server
  - url: http://staging-api.example.com
    description: Optional server description, e.g. Internal staging server for testing
paths:
  /users:
    get:
      summary: Returns a list of enum values
      description: Optional extended description in CommonMark or HTML.
      responses:
        '200':    # status code
          description: A JSON array of SomeSchema
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SomeSchema'
components:
  schemas:
    SomeSchema:
      type: integer
      enum:
      - a
      - b

spacether avatar Sep 19 '22 23:09 spacether

If you want this feature added, how about you write a PR adding it?

The validation should be better added to the swagger-parser (dependency) instead.

wing328 avatar Sep 20 '22 03:09 wing328

I've found a better validator - it's called Spectral. It's able to detect these type issues.

syntaxaire avatar Oct 07 '22 13:10 syntaxaire