Skip to content

Commit 5ce237e

Browse files
Corrected regex serialization for OpenAPI. (#7389)
* replace \Z by \z in regex * fix test cases for Django >= 3.0 * fix isort * Added comment for why `\z`. Co-authored-by: Carlton Gibson <[email protected]>
1 parent 19915d1 commit 5ce237e

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

rest_framework/schemas/openapi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ def map_field_validators(self, field, schema):
554554
if isinstance(v, URLValidator):
555555
schema['format'] = 'uri'
556556
if isinstance(v, RegexValidator):
557-
schema['pattern'] = v.regex.pattern
557+
# In Python, the token \Z does what \z does in other engines.
558+
# https://stackoverflow.com/questions/53283160
559+
schema['pattern'] = v.regex.pattern.replace('\\Z', '\\z')
558560
elif isinstance(v, MaxLengthValidator):
559561
attr_name = 'maxLength'
560562
if isinstance(field, serializers.ListField):

tests/schemas/test_openapi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ def test_serializer_validators(self):
855855
assert properties['url']['type'] == 'string'
856856
assert properties['url']['nullable'] is True
857857
assert properties['url']['default'] == 'http://www.example.com'
858+
assert '\\Z' not in properties['url']['pattern']
858859

859860
assert properties['uuid']['type'] == 'string'
860861
assert properties['uuid']['format'] == 'uuid'

0 commit comments

Comments
 (0)