OpenAPITools / OpenAPITools/openapi-generator

[BUG][PYTHON] Generated python-flask server stub enum strings don't have quotes

Open
#13,689 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Python
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Generated python server stub doesn't have quotes around the enum values. This generates python code with syntax errors.

OpenAPI 3.0 YAML input:

status:
    type: string
    description: status
    enum:
      - local
      - invited
      - accepted
      - confirmed
      - rejected

Generated python model stub code:

    @status.setter
    def status(self, status):
        """<omitted>
        """
        allowed_values = [None,local, invited, accepted, confirmed, rejected]  # noqa: E501
        if status not in allowed_values:
            raise ValueError(
                "Invalid value for `status` ({0}), must be one of {1}"
                .format(status, allowed_values)
            )

        self._status = status

Notice that the line allowed_values = ... isn't valid python code. The enum values need to be surrounded by quotes because they are string types.

openapi-generator version

6.2.0

OpenAPI declaration file content or url
status:
    type: string
    description: status
    enum:
      - local
      - invited
      - accepted
      - confirmed
      - rejected
Generation Details
 java -jar openapi-generator-cli.jar generate -g python-flask -i '/tmp/api.yaml' -o '/tmp/codegen_test'
Steps to reproduce

Simply run the codegen CLI and check the output file to see the missing quotes in the stub.

Related issues/PRs

This issue is similar to https://github.com/OpenAPITools/openapi-generator/issues/3196, but the opposite direction. Here we have manually defined an OpenAPI 3.0 YAML, and want to generate python server stub.

Suggest a fix

I'd imagine this bug is specific to the python flask server generator. I'm not too familiar with the code base to navigate to the culprit.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the python-flask generator invoked by the provided openapi-generator CLI command and trace how the generated model stub builds allowed_values for string enums. Reproduce the issue with the supplied OpenAPI YAML, then verify that generated Python is syntactically valid and preserves quoted enum values.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.