Skip to content

Commit 388ef8b

Browse files
committed
add Python generator config setting for backwards compatibility
1 parent 5e626e7 commit 388ef8b

13 files changed

Lines changed: 38 additions & 6 deletions

bin/configs/python-aiohttp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ additionalProperties:
88
mapNumberTo: float
99
poetry1: true
1010
disallowAdditionalPropertiesIfNotPresent: true
11+
legacyDisallowAdditionalPropertiesDefaultBehavior: true
1112
nameMappings:
1213
_type: underscore_type
1314
type_: type_with_underscore

bin/configs/python-echo-api.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ templateDir: modules/openapi-generator/src/main/resources/python
55
additionalProperties:
66
hideGenerationTimestamp: "true"
77
disallowAdditionalPropertiesIfNotPresent: true
8+
legacyDisallowAdditionalPropertiesDefaultBehavior: true

bin/configs/python-fastapi.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ sourceFolder: "src"
66
additionalProperties:
77
hideGenerationTimestamp: "true"
88
disallowAdditionalPropertiesIfNotPresent: true
9+
legacyDisallowAdditionalPropertiesDefaultBehavior: true

bin/configs/python-httpx.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ additionalProperties:
77
packageName: petstore_api
88
mapNumberTo: float
99
poetry1: true
10+
disallowAdditionalPropertiesIfNotPresent: true
11+
legacyDisallowAdditionalPropertiesDefaultBehavior: true
1012
nameMappings:
1113
_type: underscore_type
1214
type_: type_with_underscore

bin/configs/python-pydantic-v1-aiohttp.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ additionalProperties:
77
packageName: petstore_api
88
mapNumberTo: float
99
disallowAdditionalPropertiesIfNotPresent: true
10+
legacyDisallowAdditionalPropertiesDefaultBehavior: true

bin/configs/python-pydantic-v1-echo-api.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ templateDir: modules/openapi-generator/src/main/resources/python-pydantic-v1
55
additionalProperties:
66
hideGenerationTimestamp: "true"
77
disallowAdditionalPropertiesIfNotPresent: true
8+
legacyDisallowAdditionalPropertiesDefaultBehavior: true

bin/configs/spring-boot-oneof-interface.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ additionalProperties:
1010
hideGenerationTimestamp: "true"
1111
useOneOfInterfaces: "true"
1212
useDeductionForOneOfInterfaces: "true"
13+
disallowAdditionalPropertiesIfNotPresent: "true"

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 = "legacyDisallowAdditionalPropertiesDefaultBehavior";
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/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp
4949
protected String packageName = "openapi_client";
5050
@Setter protected String packageVersion = "1.0.0";
5151
@Setter protected String projectName; // for setup.py, e.g. petstore-api
52+
@Setter
53+
protected boolean legacyDisallowAdditionalPropertiesDefaultBehavior = false;
5254
protected boolean hasModelsToImport = Boolean.FALSE;
5355
protected String mapNumberTo = "Union[StrictFloat, StrictInt]";
5456
protected Map<Character, String> regexModifiers;
@@ -138,6 +140,10 @@ public AbstractPythonPydanticV1Codegen() {
138140
public void processOpts() {
139141
super.processOpts();
140142

143+
if (additionalProperties.containsKey(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR)) {
144+
setLegacyDisallowAdditionalPropertiesDefaultBehavior(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR).toString()));
145+
}
146+
141147
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
142148
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)");
143149
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
@@ -983,6 +989,10 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
983989
model.getVendorExtensions().putIfAbsent("x-py-datetime-imports", datetimeImports);
984990
model.getVendorExtensions().putIfAbsent("x-py-readonly", readOnlyFields);
985991

992+
if (legacyDisallowAdditionalPropertiesDefaultBehavior) {
993+
model.vendorExtensions.putIfAbsent("x-py-legacy-disallow-additional-properties-default-behavior", true);
994+
}
995+
986996
// remove the items of postponedModelImports in modelImports to avoid circular imports error
987997
if (!modelImports.isEmpty() && !postponedModelImports.isEmpty()) {
988998
modelImports.removeAll(postponedModelImports);

0 commit comments

Comments
 (0)