From 7f07fa5ba0ea933b2cc9f4b565b187bd521b09ca Mon Sep 17 00:00:00 2001 From: vanjur <41238964+vanjur@users.noreply.github.com> Date: Mon, 24 Jan 2022 02:38:19 -0600 Subject: [PATCH] [Python] Some regex patterns can generate invalid code. #6675 (#10920) * fix issue 6675 & add javadoc * fix formatting issue * update JUnit test for 6675 * build & update samples for PR * clean package and regenerating samples * add progress for fix to issue 10957 * Revert "add progress for fix to issue 10957" This reverts commit 8240c7ccb17141f7551ab34eda864ab4e068ebd8. * fix version issues * fix more versioning issues * fix discrepancies with backslashes * update samples Co-authored-by: William Cheng --- .../languages/PythonLegacyClientCodegen.java | 14 ++++++++++++++ .../python/PythonLegacyClientCodegenTest.java | 3 +++ .../src/test/resources/3_0/issue_1517.yaml | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java index 7fc09f0cbfa..bdaa9a06ef3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonLegacyClientCodegen.java @@ -311,6 +311,13 @@ public class PythonLegacyClientCodegen extends AbstractPythonCodegen implements * The OpenAPI pattern spec follows the Perl convention and style of modifiers. Python * does not support this in as natural a way so it needs to convert it. See * https://docs.python.org/2/howto/regex.html#compilation-flags for details. + * + * @param pattern (the String pattern to convert from python to Perl convention) + * @param vendorExtensions (list of custom x-* properties for extra functionality-see https://swagger.io/docs/specification/openapi-extensions/) + * @return void + * @throws IllegalArgumentException if pattern does not follow the Perl /pattern/modifiers convention + * + * Includes fix for issue #6675 */ public void postProcessPattern(String pattern, Map vendorExtensions) { if (pattern != null) { @@ -321,6 +328,13 @@ public class PythonLegacyClientCodegen extends AbstractPythonCodegen implements throw new IllegalArgumentException("Pattern must follow the Perl " + "/pattern/modifiers convention. " + pattern + " is not valid."); } + + //check for instances of extra backslash that could cause compile issues and remove + int firstBackslash = pattern.indexOf("\\"); + int bracket = pattern.indexOf("["); + if (firstBackslash == 0 || firstBackslash == 1 || firstBackslash == bracket+1) { + pattern = pattern.substring(0,firstBackslash)+pattern.substring(firstBackslash+1); + } String regex = pattern.substring(1, i).replace("'", "\\'"); List modifiers = new ArrayList(); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonLegacyClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonLegacyClientCodegenTest.java index d3bff53a8ea..72dbdda27ab 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonLegacyClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonLegacyClientCodegenTest.java @@ -91,6 +91,9 @@ public class PythonLegacyClientCodegenTest { Assert.assertEquals(op.allParams.get(4).pattern, "/^pattern\\/\\d{3}$/"); // pattern_with_modifiers '/^pattern\d{3}$/i Assert.assertEquals(op.allParams.get(5).pattern, "/^pattern\\d{3}$/i"); + // pattern_with_backslash_after_bracket '/^[\pattern\d{3}$/i' + // added to test fix for issue #6675 + Assert.assertEquals(op.allParams.get(6).pattern, "/^[\\pattern\\d{3}$/i"); } diff --git a/modules/openapi-generator/src/test/resources/3_0/issue_1517.yaml b/modules/openapi-generator/src/test/resources/3_0/issue_1517.yaml index 038a637608f..788b46a6dd5 100644 --- a/modules/openapi-generator/src/test/resources/3_0/issue_1517.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/issue_1517.yaml @@ -45,6 +45,11 @@ paths: schema: type: string pattern: '/^pattern\d{3}$/i' + - name: pattern_with_backslash_after_bracket + in: header + schema: + type: string + pattern: '/^[\pattern\d{3}$/i' responses: '200':