MarcoMuellner / openapi-python-generator

A client generator from openapi for python.
MIT License
56 stars 25 forks source link

openapi-python-generator creates a empty pydantic model from an object that is a string #25

Open dcermak opened 1 year ago

dcermak commented 1 year ago

Describe the bug

When working with the gitea API, I've ran into the issue that the CommitStatusState object is converted into the following class:

class CommitStatusState(BaseModel):
    """
        None model
            CommitStatusState holds the state of a CommitStatus
    It can be "pending", "success", "error", "failure", and "warning"

    """

from the following openapi.json:

      "CommitStatusState" : {
        "description" : "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"",
        "type" : "string",
        "x-go-package" : "code.gitea.io/gitea/modules/structs"
      },

The problem is, that it actually should be a string. In practice this causes for instance the /repos/{owner}/{repo}/statuses/{sha} to not be usable, as openapi-python-generator creates the following api method:

async def repoCreateStatus(
    owner: str,
    repo: str,
    sha: str,
    data: CreateStatusOption,
    api_config_override: Optional[APIConfig] = None,
) -> CommitStatus:
    ...

where CreateStatusOption is the following class:

class CreateStatusOption(BaseModel):
    """
    None model
        CreateStatusOption holds the information needed to create a new CommitStatus for a Commit

    """

    context: Optional[str] = Field(alias="context", default=None)

    description: Optional[str] = Field(alias="description", default=None)

    state: Optional[CommitStatusState] = Field(alias="state", default=None)

    target_url: Optional[str] = Field(alias="target_url", default=None)

Now when you try to call it as follows:

    await repoCreateStatus(
        owner=repo_owner,
        repo=repo_name,
        sha=commit_sha,
        data=CreateStatusOption(
            context=OBS_CI_CTX,
            target_url=f"https://build.opensuse.org/package/show/{project_name}/{pkg_name}",
            state=ci_state,
        ),
        api_config_override=api_config,
    )

where ci_state is some string, you'll receive the following validation error:

pydantic.error_wrappers.ValidationError: 1 validation error for CreateStatusOption
state
  value is not a valid dict (type=type_error.dict)

To Reproduce Steps to reproduce the behavior:

  1. podman run --pull=always --rm -v (pwd):/local:Z openapitools/openapi-generator-cli generate -i https://gitea.opensuse.org/swagger.v1.json -g openapi -o /local/
  2. poetry run openapi-python-generator --library aiohttp openapi.json gitea/
  3. inspect the generated files gitea/services/async_repository_service.py, gitea/models/CreateStatusOption.py and gitea/models/CommitStatusState.py

Expected behavior

The state attribute in CreateStatusOption should have the type Optional[str] and not CommitStatusState or CommitStatusState should be an alias for str.