Fixed regular expression in python client codegen that was removing any trailing chars instead of only expected ones (#13164)

* Fixed regular expression in python client codegen

The previous regular expression was too loose, including any last character in the second group. This commit fixes that, making sure we only remove leading forward slashes, trailing forward slashes or trailing `\i`.

This commit closes 13069.

* Added generated files

* Edit comment

* Add test cases

Co-authored-by: antonio.silva <antonio.silva@feedzai.com>
This commit is contained in:
antssilva96 2022-08-16 17:06:53 +01:00 committed by GitHub
parent 4e123436d0
commit 7e5ee2142c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 5 deletions

View File

@ -1173,9 +1173,9 @@ public class PythonClientCodegen extends PythonLegacyClientCodegen {
String pattern = schema.getPattern();
/*
RxGen does not support our ECMA dialect https://github.com/curious-odd-man/RgxGen/issues/56
So strip off the leading / and trailing / and turn on ignore case if we have it
So strip off the leading /, trailing / and trailing /i, and turn on ignore case if we have it
*/
Pattern valueExtractor = Pattern.compile("^/?(.+?)/?(.?)$");
Pattern valueExtractor = Pattern.compile("^/?(.+?)/?(i?)$");
Matcher m = valueExtractor.matcher(pattern);
RgxGen rgxGen = null;
if (m.find()) {

View File

@ -35,6 +35,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.PythonClientCodegen;
import org.openapitools.codegen.utils.ModelUtils;
@ -453,6 +456,30 @@ public class PythonClientTest {
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");
// ignore warnings, should be the same as in issue_11521.yaml
Pattern pattern = Pattern.compile("[\\d]{4}-[\\d]{2}-[\\d]{2}T[\\d]{1,2}:[\\d]{2}Z");
Matcher matcher = pattern.matcher(property1.example);
Assert.assertTrue(matcher.find());
}
@Test(description = "tests uuid example works even if a pattern is provided")
public void testUuidExampleWorksEvenIfPatternIsDefined() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/issues_13069.yaml");
final DefaultCodegen codegen = new PythonClientCodegen();
codegen.setOpenAPI(openAPI);
Operation operation = openAPI.getPaths().get("/test").getGet();
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
codegen.setParameterExampleValue(codegenParameter, operation.getParameters().get(0));
String modelName = "UUID";
Schema modelSchema = ModelUtils.getSchema(openAPI, modelName);
final CodegenModel model = codegen.fromModel(modelName, modelSchema);
Pattern pattern = Pattern.compile("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}");
Matcher matcher = pattern.matcher(codegenParameter.example);
Assert.assertTrue(matcher.find());
}
@Test(description = "tests RecursiveToExample")

View File

@ -0,0 +1,23 @@
---
openapi: 3.0.3
info:
title: Test
version: 1.0.0-SNAPSHOT
paths:
/test:
get:
parameters:
- name: uuid
in: query
schema:
$ref: '#/components/schemas/UUID'
responses:
"200":
description: OK
components:
schemas:
UUID:
format: uuid
pattern: "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
type: string

View File

@ -865,7 +865,7 @@ with petstore_api.ApiClient(configuration) as api_client:
api_instance = fake_api.FakeApi(api_client)
number = 32.1 # float | None
double = 67.8 # float | None
pattern_without_delimiter = "Aj" # str | None
pattern_without_delimiter = "AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>" # str | None
byte = 'YQ==' # str | None
integer = 10 # int | None (optional)
int32 = 20 # int | None (optional)

View File

@ -865,7 +865,7 @@ with petstore_api.ApiClient(configuration) as api_client:
api_instance = fake_api.FakeApi(api_client)
number = 32.1 # float | None
double = 67.8 # float | None
pattern_without_delimiter = "Aj" # str | None
pattern_without_delimiter = "AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>" # str | None
byte = 'YQ==' # str | None
integer = 10 # int | None (optional)
int32 = 20 # int | None (optional)

View File

@ -1327,7 +1327,7 @@ with petstore_api.ApiClient(configuration) as api_client:
api_instance = fake_api.FakeApi(api_client)
number = 32.1 # float | None
double = 67.8 # float | None
pattern_without_delimiter = "Aj" # str | None
pattern_without_delimiter = "AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>" # str | None
byte = 'YQ==' # str | None
integer = 10 # int | None (optional)
int32 = 20 # int | None (optional)