Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Nested Routes Support and Documentation #20

Open
igorbenav opened this issue Feb 24, 2024 · 4 comments
Open

Add Nested Routes Support and Documentation #20

igorbenav opened this issue Feb 24, 2024 · 4 comments
Labels
Automatic Endpoint Related to automatic endpoint creation functionality documentation Improvements or additions to documentation enhancement New feature or request

Comments

@igorbenav
Copy link
Owner

Describe the bug or question
Handling something like this scenario:

router = APIRouter(prefix="/user/{user_id}")


@router.get("/posts/{post_id}")
def get_post(user_id: int, post_id: int):
    ...
    return {"user": user_id, "post:": post_id, "text": post_text}
curl localhost:8000/user/1/posts/2

{
  "user": 1,
  "post:": 2
  "text": "sample text"
}
@igorbenav igorbenav added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 24, 2024
@igorbenav igorbenav added the Automatic Endpoint Related to automatic endpoint creation functionality label Apr 6, 2024
@JakNowy
Copy link
Contributor

JakNowy commented Apr 24, 2024

I tried addressing this. After short research I thought that something like this would work out of the box:

class UserCRUD(fastcrud.FastCRUD):
    async def read(self, db: AsyncSession, root_param: int, read_param: int) -> Any:
        return {
            'root_param': root_param,
            'read_param': read_param,
        }

user_router = crud_router(
    crud=UserCRUD(User),
    model=User,
    session=deps.get_async_db,
    create_schema=UserIn,
    update_schema=UserUpdate,
    endpoint_names={'read': 'read/{read_param}'}
)

my_router = APIRouter()
my_router.include_router(user_router, prefix='root_router/{root_param}')

It turns out, that while the route gets properly built, the path params are not resolved dynamically as integers (they are parsed as raw path strings). Only default id gets recognized as a path param. I can see they are all gettin passed in

            self.router.add_api_route(
                f"{self.path}/{endpoint_name}/{_primary_keys_path_suffix}",
                ...
            )

but not sure what magic makes the _primary_keys_path_suffix resolve the params but not endpoint_name. Any thoughts on that? Is that approach ok or we want something else?

image

(note I have one more layer of /user/ nesting in my project but that's irrelevant here)

@JakNowy
Copy link
Contributor

JakNowy commented Apr 25, 2024

@igorbenav

@igorbenav
Copy link
Owner Author

I'll take a proper look at it later today

@igorbenav
Copy link
Owner Author

Sorry, had a really long day. I'll try to do it on friday

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Automatic Endpoint Related to automatic endpoint creation functionality documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants