Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error mapping Python regex to OpenAPI. #7204

Closed
Behoston opened this issue Feb 28, 2020 · 4 comments · Fixed by #7389
Closed

Error mapping Python regex to OpenAPI. #7204

Behoston opened this issue Feb 28, 2020 · 4 comments · Fixed by #7389

Comments

@Behoston
Copy link
Contributor

Behoston commented Feb 28, 2020

Checklist

  • [ v ] I have verified that that issue exists against the master branch of Django REST framework.
  • [ v ] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ v ] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [ x ] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [ v ] I have reduced the issue to the simplest possible case.
  • [ x ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

Use URLField in model and use openapi schema generator. Then copy-paste generated JSON to editor.swagger.io

Expected behavior

Regexp should be valid for openapi.

Actual behavior

Regexp is invalid.

obraz

pattern: "^(?:[a-z0-9\\.\\-\\+]*)://(?:[^\\s:@/]+(?::[^\\s:@/]*)?@)?(?:(?:25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}|\\[[0-9a-f:\\.]+\\]|([a-z¡-\uFFFF0-9](?:[a-z¡-\uFFFF0-9-]{0,61}[a-z¡-\uFFFF0-9])?(?:\\.(?!-)[a-z¡-\uFFFF0-9-]{1,63}(?<!-))*\\.(?!-)(?:[a-z¡-\uFFFF-]{2,63}|xn--[a-z0-9]{1,59})(?<!-)\\.?|localhost))(?::\\d{2,5})?(?:[/?#][^\\s]*)?\\Z"
@carltongibson carltongibson added this to the 3.12 Release milestone Apr 6, 2020
@carltongibson carltongibson changed the title OpenAPI schema URL regexp is invalid Error mapping Python regex to OpenAPI. Apr 6, 2020
@carltongibson carltongibson removed this from the 3.12 Release milestone Apr 6, 2020
@carltongibson
Copy link
Collaborator

The regex comes straight off the validator, which we attach in map_field_validators(). It's a good regex, so why isn't it acceptable to the Swagger validator?

I can only find one match for "regex" in the spec.

So..

  1. What's the required regex format?
  2. Is there any way of mapping to that from the validator?
  3. If not, do we need to skip adding the pattern for all cases, or can we identify a subset?

@dhaval-mehta
Copy link
Contributor

@carltongibson I found the root cause of this problem.
Swagger Editor does not allow regex to be wrapped in "(double-quotes). It must be wrapped in '(single-quotes).

For example:
pattern: '^(\d+.)?(\d+.)?(*|\d+)$' => No error
pattern: ^(\d+.)?(\d+.)?(*|\d+)$ => No error
pattern: "^(\d+.)?(\d+.)?(*|\d+)$" => Error

I also found that pyyaml wraps it in ". Also, not all regex are wrapped like this. Checkout: https://i.pinimg.com/originals/76/d9/52/76d952bf3c40d001dbdcb39c65f3c4f1.png

@carltongibson
Copy link
Collaborator

Hi @dhaval-mehta. Nice.

So two questions:

Swagger Editor does not allow regex to be wrapped in "(double-quotes). It must be wrapped in '(single-quotes).

Is that a reasonable restriction? What do the Swagger folks say about that?

I also found that pyyaml wraps it in ". Also, not all regex are wrapped like this.

What do the pyyaml folks say about that?

I'm not seeing anything we can action as yet... 🤔

@dhaval-mehta
Copy link
Contributor

I have fixed this issue in #7389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants