Skip to content

Commit ab972b2

Browse files
committed
add Python generator config setting for backwards compatibility
1 parent 41af1a2 commit ab972b2

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: false
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
@@ -54,6 +54,8 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
5454
protected String packageName = "openapi_client";
5555
@Setter protected String packageVersion = "1.0.0";
5656
@Setter protected String projectName; // for setup.py, e.g. petstore-api
57+
@Setter
58+
protected boolean legacyDisallowAdditionalPropertiesDefaultBehavior = false;
5759
protected boolean hasModelsToImport = Boolean.FALSE;
5860
protected String mapNumberTo = "Union[StrictFloat, StrictInt]";
5961
protected Map<Character, String> regexModifiers;
@@ -150,6 +152,10 @@ public AbstractPythonCodegen() {
150152
public void processOpts() {
151153
super.processOpts();
152154

155+
if (additionalProperties.containsKey(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR)) {
156+
setLegacyDisallowAdditionalPropertiesDefaultBehavior(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.LEGACY_DEFAULT_DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_BEHAVIOR).toString()));
157+
}
158+
153159
if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) {
154160
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)");
155161
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
@@ -991,6 +997,10 @@ private ModelsMap postProcessModelsMap(ModelsMap objs) {
991997
// set the extensions if the key is absent
992998
model.getVendorExtensions().putIfAbsent("x-py-readonly", readOnlyFields);
993999

1000+
if (legacyDisallowAdditionalPropertiesDefaultBehavior) {
1001+
model.vendorExtensions.putIfAbsent("x-py-legacy-disallow-additional-properties-default-behavior", true);
1002+
}
1003+
9941004
// remove the items of postponedModelImports in modelImports to avoid circular imports error
9951005
if (!modelImports.isEmpty() && !postponedModelImports.isEmpty()) {
9961006
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
@@ -51,6 +51,8 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp
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;
@@ -140,6 +142,10 @@ public AbstractPythonPydanticV1Codegen() {
140142
public void processOpts() {
141143
super.processOpts();
142144

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

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

0 commit comments

Comments
 (0)