[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 <wing328hk@gmail.com>
This commit is contained in:
vanjur 2022-01-24 02:38:19 -06:00 committed by GitHub
parent 0fc3f65ce3
commit 7f07fa5ba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -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<String, Object> vendorExtensions) {
if (pattern != null) {
@ -322,6 +329,13 @@ public class PythonLegacyClientCodegen extends AbstractPythonCodegen implements
+ "/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<String> modifiers = new ArrayList<String>();

View File

@ -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");
}

View File

@ -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':