[Python] python regex validation generation (#11525)

* fix: python regex validation generation

* docs: updated comment to be more specific

* fix: check the right value used when generating the regex
This commit is contained in:
Aaron Pittenger 2022-02-18 00:11:39 -05:00 committed by GitHub
parent d45cb6511f
commit 0d4dba13f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -329,13 +329,6 @@ 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

@ -440,6 +440,21 @@ public class PythonClientTest {
Assert.assertEquals((int) model.getMinProperties(), 1);
}
@Test(description = "tests RegexObjects")
public void testRegexObjects() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_11521.yaml");
final DefaultCodegen codegen = new PythonClientCodegen();
codegen.setOpenAPI(openAPI);
String modelName = "DateTimeObject";
Schema modelSchema = ModelUtils.getSchema(openAPI, modelName);
final CodegenModel model = codegen.fromModel(modelName, modelSchema);
final CodegenProperty property1 = model.vars.get(0);
Assert.assertEquals(property1.baseName, "datetime");
Assert.assertEquals(property1.pattern, "/[\\d]{4}-[\\d]{2}-[\\d]{2}T[\\d]{1,2}:[\\d]{2}Z/");
Assert.assertEquals(property1.vendorExtensions.get("x-regex"), "[\\d]{4}-[\\d]{2}-[\\d]{2}T[\\d]{1,2}:[\\d]{2}Z");
}
@Test(description = "tests RecursiveToExample")
public void testRecursiveToExample() throws IOException {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issue_8052_recursive_model.yaml");

View File

@ -93,7 +93,9 @@ public class PythonLegacyClientCodegenTest {
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");
// removed because "/^[\\pattern\\d{3}$/i" is invalid regex because [ is not escaped and there is no closing ]
// Assert.assertEquals(op.allParams.get(6).pattern, "/^[\\pattern\\d{3}$/i");
}

View File

@ -0,0 +1,17 @@
openapi: 3.0.0
info:
description: a spec to test free form object models
version: 1.0.0
title: OpenAPI Petstore
license:
name: Apache-2.0
url: "https://www.apache.org/licenses/LICENSE-2.0.html"
tags: []
paths: {}
components:
schemas:
DateTimeObject:
properties:
datetime:
type: string
pattern: '[\d]{4}-[\d]{2}-[\d]{2}T[\d]{1,2}:[\d]{2}Z'