Skip to content

Commit

Permalink
[Python] python regex validation generation (#11525)
Browse files Browse the repository at this point in the history
* fix: python regex validation generation

* docs: updated comment to be more specific

* fix: check the right value used when generating the regex
  • Loading branch information
aaronatbissell authored Feb 18, 2022
1 parent d45cb65 commit 0d4dba1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,6 @@ public void postProcessPattern(String pattern, Map<String, Object> vendorExtensi
+ "/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>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,21 @@ public void testObjectWithValidations() {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public void testRegularExpressionOpenAPISchemaVersion3() {
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");

}


Expand Down
17 changes: 17 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_11521.yaml
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 0d4dba1

Please sign in to comment.