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.

routes appear under tag of Apirouter and parent Apirouter

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.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here’s a self-contained, minimal, reproducible, example with my use case:

import uvicorn
from fastapi import FastAPI, APIRouter

items = {}
app = FastAPI()

router = APIRouter(prefix="/api")
items_router = APIRouter(prefix="/items", tags=["items"])

@items_router.post("/{id}")
def add_item(id: str):
	items[id] = {"attachments": []}
	return items[id]

@items_router.get("/{id}")
def get_item(id: str):
	if not (item := items.get(id)):
		return "error"
	return item

items_attachment_router = APIRouter(prefix="/{id}", tags=["attachment"])

@items_attachment_router.post("/add")
def add_attachment(id: str, attachment: str):
	if not (item := items.get(id)):
		return "error"
	item["attachments"].append(attachment)
	return item

items_attachment_router2 = APIRouter()

@items_attachment_router2.post("/{id}/add2")
def add_attachment2(id: str, attachment: str):
	if not (item := items.get(id)):
		return "error"
	item["attachments"].append(attachment)
	return item

items_router.include_router(items_attachment_router)
items_router.include_router(items_attachment_router2, tags=["attachment2"])
router.include_router(items_router)
app.include_router(router)

if __name__ == "__main__":
	uvicorn.run(app)

Description

the routes of an APIRouter appear under the tags given by the router and all parent routers. I implemented it the old and the new way (adding tags to the router directly & adding them as a param in include_router). Both methods put the routes add_attachment and add_attachment2 under their own tag and them items tag

  • Python version: 3.8

Additional context

FastAPI version 0.62.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
ajju10commented, Dec 9, 2020

Please describe the issue clearly or better to share a screenshot of the page you are getting issue with along with the clean description of your issue.

1reaction
transfluxuscommented, Dec 14, 2020

Hi,

sorry, the issue is that the routes appear twice (in swagger)

https://imagebin.ca/v/5kgpOV3O3InG

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bigger Applications - Multiple Files - FastAPI
You can think of APIRouter as a "mini FastAPI " class. All the same options are supported. All the same parameters , responses...
Read more >
Routers in FastAPI - Medium
Let's understand how to create routers with code, below is our basic(non-router-ish) code, here for an example I have created : homepage, add-numbers...
Read more >
FastAPI with APIRouter plugin system not working
When I run the server, the plugins are loaded and log their information, but the API endpoints are not loaded. What I'm missing...
Read more >
RouterOutlet - Angular
Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive. The router keeps track of separate branches...
Read more >
14 : Our First FastAPI Route ! - FastapiTutorial
Now, we need to type the below lines in apis > version1 > route_users.py. Copy from fastapi import APIRouter from sqlalchemy.orm import Session...
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