Skip to content

Commit 4895b56

Browse files
authored
[python-nextgen] fix pattern with double quote (#15073)
* fix pattern with double quote * fix test
1 parent 0973795 commit 4895b56

8 files changed

Lines changed: 27 additions & 5 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonNextgenClientCodegen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ public void postProcessPattern(String pattern, Map<String, Object> vendorExtensi
12011201
}
12021202
}
12031203

1204-
vendorExtensions.put("x-regex", regex);
1204+
vendorExtensions.put("x-regex", regex.replace("\"","\\\""));
1205+
vendorExtensions.put("x-pattern", pattern.replace("\"","\\\""));
12051206
vendorExtensions.put("x-modifiers", modifiers);
12061207
}
12071208
}

modules/openapi-generator/src/main/resources/python-nextgen/model_generic.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
3333
@validator('{{{name}}}')
3434
def {{{name}}}_validate_regular_expression(cls, v):
3535
if not re.match(r"{{{.}}}", v{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}):
36-
raise ValueError(r"must validate the regular expression {{{pattern}}}")
36+
raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}")
3737
return v
3838
{{/vendorExtensions.x-regex}}
3939
{{#isEnum}}

modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,9 @@ components:
15911591
string:
15921592
type: string
15931593
pattern: '/[a-z]/i'
1594+
string_with_double_quote_pattern:
1595+
type: string
1596+
pattern: 'this is "something"'
15941597
byte:
15951598
type: string
15961599
format: byte

samples/openapi3/client/petstore/python-nextgen-aiohttp/docs/FormatTest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Name | Type | Description | Notes
1212
**double** | **float** | | [optional]
1313
**decimal** | **decimal.Decimal** | | [optional]
1414
**string** | **str** | | [optional]
15+
**string_with_double_quote_pattern** | **str** | | [optional]
1516
**byte** | **str** | | [optional]
1617
**binary** | **str** | | [optional]
1718
**var_date** | **date** | |

samples/openapi3/client/petstore/python-nextgen-aiohttp/petstore_api/models/format_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FormatTest(BaseModel):
3434
double: Optional[confloat(le=123.4, ge=67.8)] = None
3535
decimal: Optional[condecimal()] = None
3636
string: Optional[constr(strict=True)] = None
37+
string_with_double_quote_pattern: Optional[constr(strict=True)] = None
3738
byte: Optional[StrictBytes] = None
3839
binary: Optional[StrictBytes] = None
3940
var_date: date = Field(..., alias="date")
@@ -42,14 +43,20 @@ class FormatTest(BaseModel):
4243
password: constr(strict=True, max_length=64, min_length=10) = ...
4344
pattern_with_digits: Optional[constr(strict=True)] = Field(None, description="A string that is a 10 digit number. Can have leading zeros.")
4445
pattern_with_digits_and_delimiter: Optional[constr(strict=True)] = Field(None, description="A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.")
45-
__properties = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"]
46+
__properties = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"]
4647

4748
@validator('string')
4849
def string_validate_regular_expression(cls, v):
4950
if not re.match(r"[a-z]", v ,re.IGNORECASE):
5051
raise ValueError(r"must validate the regular expression /[a-z]/i")
5152
return v
5253

54+
@validator('string_with_double_quote_pattern')
55+
def string_with_double_quote_pattern_validate_regular_expression(cls, v):
56+
if not re.match(r"this is \"something\"", v):
57+
raise ValueError(r"must validate the regular expression /this is \"something\"/")
58+
return v
59+
5360
@validator('pattern_with_digits')
5461
def pattern_with_digits_validate_regular_expression(cls, v):
5562
if not re.match(r"^\d{10}$", v):
@@ -105,6 +112,7 @@ def from_dict(cls, obj: dict) -> FormatTest:
105112
"double": obj.get("double"),
106113
"decimal": obj.get("decimal"),
107114
"string": obj.get("string"),
115+
"string_with_double_quote_pattern": obj.get("string_with_double_quote_pattern"),
108116
"byte": obj.get("byte"),
109117
"binary": obj.get("binary"),
110118
"var_date": obj.get("date"),

samples/openapi3/client/petstore/python-nextgen-aiohttp/tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_float_strict_type(self):
209209
a = petstore_api.FormatTest(number=39.8, float=123, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876")
210210
self.assertEqual(a.float, 123.0)
211211

212-
json_str = '{"number": 34.5, "float": "456", "date": "2013-12-08", "password": "empty1234567", "pattern_with_digits": "1234567890", "pattern_with_digits_and_delimiter": "image_123" , "string": "string"}'
212+
json_str = "{\"number\": 34.5, \"float\": \"456\", \"date\": \"2013-12-08\", \"password\": \"empty1234567\", \"pattern_with_digits\": \"1234567890\", \"pattern_with_digits_and_delimiter\": \"image_123\", \"string_with_double_quote_pattern\": \"this is \\\"something\\\"\", \"string\": \"string\"}"
213213
# no exception thrown when assigning 456 (integer) to float type since strict is set to false
214214
f = petstore_api.FormatTest.from_json(json_str)
215215
self.assertEqual(f.float, 456.0)

samples/openapi3/client/petstore/python-nextgen/docs/FormatTest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Name | Type | Description | Notes
1212
**double** | **float** | | [optional]
1313
**decimal** | **decimal.Decimal** | | [optional]
1414
**string** | **str** | | [optional]
15+
**string_with_double_quote_pattern** | **str** | | [optional]
1516
**byte** | **str** | | [optional]
1617
**binary** | **str** | | [optional]
1718
**var_date** | **date** | |

samples/openapi3/client/petstore/python-nextgen/petstore_api/models/format_test.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class FormatTest(BaseModel):
3434
double: Optional[confloat(le=123.4, ge=67.8, strict=True)] = None
3535
decimal: Optional[condecimal()] = None
3636
string: Optional[constr(strict=True)] = None
37+
string_with_double_quote_pattern: Optional[constr(strict=True)] = None
3738
byte: Optional[StrictBytes] = None
3839
binary: Optional[StrictBytes] = None
3940
var_date: date = Field(..., alias="date")
@@ -43,14 +44,20 @@ class FormatTest(BaseModel):
4344
pattern_with_digits: Optional[constr(strict=True)] = Field(None, description="A string that is a 10 digit number. Can have leading zeros.")
4445
pattern_with_digits_and_delimiter: Optional[constr(strict=True)] = Field(None, description="A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.")
4546
additional_properties: Dict[str, Any] = {}
46-
__properties = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"]
47+
__properties = ["integer", "int32", "int64", "number", "float", "double", "decimal", "string", "string_with_double_quote_pattern", "byte", "binary", "date", "dateTime", "uuid", "password", "pattern_with_digits", "pattern_with_digits_and_delimiter"]
4748

4849
@validator('string')
4950
def string_validate_regular_expression(cls, v):
5051
if not re.match(r"[a-z]", v ,re.IGNORECASE):
5152
raise ValueError(r"must validate the regular expression /[a-z]/i")
5253
return v
5354

55+
@validator('string_with_double_quote_pattern')
56+
def string_with_double_quote_pattern_validate_regular_expression(cls, v):
57+
if not re.match(r"this is \"something\"", v):
58+
raise ValueError(r"must validate the regular expression /this is \"something\"/")
59+
return v
60+
5461
@validator('pattern_with_digits')
5562
def pattern_with_digits_validate_regular_expression(cls, v):
5663
if not re.match(r"^\d{10}$", v):
@@ -112,6 +119,7 @@ def from_dict(cls, obj: dict) -> FormatTest:
112119
"double": obj.get("double"),
113120
"decimal": obj.get("decimal"),
114121
"string": obj.get("string"),
122+
"string_with_double_quote_pattern": obj.get("string_with_double_quote_pattern"),
115123
"byte": obj.get("byte"),
116124
"binary": obj.get("binary"),
117125
"var_date": obj.get("date"),

0 commit comments

Comments
 (0)