OpenAPITools / OpenAPITools/openapi-generator

[BUG][Python] Schema property named _ (a single underscore) generates broken syntax

Open
#18,398 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Syntax error in generated Python code as the processing eats the underscore and leaves us with nothing:

class TestGet200Response(BaseModel):
    """
    TestGet200Response
    """ # noqa: E501
    : Optional[StrictStr] = Field(default=None, alias="_")
    __properties: ClassVar[List[str]] = ["_"]

    model_config = ConfigDict(
openapi-generator version

git 9351217048ff3564dff

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 1.0.0
  title: test
paths:
  /test:
    get:
      summary: sample
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  _:
                    type: string
Generation Details
java -jar openapi-generator-cli-7.5.0-SNAPSHOT.jar generate -i undersc.yml -o undersc/ -g python
Steps to reproduce

Use the python generator on a spec with _ as the field name in a response.

Related issues/PRs

couldn't find any 🤷‍♀️

Suggest a fix

Don't eat the underscore? Or rename the field, seems like angular has turned it into _u 🤔

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

Reproduce the issue with the provided undersc.yml and the Python generator command, then inspect the generator path that produces the shown response model. Done means the generated Python is syntactically valid while retaining the schema property alias "_"; verify the result by compiling or otherwise parsing the generated model.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.