oapi-gen-dishka
Inject Dishka dependencies into the async methods called by an oapi-gen router.
Generated code stays unchanged. The integration reuses Dishka's native Starlette
request container, so operation handlers, security handlers, and ordinary Starlette
endpoints share the same Scope.REQUEST instances.
Use the same version of oapi-gen-dishka as the oapi-gen generator that produced
your application code. Both packages are published together; see
the release instructions.
uv add oapi-gen-dishka
This directory is also independently buildable. To install from a local checkout:
uv add /path/to/oapi-gen/packages/oapi-gen-dishka
Usage
The example uses the repository's complete Cats specification. Generate it into your application's package:
uv run oapi-gen generate packages/oapi-gen/tests/fixtures/cats.openapi.yaml --output app/http/generated
from contextlib import asynccontextmanager
from uuid import UUID, uuid4
from dishka import FromDishka, Provider, Scope, make_async_container, provide
from starlette.applications import Starlette
from oapi_gen_dishka import inject, setup_dishka
from app.http.generated import Handlers, contracts, create_router, models
class CatsRepository:
def __init__(self):
self.cats: dict[UUID, models.Cat] = {}
class CatsController:
@inject
async def list_cats(
self,
request: contracts.ListCats.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.ListCats.Response:
cats = list(repository.cats.values())
return contracts.ListCats.Ok(body=cats[: request.limit])
@inject
async def create_cat(
self,
request: contracts.CreateCat.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.CreateCat.Response:
cat = models.Cat(id=uuid4(), name=request.body.name)
repository.cats[cat.id] = cat
return contracts.CreateCat.Created()
@inject
async def show_cat_by_id(
self,
request: contracts.ShowCatById.Request,
*,
repository: FromDishka[CatsRepository],
) -> contracts.ShowCatById.Response:
cat = repository.cats.get(request.cat_id)
if cat is None:
raise contracts.ShowCatById.NotFound(
body=models.HttpError(message="Cat not found"),
)
return contracts.ShowCatById.Ok(body=cat)
class AppProvider(Provider):
# This in-memory example keeps cats between requests in one process.
repository = provide(CatsRepository, scope=Scope.APP)
container = make_async_container(AppProvider())
@asynccontextmanager
async def lifespan(app: Starlette):
try:
yield
finally:
await container.close()
router = create_router(Handlers(cats=CatsController()), prefix="/api")
app = Starlette(lifespan=lifespan, routes=router.routes)
setup_dishka(container, app)
Use Scope.REQUEST for services or database sessions that must be recreated for
each request. Dishka resolves dependencies from the normal provider graph and
finalizes generator providers at the end of the request, including on exceptions.
Add Dishka's StarletteProvider() from dishka.integrations.starlette when a
provider needs starlette.requests.Request.
@inject also works on generated security handler methods. Use
dishka.integrations.starlette.inject on ordinary Starlette endpoints; both
decorators resolve from the same container after the single setup call above.
Boundaries
-
Async HTTP handlers only. WebSockets, sync containers, and background work outside the request lifetime are not supported by this decorator.
-
Call this package's
setup_dishkaonce, before startup, instead of callingdishka.integrations.starlette.setup_dishkaseparately. -
The controller instance passed to
Handlersis shared. Keep request-specific state in local variables and injected dependencies, not onself. -
Providers must register dependencies;
FromDishka[T]selects the registered type and does not register it automatically. Dishka components are supported. -
Runtime signatures omit injected parameters and retain the remaining annotations. The decorator preserves the static response type but uses
Callable[..., ...]for arguments, since Python typing cannot remove arbitraryFromDishkaparameters. Annotate controller references with the generated protocol when calling them directly to check request argument types:controller: contracts.CatsHandler = CatsController()
Contributing
See CONTRIBUTING.md for workspace setup, package checks, and releases.
Metadata
Release files for oapi-gen-dishka 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| oapi_gen_dishka-0.1.11.tar.gz | 9.0 kB | With provenance | Details for oapi_gen_dishka-0.1.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| oapi_gen_dishka-0.1.11-py3-none-any.whl | Python 3 | none | any | Details for oapi_gen_dishka-0.1.11-py3-none-any.whl |
Total release size: 13.9 kB
Release files / oapi_gen_dishka-0.1.11.tar.gz
| Download URL | oapi_gen_dishka-0.1.11.tar.gz |
|---|---|
| Size | 9.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
633f0aeeb4ac58a424f19841af5811ccff4c0e9094e1372083eae2a95f81f792
|
|
BLAKE2b-256 checksum How to use checksums |
a3c46434b3013ee0b0bd5046806dfdd81f255694057ab13977e2ed2d61c0b930
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logRelease files / oapi_gen_dishka-0.1.11-py3-none-any.whl
| Download URL | oapi_gen_dishka-0.1.11-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
85cfd582c252ff97400673ddefe25f3a7238cfda98348532a7a1f4ed603f418b
|
|
BLAKE2b-256 checksum How to use checksums |
c5f03ac27696ad8edae66918fe6438afd1cfc862d9874186be86832ac394c859
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logSecurity for oapi-gen-dishka 0.1.11
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for oapi_gen_dishka-0.1.11.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 5, 2026.
Transparency logA 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.