Skip to main content
https://github.com/correl/tornado-openapi3/actions/workflows/test.yml/badge.svg?branch=master https://codecov.io/gh/correl/tornado-openapi3/branch/master/graph/badge.svg?token=CTYWWDXTL9 https://readthedocs.org/projects/tornado-openapi3/badge/ https://img.shields.io/badge/code%20style-black-000000.svg

Tornado OpenAPI 3 request and response validation library.

Provides integration between the Tornado web framework and Openapi-core library for validating request and response objects against an OpenAPI 3 specification.

Full documentation is available at https://tornado-openapi3.readthedocs.io

Usage

Adding validation to request handlers

import tornado.ioloop
import tornado.web
from tornado_openapi3.handler import OpenAPIRequestHandler


class MyRequestHandler(OpenAPIRequestHandler):
    spec_dict = {
        "openapi": "3.0.0",
        "info": {
            "title": "Simple Example",
            "version": "1.0.0",
        },
        "paths": {
            "/": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "Index",
                            "content": {
                                "text/html": {
                                    "schema": {"type": "string"},
                                }
                            },
                        }
                    }
                }
            }
        },
    }


class RootHandler(MyRequestHandler):
    async def get(self):
        self.finish("Hello, World!")


if __name__ == "__main__":
    app = tornado.web.Application([(r"/", RootHandler)])
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Validating responses in tests

import unittest

import tornado.web
from tornado_openapi3.testing import AsyncOpenAPITestCase


class RootHandler(tornado.web.RequestHandler):
    async def get(self):
        self.finish("Hello, World!")


class BaseTestCase(AsyncOpenAPITestCase):
    spec_dict = {
        "openapi": "3.0.0",
        "info": {
            "title": "Simple Example",
            "version": "1.0.0",
        },
        "paths": {
            "/": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "Index",
                            "content": {
                                "text/html": {
                                    "schema": {"type": "string"},
                                }
                            },
                        }
                    }
                }
            }
        },
    }

    def get_app(self):
        return tornado.web.Application([(r"/", RootHandler)])

    def test_root_endpoint(self):
        response = self.fetch("/")
        self.assertEqual(200, response.code)
        self.assertEqual(b"Hello, World!", response.body)


if __name__ == "__main__":
    unittest.main()

Contributing

Getting Started

This project uses Poetry to manage its dependencies. To set up a local development environment, just run:

poetry install

Formatting Code

The Black tool is used by this project to format Python code. It is included as a development dependency, and should be run on all committed code. To format code prior to committing it and submitting a PR, run:

poetry run black .

Running Tests

pytest is the preferred test runner for this project. It is included as a development dependency, and is configured to track code coverage, Flake8 style compliance, and Black code formatting. Tests can be run in your development environment by running:

poetry run pytest

Additionally, tests can be run using tox, which will run the tests using multiple versions of both Python and Tornado to ensure broad compatibility.

Configuring Hypothesis

Many of the tests make use of Hypothesis to specify their expectations and generate a large volume of randomized test input. Because of this, the tests may take a long time to run on slower computers. Two profiles are defined for Hypothesis to use which can be selected by setting the HYPOTHESIS_PROFILE environment variable to one of the following values:

ci

Runs tests using the default Hypothesis settings (100 examples per test) and no completion deadline.

dev

The fastest profile, meant for local development only. Uses only 10 examples per test with no completion deadline.

Metadata

Release files for tornado-openapi3 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tornado-openapi3 1.2.0
File Size Uploaded
tornado_openapi3-1.2.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tornado-openapi3 1.2.0
File Interpreter ABI Platform
tornado_openapi3-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / tornado_openapi3-1.2.0.tar.gz

Download URL tornado_openapi3-1.2.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fc382f574fa47c464be9d237fe76fdf06a5a47a6fb6de360155bdb126b56483d
BLAKE2b-256 checksum
How to use checksums
647ce189ba4f8638a87d44f8d5926345290ec1bbf68bef7675370669d95f1aa0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / tornado_openapi3-1.2.0-py3-none-any.whl

Download URL tornado_openapi3-1.2.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3f08644dcd8bbb3ace4544ee90de2bb80b6064438e5ad56491555537983f7b15
BLAKE2b-256 checksum
How to use checksums
ba118d3dc4f30c8c9eb00f3e72cb58e7dcc7b9140087491090ef6781a2d9bd8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page