OpenAPITools / OpenAPITools/openapi-generator
[BUG] Patch operations reset fields to default if not specified
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
When using the Python generator, PATCH requests can reset fields to their default values even when the client did not explicitly set those fields. This happens specifically if a default value is specified in the PatchedModel spec. Even though we are defining this default, I don't think it matches user expectations for the value to be implicitly set in the patch request. Openapigenerator 5.4.0 did not exhibit this behavior
openapi-generator version
7.22.0
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: Example API
version: 1.0.0
paths:
/users/{id}:
patch:
operationId: users_partial_update
parameters:
- name: id
in: path
required: true
schema:
type: integer
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/PatchedUser'
responses:
'200':
description: Success
components:
schemas:
PatchedUser:
type: object
properties:
name:
type: string
default: ""
status:
type: string
default: "active"
priority:
type: integer
default: 0
Generated SDK model:
class PatchedUser(BaseModel):
"""
PatchedUser
""" # noqa: E501
name: Optional[StrictStr] = ''
status: Optional[StrictStr] = 'active'
priority: Optional[StrictInt] = 0
__properties: ClassVar[List[str]] = ["name", "status", "priority"]
model_config = ConfigDict(
populate_by_name=True,
validate_assignment=True,
protected_namespaces=(),
)
def to_dict(self) -> Dict[str, Any]:
"""Return the dictionary representation of the model using alias."""
excluded_fields: Set[str] = set([
])
_dict = self.model_dump(
by_alias=True,
exclude=excluded_fields,
exclude_none=True, # <-- The problem: doesn't exclude unset fields with defaults
)
return _dict
Steps to reproduce
openapi-generator generate -i openapi-bug-report.yaml -g python -o ./bug-report-sdk --package-name bug_report_sdk
Related issues/PRs
- #19067
Suggest a fix/enhancement
Maybe it would make sense to use exclude_unset=True instead of exclude_none=True when serializing Pydantic models for requests?
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 with the Python generator entry point and the generated model's to_dict method shown in the report, then reproduce the issue with the supplied OpenAPI declaration and generation command. Confirm that a PATCH model with defaulted but unset fields omits those fields from the request payload while explicitly provided values remain serialized.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100