Skip to content

Commit 6afb316

Browse files
committed
add config option for Python backwards compatibility
1 parent 3ad868c commit 6afb316

5 files changed

Lines changed: 20 additions & 6 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
403403
public static final String LEGACY_DISCRIMINATOR_BEHAVIOR = "legacyDiscriminatorBehavior";
404404
public static final String LEGACY_DISCRIMINATOR_BEHAVIOR_DESC = "Set to false for generators with better support for discriminators. (Python, Java, Go, PowerShell, C# have this enabled by default).";
405405

406+
public static final String LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR = "legacyDefaultDisallowAdditionalPropertiesIfNotPresentBehavior";
407+
public static final String LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR_DESC = "Set to true for Python generators so that generated code matches what was previously produced when `disallowAdditionalProperties` was unspecified";
408+
409+
406410
public static final String USE_SINGLE_REQUEST_PARAMETER = "useSingleRequestParameter";
407411
public static final String USE_SINGLE_REQUEST_PARAMETER_DESC = "Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.";
408412

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
5151
protected String packageName = "openapi_client";
5252
@Setter protected String packageVersion = "1.0.0";
5353
@Setter protected String projectName; // for setup.py, e.g. petstore-api
54+
@Setter
55+
protected boolean legacyDisallowAdditionalPropertiesDefaultBehavior = false;
5456
protected boolean hasModelsToImport = Boolean.FALSE;
5557
protected String mapNumberTo = "Union[StrictFloat, StrictInt]";
5658
protected Map<Character, String> regexModifiers;
@@ -145,6 +147,10 @@ public AbstractPythonCodegen() {
145147
public void processOpts() {
146148
super.processOpts();
147149

150+
if (additionalProperties.containsKey(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR)) {
151+
setLegacyDisallowAdditionalPropertiesDefaultBehavior(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR).toString()));
152+
}
153+
148154
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
149155
LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)");
150156
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
@@ -975,6 +981,10 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
975981
// set the extensions if the key is absent
976982
model.getVendorExtensions().putIfAbsent("x-py-readonly", readOnlyFields);
977983

984+
if (legacyDisallowAdditionalPropertiesDefaultBehavior) {
985+
model.vendorExtensions.putIfAbsent("x-py-legacy-disallow-additional-properties-default-behavior", true);
986+
}
987+
978988
// remove the items of postponedModelImports in modelImports to avoid circular imports error
979989
if (!modelImports.isEmpty() && !postponedModelImports.isEmpty()) {
980990
modelImports.removeAll(postponedModelImports);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,15 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
260260
if not isinstance(obj, dict):
261261
return cls.model_validate(obj)
262262

263-
{{^vendorExtensions.x-py-legacy-additional-properties-validation}}
263+
{{^vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
264264
{{^isAdditionalPropertiesTrue}}
265265
# raise errors for additional fields in the input
266266
for _key in obj.keys():
267267
if _key not in cls.__properties:
268268
raise ValueError("Error due to additional fields (not defined in {{classname}}) in the input: " + _key)
269269

270270
{{/isAdditionalPropertiesTrue}}
271-
{{/vendorExtensions.x-py-legacy-additional-properties-validation}}
271+
{{/vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
272272
_obj = cls.model_validate({
273273
{{#allVars}}
274274
{{#isContainer}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
252252
if not isinstance(obj, dict):
253253
return {{{classname}}}.parse_obj(obj)
254254

255-
{{^vendorExtensions.x-py-legacy-additional-properties-validation}}
255+
{{^vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
256256
{{^isAdditionalPropertiesTrue}}
257257
# raise errors for additional fields in the input
258258
for _key in obj.keys():
259259
if _key not in cls.__properties:
260260
raise ValueError("Error due to additional fields (not defined in {{classname}}) in the input: " + obj)
261261

262262
{{/isAdditionalPropertiesTrue}}
263-
{{/vendorExtensions.x-py-legacy-additional-properties-validation}}
263+
{{/vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
264264
_obj = {{{classname}}}.parse_obj({
265265
{{#allVars}}
266266
{{#isContainer}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,15 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
279279
if not isinstance(obj, dict):
280280
return cls.model_validate(obj)
281281

282-
{{^vendorExtensions.x-py-legacy-additional-properties-validation}}
282+
{{^vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
283283
{{^isAdditionalPropertiesTrue}}
284284
# raise errors for additional fields in the input
285285
for _key in obj.keys():
286286
if _key not in cls.__properties:
287287
raise ValueError("Error due to additional fields (not defined in {{classname}}) in the input: " + _key)
288288

289289
{{/isAdditionalPropertiesTrue}}
290-
{{/vendorExtensions.x-py-legacy-additional-properties-validation}}
290+
{{/vendorExtensions.x-py-legacy-disallow-additional-properties-default-behavior}}
291291
_obj = cls.model_validate({
292292
{{#allVars}}
293293
{{#isContainer}}

0 commit comments

Comments
 (0)