From bb1a44b0c75471b5c004170552540b9632f89810 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sat, 3 Oct 2026 12:14:01 +0000 Subject: Add /openapi.json describing the REST API. * Cargo.toml (dependencies): Add utoipa, feature axum_extras; add utoipa-axum. * src/lib.rs: Use utoipa_axum::router::OpenApiRouter and routes! instead of axum::Router and route!, so paths register into an OpenApi document as well as the router. (ApiDoc): New struct, deriving utoipa::OpenApi with title, description and license. (app): Add /openapi.json, serving the OpenApi document split out of OpenApiRouter via split_for_parts. (hello_world, version, species): Add #[utoipa::path(...)] attributes describing each endpoint's responses. (VersionInfo): Derive utoipa::ToSchema in addition to Serialize. * src/db.rs (Species): Likewise. * tests/openapi.rs: New file. Checks /openapi.json's info.title and that it lists all three endpoints. * test/python/test_openapi.py: New file. Likewise. * README.md: Document /openapi.json. * rust-package.scm: Regenerate with guix import crate -f Cargo.lock genenetwork4. Adds crate-source definitions for utoipa, utoipa-gen, utoipa-axum and pastey. * guix.scm (genenetwork4-git)[native-inputs]: Add rust-utoipa-6.0.0, rust-utoipa-gen-6.0.1, rust-utoipa-axum-0.3.0 and rust-pastey-0.2.3. --- test/python/test_openapi.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/python/test_openapi.py (limited to 'test/python/test_openapi.py') diff --git a/test/python/test_openapi.py b/test/python/test_openapi.py new file mode 100644 index 0000000..03100b9 --- /dev/null +++ b/test/python/test_openapi.py @@ -0,0 +1,28 @@ +"""Mirrors tests/openapi.rs.""" + +import http.client +import json +import unittest + +from server_test_case import HOST, PORT, ServerTestCase + + +class OpenApiTest(ServerTestCase): + def test_openapi_json_describes_all_endpoints(self): + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=5) + try: + conn.request("GET", "/openapi.json") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 200) + + doc = json.loads(body) + self.assertEqual(doc["info"]["title"], "GeneNetwork4 API") + for path in ("/", "/version", "/species"): + self.assertIn(path, doc["paths"]) + + +if __name__ == "__main__": + unittest.main() -- cgit 1.4.1