#31819 closed Bug (invalid)
Django + Django REST Framework generates API errors with swagger
Reported by: | Perry Harrington | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | 3.0 |
Severity: | Normal | Keywords: | swagger regex javascrip rest openapi |
Cc: | Perry Harrington | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The Django Validators use the syntax \Z to designate an end of line, similar to the more conventional $ marker.
This swagger ticket gives context to the following: https://github.com/swagger-api/swagger-editor/issues/1601
Javascript uses the $ as the end anchor, this has slightly different semantics to the intended \Z anchor in Python.
The problem is that swagger chokes on this syntax, and looking at the code it comes directly from the Python regexes being exported to the openapi spec.
I propose 2 possible solutions:
- Change the Django regexes to use the $ anchor, this may have unintended behavior. The \Z regex is not valid as an anchor in Javascript.
- Run all of the python regexes through a simple replace, changing \Z for $
I'm open to any other suggestions, but as I see it now, the yaml produced from Django regexes is not compliant with Javascript and javascript validators by extension.
I can manually edit yaml files, but I'd like to have a dynamic API endpoint for schema definitions.
Change History (6)
comment:1 by , 4 years ago
Cc: | added |
---|
comment:3 by , 4 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Hi Perry. This isn't in scope for Django: differences between language implementations of regex isn't something we're going to address.
The particular issue with DRF OpenAPI schema generation should (at least in part) be addressed by this PR, which was merged into DRF. That will be part of DRF 3.12, which is pending, but has been delayed by (let us just call it) 2020, and will be released some time soon. In the meantime you can install the latest master using pip's VCS support.
If you find there are further changes needed, please open a new issue or preferably a PR on the DRF repo: it is appropriate there that we'll do what we can to map between the required formats when generating the OpenAPI schema.
Thanks.
comment:4 by , 4 years ago
Hi, thank you for your reply.
I looked at the commit and it changes \Z to \z, from my research and reading \z is not a valid anchor in several languages.
I think that $ is a more suitable replacement based on behavior, since \Z matched before a newline like $ in most languages.
Like you said, not Django's problem, DRF's problem.
Let's see if I get any traction on that front.
I see you are the one who merged that pull request, would you care to elaborate on why \z was accepted over $?
For me it's a simple code hack, but I would really like for it to be upstream.
comment:5 by , 4 years ago
Quick update because an edit doesn't trigger an email and you won't have context in email:
I see you are the one who merged that pull request, would you care to elaborate on why \z was accepted over $?
For me it's a simple code hack, but I would really like for it to be upstream.
comment:6 by , 4 years ago
It's best effort — the contributor suggested that fix (targeting JavaScript I think.)
I'm sceptical that there's any perfect solution here. If you can do better, please do suggest a PR with reasoning on DRF.
Here's a comparison of the regex APIs for Python and Javascript:
Python:
\Z
$
Javascript:
$
PHP:
\Z
$