Skip to content

Commit

Permalink
Convert regex '\Z' to '$'
Browse files Browse the repository at this point in the history
  • Loading branch information
axnsan12 committed Dec 27, 2017
1 parent 1aaec6b commit c85acee
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ def find_regex(regex_field):
regex_validator = validator

# regex_validator.regex should be a compiled re object...
return getattr(getattr(regex_validator, 'regex', None), 'pattern', None)
pattern = getattr(getattr(regex_validator, 'regex', None), 'pattern', None)
if pattern:
# attempt some basic cleanup to remove regex constructs not supported by JavaScript
# -- swagger uses javascript-style regexes - see https://github.com/swagger-api/swagger-editor/issues/1601
if pattern.endswith('\\Z') or pattern.endswith('\\z'):
pattern = pattern[:-2] + '$'

return pattern


numeric_fields = (serializers.IntegerField, serializers.FloatField, serializers.DecimalField)
Expand Down
4 changes: 1 addition & 3 deletions src/drf_yasg/inspectors/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ def get_default_responses(self):

default_status = guess_response_status(method)
default_schema = ''
if method == 'post':
default_schema = self.get_request_serializer() or self.get_view_serializer()
elif method in ('get', 'put', 'patch'):
if method in ('get', 'post', 'put', 'patch'):
default_schema = self.get_request_serializer() or self.get_view_serializer()

default_schema = default_schema or ''
Expand Down
2 changes: 1 addition & 1 deletion tests/reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ definitions:
items:
type: string
format: slug
pattern: ^[-a-zA-Z0-9_]+\Z
pattern: ^[-a-zA-Z0-9_]+$
readOnly: true
readOnly: true
uniqueItems: true
Expand Down

4 comments on commit c85acee

@priyadhoundiyal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, is this included in 1.1.0?
I came across this issue when I couldn't get the Try it Out links to work without ending my kwarg(which was a slug) with a Z.
I've since upgraded from 1.06 to 1.1.0 and still can't get it to work. Is there anything else I could try?

@axnsan12
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not, as you can see it is committed after the 1.1.0 tag.

It will be included in 1.1.1, which I will probably release today.

@axnsan12
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@priyadhoundiyal it's live

@priyadhoundiyal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @axnsan12! :)

Please sign in to comment.