From 08d1459a4a400083c92f8d7cf7738899b560bdf0 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 15 Nov 2018 13:57:58 +0100 Subject: 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//') 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 --- requirements-test.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'requirements-test.txt') diff --git a/requirements-test.txt b/requirements-test.txt index 6c9bd88..cfb8ce7 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -2,3 +2,4 @@ mysqlclient==1.3.13 psycopg2-binary==2.7.6 sqlparse==0.2.4 python-dateutil==2.7.5 +openapi-core==0.7.1 -- cgit v1.2.3