Skip to content

Commit 1d633da

Browse files
fix(python): Use exclude_unset=True for PATCH models to prevent sending default values
Fixes issue where PATCH requests were sending default values even when fields were not explicitly set by the client, causing unintended field resets. Changes: - Add postProcessModels() override in PythonClientCodegen to detect PATCH models (models starting with "Patched") and mark them with isPatchedModel vendor extension - Update model_generic.mustache to use exclude_unset=True for PATCH models, exclude_none=True for other models - Only affects PythonClientCodegen, not other Python generators - Maintains backward compatibility for non-PATCH models Before: PatchedUser(name="John") would send {"name": "John", "status": "active", "priority": 0} After: PatchedUser(name="John") sends {"name": "John"} (only explicitly set fields) Fixes #1234
1 parent d27cc40 commit 1d633da

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
442442
for (ModelMap mo : objs.getModels()) {
443443
CodegenModel cm = mo.getModel();
444444
if (cm.getClassname().startsWith("Patched")) {
445+
// Mark as PATCH model for template handling
445446
cm.vendorExtensions.put("isPatchedModel", true);
446447
}
447448
}

modules/openapi-generator/src/main/resources/python/model_generic.mustache

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,20 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
164164
{{/isAdditionalPropertiesTrue}}
165165
])
166166

167-
{{#classname}}
168-
{{#isPatchedModel}}
167+
{{#vendorExtensions.isPatchedModel}}
169168
_dict = self.model_dump(
170169
by_alias=True,
171170
exclude=excluded_fields,
172171
exclude_unset=True, # For PATCH models, exclude unset fields to avoid sending default values
173172
)
174-
{{/isPatchedModel}}
175-
{{^isPatchedModel}}
173+
{{/vendorExtensions.isPatchedModel}}
174+
{{^vendorExtensions.isPatchedModel}}
176175
_dict = self.model_dump(
177176
by_alias=True,
178177
exclude=excluded_fields,
179178
exclude_none=True,
180179
)
181-
{{/isPatchedModel}}
182-
{{/classname}}
180+
{{/vendorExtensions.isPatchedModel}}
183181
{{#allVars}}
184182
{{#isContainer}}
185183
{{#isArray}}

0 commit comments

Comments
 (0)