Bug Report Checklist
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
Bug Report Checklist
Description
An enum
v2SortDirectionis defined with default value"SORT_DIRECTION_UNSPECIFIED". Another schemav2SortFieldhas adirectionfield with this type. In the generated Python model, where the default value for the field is defined, theSORT_DIRECTION_prefix is stripped off:The default value should be
V2SortDirection.SORT_DIRECTION_UNSPECIFIED, notV2SortDirection.UNSPECIFIED. This results in anAttributeError: UNSPECIFIEDwhen 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:
Generation Details
Using the
openapitools/openapi-generator-clidocker image (7.10.0) with-g python, all default options.Steps to reproduce
(previous section)
Related issues/PRs
Possibly #18843:
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