OpenAPITools / OpenAPITools/openapi-generator
[BUG] [PYTHON] Model property: required AND default NOT combinable
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
I want to have a required property with a default value. When I require it in the model spec, it loses the default= parameter in the Field() constructor. I need it to be required since, if it isn't, it could be None.
Firstly, is this intended? If so how am I supposed to set up the property? If not, please help :)
openapi-generator version
master/7.5.0
OpenAPI declaration file content or url
openapi: 3.0.0
info:
title: Title
description: Title
version: 1.0.0
paths:
'/user':
get:
responses:
200:
description: "success"
content:
application/json:
schema:
$ref: '#/components/schemas/RequiredProperties'
components:
schemas:
RequiredProperties:
type: object
required:
- requiredWithDefault
- requiredWithoutDefault
properties:
requiredWithoutDefault:
type: string
requiredWithDefault:
type: string
default: "4.5.0"
notRequiredWithDefault:
type: string
default: "4.5.0"
notRequiredWithoutDefault:
type: string
Generation Details
java -jar openapi-generator-cli.jar generate -i required.yaml -g python -o object --additional-properties packageName=object_client
Steps to reproduce
- Generate from provided spec
- See Model class
class RequiredProperties(BaseModel):
"""
RequiredProperties
""" # noqa: E501
required_with_default: StrictStr = Field(alias="requiredWithDefault")
required_without_default: StrictStr = Field(alias="requiredWithoutDefault")
not_required_with_default: Optional[StrictStr] = Field(default='4.5.0', alias="notRequiredWithDefault")
not_required_without_default: Optional[StrictStr] = Field(default=None, alias="notRequiredWithoutDefault")
__properties: ClassVar[List[str]] = ["requiredWithDefault", "requiredWithoutDefault", "notRequiredWithDefault", "notRequiredWithoutDefault"]
Interestingly enough, the default shows up in the from_dict() classmethod:
_obj = cls.model_validate({
"requiredWithDefault": obj.get("requiredWithDefault") if obj.get("requiredWithDefault") is not None else '4.5.0',
"requiredWithoutDefault": obj.get("requiredWithoutDefault"),
"notRequiredWithDefault": obj.get("notRequiredWithDefault") if obj.get("notRequiredWithDefault") is not None else '4.5.0',
"notRequiredWithoutDefault": obj.get("notRequiredWithoutDefault")
})
Suggest a fix
I haven't quite found the problem yet, I'm pretty sure this is the part that's causing it: https://github.com/OpenAPITools/openapi-generator/blob/a4cf255dce69b74fabe49161d75bdc39851d1d95/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java#L914
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
Start at AbstractPythonPydanticV1Codegen.java around line 914, which the report identifies as related to the missing default. Generate the supplied OpenAPI YAML with the Python generator and inspect the RequiredProperties model and from_dict() output. Done means a required property with a default retains the default in its Field() declaration without changing the other generated properties.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100