pepkit / pepkit/pephub

Adding example request data to path parameters defined through dependency injection

Open
#20 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first issue help wanted likely solved question
Dominant language
TypeScript
Stars
22
Forks
2
PR merge metrics
No merged PRs in 30d

Description

Overview

As heavily discussed in #14. I can't seem to add route example request data to /docs when the path parameters are defined through dependency injection. I thought I'd pull it out to its own issue here to track that progress instead of leaving the PR in purgatory.

Issue

I guess I am having an issue declaring request data examples for specific endpoint/route parameters when said endpoint/route parameters are defined through dependency injection. In the below example, I need to verify that the namespace actually exists prior to returning data about it. To do that, I followed this example in the FastAPI docs. It works great.

However, in /docs, I'd love to give examples for namespaces. To try and do that, I followed this example in the FastAPI docs.

Visiting /docs, however, provides the user with no example request data:
image

If I comment out the global dependencies like so:

router = APIRouter(
    prefix="/pep/{namespace}",
    # dependencies=[Depends(verify_namespace)],
)

The example is shown. I can also verify that declaring requirements in the Path() instance like min_length or max_length, these requirements are honored by FastAPI (even though I can't see the requirements in /docs).

Code

# main.py
from fastapi import FastAPI, JSONResponse
from .routers import namespace

app = FastAPI()

app.include_router(
    namespace.router
)

# routers/namespace.py
from fastapi import APIRouter, Depends

from ..dependencies import *
from ..main import _PEP_STORES
from ..route_examples import example_namespace

router = APIRouter(
    prefix="/pep/{namespace}",
    dependencies=[Depends(verify_namespace)],
)

@router.get("/", summary="Fetch details about a particular namespace.")
async def get_namespace(namespace: str = example_namespace):
    """Fetch namespace. Returns a JSON representation of the namespace and the projects inside it."""
    return JSONResponse(content=_PEP_STORES[namespace])

# dependencies.py
from .main import _PEP_STORES

def verify_namespace(namespace: str = example_namespace) -> None:
    if namespace not in _PEP_STORES:
        raise HTTPException(status_code=404, detail=f"namespace '{namespace}' not found.")

# route_examples.py
from fastapi import Path

example_namespace = Path(
    ...,
    description="A namespace that holds projects.",
    example="demo",
)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the missing example from main.py using routers/namespace.py, dependencies.py, and route_examples.py, keeping the router dependency enabled. Start by inspecting the generated schema behind /docs and compare it with the version without the dependency. Done means the namespace example appears in /docs while dependency-based validation still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.