Skip to main content

openapi-bridge

OpenAPI endpoint decorator with pydantic (>=2.0.0) integration.

Allows for almost seamless integration of pydantic models with OpenAPI, generating YAML for the endpoint from type hints.

Using the @endpoint decorator

Example:

@endpoint("/foo/{foo_id}")
def foo(*, foo_id: int, debug: Optional[bool] = False) -> pydantic_models.Foo:
   """
       Foo summary.

       Lengthy foo description.

       @param foo_id The Foo ID in the database.
       @param debug Set to True to include debug information.
   """
   result = _do_something(id, debug)
   return pydantic_models.Foo(**result)

As you can see from the example, the decorator takes a path (which may include a path parameter, in this case id). You can also give it an HTTP method, a path prefix (e.g. to distinguish between internal and external API functions), and security directives.

Information about the endpoint is gathered from both the type annotations of the decorated function and its docstring.

(!) Every parameter (except the optional user) must be keyword-only, have a type hint and a @param help text in the docstring. Un-annotated or undocumented parameters are considered to be a hard error and will raise an exception on startup.

Normally you can just return an instance of the annotated type, and the decorator will handle it correctly, adding an HTTP status 200. If you need to return something else, e.g. some redirect or a 204 ("no content"), you can to return a (raw_content, http_status) tuple instead, e.g.:

return None, 204

The docstring of an endpoint contains its summary, description, the section the documentation is listed under, and parameter help, as well as (optionally) its response in various circumstances.

The summary is the first paragraph of the docstring; the description is taken to be any further paragraphs until the first @keyword.

We recognize the following keywords to designate parts of the documentation:

  • @section <section name>: endpoint is listed in this section.
  • @param <param name> <help text>: explanation of the given parameter.
  • @example <param name> <example text>: example values of the parameter.
  • @response <http status> <JSON>: allows for non-standard responses.

YAML Generation

If you're building a Connexion app, you can use the collected endpoints in your create_app() function:

def create_app():
    # TODO: import all modules with @endpoints here!!
    api_specs = {
        "paths": openapi_bridge.PATHS["default"],
        **openapi_bridge.get_pydantic_schemata(pydantic_models),
    }
    connexion_app.add_api(api_specs)

(!) You need to import all the modules with endpoints here in order to register them. This is easy to forget! If you test a new endpoint and only ever get 404, you might have forgotten to import that module ;-)

Metadata

Release files for openapi-bridge 0.5.1

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

Source distribution (sdist)

Source distribution for openapi-bridge 0.5.1
File Size Uploaded Provenance
openapi_bridge-0.5.1.tar.gz 11.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for openapi-bridge 0.5.1
File Interpreter ABI Platform
openapi_bridge-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.5 kB

Release files / openapi_bridge-0.5.1.tar.gz

Download URL openapi_bridge-0.5.1.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b8e01666f7c3949a2f951b1f99cd213951f03015856332938c30bc78e72709f5
BLAKE2b-256 checksum
How to use checksums
52010b62900bda8fc29d0e9240801f01f0c43a09f5e17405f4e4ab75277db96e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.10.0 Darwin/24.6.0

Release files / openapi_bridge-0.5.1-py3-none-any.whl

Download URL openapi_bridge-0.5.1-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48e494aea987cab4aeb8ab8cdf9e2684e0e46216dd832fc23733d7fabc572461
BLAKE2b-256 checksum
How to use checksums
052e77bff7221e1d4e918e3d48ba9c41b9dae3649ec473c89d3982aea4be72dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.10.0 Darwin/24.6.0

Security for openapi-bridge 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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