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.

Path parameter with List

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

@app.get(
    V4L2_ROUTE + "controls/{ctrl_list}",
    response_model=List[ControlValue],
    tags=[Tags.controls],
)
def get_controls(
    ctrl_list: List[str],
    params: CommonParams = Depends(),
) -> JSONResponse:
    """Get v4l2 controls"""
    return Camera.get_controls(ctrl_list, params.dev)

Description

When trying to declare a route with a Path parameter which is a List I get the following error: AssertionError: Path params must be of one of the supported types

But from what I can see it should be possible to have Lists in the Path: https://stackoverflow.com/questions/2602043/rest-api-best-practice-how-to-accept-list-of-parameter-values-as-input

Fx: http://our.api.com/Product/101404,7267261

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.85.0

Python Version

Python 3.10.5

Additional Context

No response

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
JarroVGITcommented, Oct 17, 2022

A Path param is always a int or a str. However, you could simply do the conversion yourself. The below example shows how to deal with a list of values like ‘int,int,int’ etc. Using pydantic, I will also perform basic validation, yay!

from fastapi import Depends, FastAPI, HTTPException, Path
from pydantic import BaseModel, ValidationError

app = FastAPI()


class ListOfIds(BaseModel):
    ids: list[int]


def list_path_param(list_of_ids: str = Path()):
    try:
        ids = ListOfIds(ids=list_of_ids.split(","))
    except ValidationError:
        raise HTTPException(400, "IDs must be valid integers.")
    return ids


@app.get("/controls/{list_of_ids}")
async def get_list_of_ids(list_obj: ListOfIds = Depends(list_path_param)):
    return {"count": len(list_obj.ids), "ids": list_obj.ids}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8000)

This will take the str and will split it (resulting in a list), and construct a Pydantic model with its parts (thus performing validation). The above will run as is.

0reactions
tiangolocommented, Oct 20, 2022

Ah, get it, great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Store @PathParam values from REST call in a list or array
Is it possible to store them in an array or list, so I do not list dozens of @PathParams and parameters, to avoid...
Read more >
Spring — Passing list and array of values as URL parameters
Let's see how to send a list of query parameters, and receive them in array/arraylist in Spring. Sending via URL: List of values...
Read more >
What are Path Parameters? Technical topics explained simply
Path parameters are request parameters attached to a URL that point to a specific REST API resource. The path parameter is separated from ......
Read more >
Path Parameters - FastAPI
You can declare path "parameters" or "variables" with the same syntax used by Python format strings: from fastapi import FastAPI app = FastAPI() ......
Read more >
@PathParam | RESTful Java with JAX-RS 2.0 (Second Edition)
@PathParam allows you to inject the value of named URI path parameters that were defined in @Path ... In these cases, you can...
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