]> git.ipfire.org Git - thirdparty/patchwork.git/commit
Add REST API validation using OpenAPI schema
authorStephen Finucane <stephen@that.guru>
Thu, 15 Nov 2018 12:57:58 +0000 (13:57 +0100)
committerStephen Finucane <stephen@that.guru>
Sat, 22 Dec 2018 17:19:07 +0000 (17:19 +0000)
commit08d1459a4a400083c92f8d7cf7738899b560bdf0
tree657dc060fae0a73a1ec79dd2ad84e0d4c0575e27
parentf74e54a38d2dbc628ed47bb7cd5c2cbfb5833652
Add REST API validation using OpenAPI schema

Add validation using the rather excellent 'openapi_core' library. The
biggest issue we have to contend with is the fact that 'openapi_core'
expects us to be able to provide a templated URL string for each request
(e.g. '/api/patches/123/' would become '/api/patches/<id>/') and Django
doesn't provide a way to do this [*]. We work around this by
reverse-engineering some of the Django code to turn a URL to its
matching regex, which we can then easily convert into a template string.
It's kind of hacky and not at all portable but, crucially, it does work
and has highlighted some nice bugs in the API that have already merged.

Going forward, we can probably modify 'openapi_core' somewhat to remove
the need for the templated URL string. If and when this happens, most of
the funkier code here can happily go away.

[*] Django 2.0+ [1] does actually provide a way to do template
string-based URLs and in fact recommends them now, with regexes being
reserved for more advanced corner cases. However, we don't want to drop
support for the Django 1.11 yet as it is the most recent LTS release.

[1] https://docs.djangoproject.com/en/2.1/ref/urls/#django.urls.path

Signed-off-by: Stephen Finucane <stephen@that.guru>
13 files changed:
patchwork/tests/api/test_bundle.py
patchwork/tests/api/test_check.py
patchwork/tests/api/test_comment.py
patchwork/tests/api/test_cover.py
patchwork/tests/api/test_event.py
patchwork/tests/api/test_patch.py
patchwork/tests/api/test_person.py
patchwork/tests/api/test_project.py
patchwork/tests/api/test_series.py
patchwork/tests/api/test_user.py
patchwork/tests/api/utils.py
patchwork/tests/api/validator.py [new file with mode: 0644]
requirements-test.txt