question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alias for path param does not work when hypens are used in it.

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI, Path

app = FastAPI()

@app.get("/{my-id}/list")
def list_items(
        my_id: str = Path(alias="my-id")
):
    return {"aa": my_id}

Description

On hitting

curl -X 'GET' \
  'http://localhost:8000/sample/list' \
  -H 'accept: application/json'

I get 404 error

{
  "detail": "Not Found"
}

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.79.0

Python Version

3.9.10

Additional Context

This behaviour is working fine with Query param alias but not with Path param alias

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JarroVGITcommented, Aug 18, 2022

This doesn’t make any sense to want; you are trying to create an invalid python variable name with a hyphen, which is never used outside the scope of python itself. Not like for example query parameters, where the name of the parameter is exposed to the end user in the URL.

Can I ask why you would want to do this?

0reactions
OH-ELYADATAcommented, Dec 18, 2022

@tiangolo any idea on why it isn’t working when using hyphens in path parameter aliases? is this planned to be fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Path(alias=) feature doesn't work if the alias has a hyphen in it
Path (alias=) feature doesn't work if the alias has a hyphen in it ... I used the GitHub search to find a similar...
Read more >
bash - Are there problems with hyphens in functions, aliases ...
A hyphen is not listed among the characters that must be allowed in either case. So, if they are used, portability is not...
Read more >
How to handle path parametes with hyphen(-) in a FastAPI ...
You can use alias in your definitions. It's all documented here. As stated in the comments below, if the links stops working here...
Read more >
Hyphens and forward slashes (-/) break Views contextual filters
Problem /Motivation Views contextual filters break when values contain forward slashes or when they contain hyphens and the "Transform spaces ...
Read more >
Use of Dash “-” in Command-Line Parameters - Baeldung
In this tutorial, we'll see what the “-” character is about and what purpose it serves. We'll take a look at the most...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found