From 5ce237e00471d885f05e6d979ec777552809b3b1 Mon Sep 17 00:00:00 2001 From: Dhaval Mehta <20968146+dhaval-mehta@users.noreply.github.com> Date: Sun, 28 Jun 2020 17:58:59 +0530 Subject: [PATCH] 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 --- rest_framework/schemas/openapi.py | 4 +++- tests/schemas/test_openapi.py | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 9b3082822..9774a94c7 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -554,7 +554,9 @@ class AutoSchema(ViewInspector): if isinstance(v, URLValidator): schema['format'] = 'uri' 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): attr_name = 'maxLength' if isinstance(field, serializers.ListField): diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 0e86a7f50..d483f3d45 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -855,6 +855,7 @@ class TestOperationIntrospection(TestCase): assert properties['url']['type'] == 'string' assert properties['url']['nullable'] is True assert properties['url']['default'] == 'http://www.example.com' + assert '\\Z' not in properties['url']['pattern'] assert properties['uuid']['type'] == 'string' assert properties['uuid']['format'] == 'uuid'