Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Invalid default enum value with Python generator #20397

Open
5 of 6 tasks
jlumpe opened this issue Jan 3, 2025 · 2 comments
Open
5 of 6 tasks

[BUG] Invalid default enum value with Python generator #20397

jlumpe opened this issue Jan 3, 2025 · 2 comments

Comments

@jlumpe
Copy link

jlumpe commented Jan 3, 2025

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

An enum v2SortDirection is defined with default value "SORT_DIRECTION_UNSPECIFIED". Another schema v2SortField has a direction field with this type. In the generated Python model, where the default value for the field is defined, the SORT_DIRECTION_ prefix is stripped off:

# openapi_client/models/v2_sort_direction.py

class V2SortDirection(str, Enum):
    SORT_DIRECTION_UNSPECIFIED = 'SORT_DIRECTION_UNSPECIFIED'
    SORT_DIRECTION_ASCENDING = 'SORT_DIRECTION_ASCENDING'
    SORT_DIRECTION_DESCENDING = 'SORT_DIRECTION_DESCENDING'
# openapi_client/models/v2_sort_field.py

from openapi_client.models.v2_sort_direction import V2SortDirection

class V2SortField(BaseModel):
    var_field: Optional[StrictStr] = Field(default=None, alias="field")
    # Error here:
    direction: Optional[V2SortDirection] = V2SortDirection.UNSPECIFIED
    __properties: ClassVar[List[str]] = ["field", "direction"]

The default value should be V2SortDirection.SORT_DIRECTION_UNSPECIFIED, not V2SortDirection.UNSPECIFIED. This results in an AttributeError: UNSPECIFIED when the package is imported.

This doesn't appear to be because the prefix is (almost) the same as the Enum name, changing it to "SOME_OTHER_PREFIX_UNSPECIFIED" gives the same result.

openapi-generator version

7.10.0 (Docker)

OpenAPI declaration file content or url

Minimal subset of the NCBI datasets API:

openapi: 3.0.1
info:
  title: NCBI Datasets API
  version: v2

paths: {}

components:

  schemas:
    v2SortField:
      type: object
      properties:
        field:
          type: string
        direction:
          $ref: '#/components/schemas/v2SortDirection'

    v2SortDirection:
      type: string
      enum:
        - SORT_DIRECTION_UNSPECIFIED
        - SORT_DIRECTION_ASCENDING
        - SORT_DIRECTION_DESCENDING
      default: SORT_DIRECTION_UNSPECIFIED
Generation Details

Using the openapitools/openapi-generator-cli docker image (7.10.0) with -g python, all default options.

Steps to reproduce

(previous section)

Related issues/PRs

Possibly #18843:

Then using a $ref to refer to an enum (with a default value) the default value is not applied for the Python generators. The default value is applied if it is an inline enum.

They don't provide example output, but it seems like the problem was that the default value was not generated at all, not that it was wrong. That was resolved by #18796, it's possible the error is there.

Suggest a fix
@wing328
Copy link
Member

wing328 commented Jan 3, 2025

as a workaround, can you please try disabling the enum prefix option mentioned in #19984 (comment) ?

@jlumpe
Copy link
Author

jlumpe commented Jan 3, 2025

Thanks, that seems to have fixed the problem.

I'm now getting a separate enum-related problem, but it seems like it might be due to a problem with the spec I'm using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants