From 666bbf8bc46752a0148a18f9842c54c4357825ed Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 10 Jun 2024 14:36:44 +0800 Subject: [PATCH] add option to add ensure_ascii=False in jsom.dumps --- ...pi-disallowAdditionalPropertiesIfNotPresent-true.yaml | 1 + docs/generators/python.md | 1 + .../codegen/languages/PythonClientCodegen.java | 9 ++++++++- .../src/main/resources/python/rest.mustache | 2 +- .../openapi_client/rest.py | 2 +- 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/configs/python-echo-api-disallowAdditionalPropertiesIfNotPresent-true.yaml b/bin/configs/python-echo-api-disallowAdditionalPropertiesIfNotPresent-true.yaml index 8b15af490dad..4d4ab8e891c5 100644 --- a/bin/configs/python-echo-api-disallowAdditionalPropertiesIfNotPresent-true.yaml +++ b/bin/configs/python-echo-api-disallowAdditionalPropertiesIfNotPresent-true.yaml @@ -5,3 +5,4 @@ templateDir: modules/openapi-generator/src/main/resources/python additionalProperties: hideGenerationTimestamp: "true" disallowAdditionalPropertiesIfNotPresent: "true" + setEnsureAsciiToFalse: true diff --git a/docs/generators/python.md b/docs/generators/python.md index 736de837eded..fc2e686a275a 100644 --- a/docs/generators/python.md +++ b/docs/generators/python.md @@ -31,6 +31,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |packageVersion|python package version.| |1.0.0| |projectName|python project name in setup.py (e.g. petstore-api).| |null| |recursionLimit|Set the recursion limit. If not set, use the system default value.| |null| +|setEnsureAsciiToFalse|When set to true, add `ensure_ascii=False` in json.dumps when creating the HTTP request body.| |false| |useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false| ## IMPORT MAPPING diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index 8b10ad97c160..da30b6e1ed78 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -43,6 +43,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege public static final String RECURSION_LIMIT = "recursionLimit"; public static final String DATETIME_FORMAT = "datetimeFormat"; public static final String DATE_FORMAT = "dateFormat"; + public static final String SET_ENSURE_ASCII_TO_FALSE = "setEnsureAsciiToFalse"; @Setter protected String packageUrl; protected String apiDocPath = "docs/"; @@ -50,7 +51,7 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege @Setter protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup @Setter protected String datetimeFormat = "%Y-%m-%dT%H:%M:%S.%f%z"; @Setter protected String dateFormat = "%Y-%m-%d"; - + @Setter protected boolean setEnsureAsciiToFalse = false; private String testFolder; @@ -135,6 +136,8 @@ public PythonClientCodegen() { .defaultValue(Boolean.TRUE.toString())); cliOptions.add(new CliOption(CodegenConstants.SOURCECODEONLY_GENERATION, CodegenConstants.SOURCECODEONLY_GENERATION_DESC) .defaultValue(Boolean.FALSE.toString())); + cliOptions.add(new CliOption(SET_ENSURE_ASCII_TO_FALSE, "When set to true, add `ensure_ascii=False` in json.dumps when creating the HTTP request body.") + .defaultValue(Boolean.FALSE.toString())); cliOptions.add(new CliOption(RECURSION_LIMIT, "Set the recursion limit. If not set, use the system default value.")); cliOptions.add(new CliOption(MAP_NUMBER_TO, "Map number to Union[StrictFloat, StrictInt], StrictStr or float.") .defaultValue("Union[StrictFloat, StrictInt]")); @@ -222,6 +225,10 @@ public void processOpts() { additionalProperties.put("apiDocPath", apiDocPath); additionalProperties.put("modelDocPath", modelDocPath); + if (additionalProperties.containsKey(SET_ENSURE_ASCII_TO_FALSE)) { + additionalProperties.put(SET_ENSURE_ASCII_TO_FALSE, Boolean.valueOf(additionalProperties.get(SET_ENSURE_ASCII_TO_FALSE).toString())); + } + if (additionalProperties.containsKey(PACKAGE_URL)) { setPackageUrl((String) additionalProperties.get(PACKAGE_URL)); } diff --git a/modules/openapi-generator/src/main/resources/python/rest.mustache b/modules/openapi-generator/src/main/resources/python/rest.mustache index 31e45fe5c10e..07aa7ee3feff 100644 --- a/modules/openapi-generator/src/main/resources/python/rest.mustache +++ b/modules/openapi-generator/src/main/resources/python/rest.mustache @@ -168,7 +168,7 @@ class RESTClientObject: ): request_body = None if body is not None: - request_body = json.dumps(body, ensure_ascii={{ensureAscii}}) + request_body = json.dumps(body{{#setEnsureAsciiToFalse}}, ensure_ascii=False{{/setEnsureAsciiToFalse}}) r = self.pool_manager.request( method, url, diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py index fbb002c0f6bd..02b8176438b1 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/rest.py @@ -179,7 +179,7 @@ def request( ): request_body = None if body is not None: - request_body = json.dumps(body) + request_body = json.dumps(body, ensure_ascii=False) r = self.pool_manager.request( method, url,