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

How to create parameters with regex patterns? #738

Closed
PaulWay opened this issue May 17, 2022 · 7 comments
Closed

How to create parameters with regex patterns? #738

PaulWay opened this issue May 17, 2022 · 7 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@PaulWay
Copy link
Contributor

PaulWay commented May 17, 2022

Hi there!

I'm just coming to drf_spectacular after using drf_yasg for many years, and I'm having to change all my openapi.Parameter definitions over to OpenApiParameter. What's really puzzling me is that I can set type=OpenApiTypes.REGEX, but I cannot set a pattern when initialising the parameter. I understand that serializers can use Regex validators, but I can't see how to add a regex pattern to a parameter.

I'm prepared to write the code to support it, but I thought I'd check first the correct way of setting a regex pattern on a parameter.

Thanks in advance,

Paul

tfranzel added a commit that referenced this issue May 17, 2022
@tfranzel
Copy link
Owner

Hi!

it is simply an oversight. I just added the possibility to add a pattern via the interface. You can also do type=str as the format is only informative and should yield the same outcome.

OpenApiParameter(name='test', type=OpenApiTypes.REGEX, pattern='^[0-9]{3}$')
OpenApiParameter(name='test', type=str, pattern='^[0-9]{3}$') # alternatively

Until this is released you can also do it manually with (raw schema support)

OpenApiParameter(name='test', type={'pattern': '^[0-9]{3}$', 'type': 'string', 'format': 'regex'})

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels May 17, 2022
@PaulWay
Copy link
Contributor Author

PaulWay commented May 17, 2022

OK, interesting. I'd have to check my code at work, but I did try to add the pattern keyword to the OpenApiParameter declaration and it stopped with an error that pattern wasn't a recognised argument.

@tfranzel
Copy link
Owner

@PaulWay you are reading it wrong 😄 I just added the parameter with above commit. You refer to the pattern that is part of the dict that I gave to the type argument.

you can write arbitrary schemas into a dict and pass it to type. this is a fallback mechanism available all throughout spectacular.

@PaulWay
Copy link
Contributor Author

PaulWay commented May 17, 2022

Ah - I'm with you now :-)

@aupi0
Copy link

aupi0 commented May 20, 2022

On a similar note would it be possible/is there already a method to put a max_length constraint on OpenApiParameters (similar to fields)?

@tfranzel
Copy link
Owner

@aupi0 no sry there is not. You would need to write a small custom schema dict as outlined above. The interface is already somewhat large and I don't want to dilute it further. max_length would not be the only potential addition, and adding all other possibilities simply does not scale. max_length is not special in that regard.

@tfranzel
Copy link
Owner

I'll close this issue as the initial ask as added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

3 participants