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

Enhancement: Allow body parameter to have an arbitrary name. #3053

Open
Liraim opened this issue Jan 31, 2024 · 6 comments
Open

Enhancement: Allow body parameter to have an arbitrary name. #3053

Liraim opened this issue Jan 31, 2024 · 6 comments
Labels
3.x This is related to Litestar version 3 Breaking 🔨

Comments

@Liraim
Copy link

Liraim commented Jan 31, 2024

Summary

When you write a handler it is more convenient to have a body parameter named as entity you work with.

Basic Example

Before you required to write live:

@post
async def post_todo_item(data: TodoItem) -> None:
    ...

After, you CAN to annotate it with Body and have an arbitrary name.

@post()
async def post_todo_item(todo_item: Annotated[TodoItem, Body()]) -> None:
    ...

Drawbacks and Impact

  • More complicated handler logic to implement
  • New name can overlap with dependency name and break dependencies resolution.

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@Liraim Liraim added the Enhancement This is a new feature or request label Jan 31, 2024
@cofin
Copy link
Member

cofin commented Feb 23, 2024

I've guessing this should also apply to other reserved parameters such as state and query?

@provinzkraut
Copy link
Member

Yeah. I think we should do this for all injected things.

E.g.

async def handler(
  param: Query[str], 
  request_body: Body[list[str]], 
  header: Header[int],
):
 ...

IMO this belongs to the DI redesign and is something I'd definitely want to address as part of that.

@peterschutt
Copy link
Contributor

Agree - it would be great to be more flexible in this respect!

@tuukkamustonen
Copy link
Contributor

Just want to highlight how nice it would be to split Parameter into more specific types, ie. Body, Query, Header, Cookie, Path(?). Current mechanism of having to thing: Annotated[int, Parameter(header="thing")] vs thing: Annotated[int, Header()]) feels a bit bloated.

Also discussed in #3223 (comment) & #3223 (comment). FYI.

@provinzkraut
Copy link
Member

@tuukkamustonen That's already in the current proposal, see #3053 (comment)

@tuukkamustonen
Copy link
Contributor

Yeah, just wanted to highlight how nice it would be to have those types. The topic of the ticket is "allow ... parameter to have an arbitrary name" (and not add Query, Body, etc convenience wrappers for shorter field name syntax...) after all.

But yeah, good that you have it scoped like that 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x This is related to Litestar version 3 Breaking 🔨
Projects
Status: Backlog
Development

No branches or pull requests

6 participants