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.

Catching routes based on path parameter types

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

class SubType(Enum):
    thriller = "thriller"
    horror = "horror"

@router.get("/{id}-{subType}")
async def article_func_a(id: int, subType: SubType):
    return "show article with subtype"

@router.get("/{id}-{subId}")
async def article_func_b(id: int, subId: int):
    return "show article with subId"

Description

TL;DR

Is there a way to perform routing based on the data types of the path parameters? Instead of just the pattern of the path?

Explanation

Hi,

I am designing an application which will have two different types of routes:

  1. {id}-{subType}
  • Such as /1129-horror
  1. {id}-{subId}
  • Such as /1129-32

When I use the code above, the first given route catches both types of URLs, and throws validation error. Because routing is based on the parameter, and doesn’t care about the type of those parameters.

I am currently porting a Flask application, and it is doable in Flask by specifying types in routes such as: /<int:id>-<string:subType>. But apparently this isn’t possible in FastAPI.

I can convert enums into saperate routes, but I have dozens of subTypes, and that won’t be the most optimal solution. Especially when I am generating OpenAPI schema.

Operating System

Linux, Windows, macOS

Operating System Details

No response

FastAPI Version

0.79.0

Python Version

3.10.0

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JarroVGITcommented, Jul 16, 2022

My solution is not a workaround, it is the proper implementation of your requirements. But to answer your question; yes it results in the correct schema (otherwise it would be a workaround). You can try and see for your self in the generated /docs:

image image
1reaction
AliFluxcommented, Jul 26, 2022

Thanks for the solution. It really works 🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parameterised Routes • Angular - codecraft.tv
With parameterised routes we can support variable paths in our routes. Angular also supports optional routes via passing in an object to the...
Read more >
How to capture arbitrary paths at one route in FastAPI?
Since FastAPI is based on Starlette, you can use what they call "converters" with your route parameters, using type path in this case, ......
Read more >
Working with routes for HTTP APIs - Amazon API Gateway
Routes direct incoming API requests to backend resources. Routes consist of two parts: an HTTP method and a resource path—for example, GET /pets...
Read more >
How To Pass Multiple Route Parameters in a React URL Path
Route params are parameters whose values are set dynamically in a page's URL. This allows a route to render the same component while...
Read more >
The Art of Routing in Flask - Hackers and Slackers
Dynamic Routes & Variable Rules ... Static route URLs can only get us so far, as modern-day web applications are rarely straightforward. Let's...
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