OpenAPITools / OpenAPITools/openapi-generator
[BUG] [Python] Object with enum in additionalProperties and RESOLVE_INLINE_ENUMS can not be optional.
Nobody has claimed this yet.
- 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
When having schema where one of properties is optional enum, then generated model method from_dict() is throwing error:
- AttributeError: 'NoneType' object has no attribute 'items'
Adding 'nullable: true' to property1 is not changing anything.
openapi-generator version
7.5.0
OpenAPI declaration file content or url
IssueSchema:
type: object
properties:
property1:
type: object
additionalProperties:
type: string
enum:
- TEXT1
- TEXT2
- TEXT3
property2:
type: object
additionalProperties:
type: boolean
Generation Details
openapi-generator-cli generate -g python -c config.yaml
Config file:
globalProperties:
apiTests: false
modelTests: false
additionalProperties:
enablePostProcessFile: true
inlineSchemaOptions:
RESOLVE_INLINE_ENUMS: true
Steps to reproduce
Try tu run method from_dict() of model genrated from provided schema when property1 is not present. For example:
{
"property2": {
"key1": "true",
"key2": "false"
}
}
Related issues/PRs
Suggest a fix
Genrated method from_dict() of model is generated as follow:
@classmethod
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"""Create an instance of IssueSchema from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return cls.model_validate(obj)
_obj = cls.model_validate(
{
"property1": dict(
(_k, _v) for _k, _v in obj.get("property1").items() # This line is throwing an error.
),
"property2": obj.get("booleanPermissions"),
}
)
return _obj
When obj.get("property1") is None we can not call items() on it. Simple 'if' statement is fixing issue:
@classmethod
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
"""Create an instance of IssueSchema from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return cls.model_validate(obj)
_obj = cls.model_validate(
{
"property1": dict(
(_k, _v) for _k, _v in obj.get("property1").items() # This line is throwing an error.
) if obj.get("property1") is not None else None, # This 'if' fix all problem.
"property2": obj.get("booleanPermissions"),
}
)
return _obj
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the supplied OpenAPI schema, config.yaml, and openapi-generator-cli generate -g python -c config.yaml command. Start from the generated model's from_dict() method and verify that an absent optional property1 no longer causes an AttributeError while property2 still deserializes correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100