Skip to content

Commit

Permalink
Fix parse errors when property pattern is explicit. (#125)
Browse files Browse the repository at this point in the history
* Fix parse errors when property pattern is explicit.

* Fix test for regex datatypes.

The regex needs to be declared between quotes, otherwise it will be
considered a normal string.

* Remove redundant code.
  • Loading branch information
mgonzalezperna authored May 6, 2020
1 parent 8afd441 commit e979f13
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions datamodel_code_generator/model/pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def get_data_float_type(types: Types, **kwargs: Any) -> DataType:
def get_data_str_type(types: Types, **kwargs: Any) -> DataType:
data_type_kwargs = transform_kwargs(kwargs, string_kwargs)
if data_type_kwargs:
if 'regex' in data_type_kwargs:
data_type_kwargs['regex'] = f'\'{data_type_kwargs["regex"]}\''
return DataType(
type='constr',
is_func=True,
Expand Down
2 changes: 1 addition & 1 deletion tests/model/pydantic/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_get_data_float_type(types, params, data_type):
DataType(
type='constr',
is_func=True,
kwargs={'regex': '^abc'},
kwargs={'regex': "'^abc'"},
imports_=[IMPORT_CONSTR],
),
),
Expand Down

0 comments on commit e979f13

Please sign in to comment.