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 <carlton@noumenal.es>
This commit is contained in:
Dhaval Mehta 2020-06-28 17:58:59 +05:30 committed by GitHub
parent 19915d1917
commit 5ce237e004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 1 deletions

View File

@ -554,7 +554,9 @@ class AutoSchema(ViewInspector):
if isinstance(v, URLValidator): if isinstance(v, URLValidator):
schema['format'] = 'uri' schema['format'] = 'uri'
if isinstance(v, RegexValidator): if isinstance(v, RegexValidator):
schema['pattern'] = v.regex.pattern # In Python, the token \Z does what \z does in other engines.
# https://stackoverflow.com/questions/53283160
schema['pattern'] = v.regex.pattern.replace('\\Z', '\\z')
elif isinstance(v, MaxLengthValidator): elif isinstance(v, MaxLengthValidator):
attr_name = 'maxLength' attr_name = 'maxLength'
if isinstance(field, serializers.ListField): if isinstance(field, serializers.ListField):

View File

@ -855,6 +855,7 @@ class TestOperationIntrospection(TestCase):
assert properties['url']['type'] == 'string' assert properties['url']['type'] == 'string'
assert properties['url']['nullable'] is True assert properties['url']['nullable'] is True
assert properties['url']['default'] == 'http://www.example.com' assert properties['url']['default'] == 'http://www.example.com'
assert '\\Z' not in properties['url']['pattern']
assert properties['uuid']['type'] == 'string' assert properties['uuid']['type'] == 'string'
assert properties['uuid']['format'] == 'uuid' assert properties['uuid']['format'] == 'uuid'