Skip to content

Commit 0a71bbb

Browse files
fix(python): Use exclude_unset=True for PATCH models
Prevents sending default values in PATCH requests when fields are not explicitly set by the client. Only affects models starting with 'Patched'. Fixes #23676
1 parent d27cc40 commit 0a71bbb

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)