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.

[Feature]Can't support list type swagger response

See original GitHub issue

like:

class Item(BaseModel):
    name: str
    price: float


@app.route("/", method=["POST"])
@api.validate(json=Item, resp=Response(HTTP_200=List[Item]), tags=["demo"])
def demo():
    item = Item.parse_obj(request.context.json)
    return [item, item, item]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
kemingycommented, Dec 14, 2020

One possible way is to use __root__ pydantic root.

class Item(BaseModel):
    name: str
    price: float


class ItemList(BaseModel):
    __root__: List[Item]


@app.route("/", method=["POST"])
@api.validate(json=Item, resp=Response(HTTP_200=ItemList), tags=["demo"])
def demo():
    item = request.context.json
    return ItemList.parse_obj([item, item, item])

By the way, you don’t need to parse request.context.json again. For the return statement, you can use return ItemList.parse_obj([item, item, item]) or return ItemList(__root__=[item, item, item]).

2reactions
onecrayoncommented, Jul 4, 2021

Has anyone undertaken this issue yet? It’s something I need and I was wondering if I should take a swing at it or if it’s already underway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Describing Responses - Swagger
An API can respond with various media types. JSON is the most common format for data exchange, but not the only one possible....
Read more >
Describing Responses - Swagger
To specify the response media types, use the produces keyword on the root level or operation level. The global list can be overridden...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
Primitive data types in the OAS are based on the types supported by the JSON ... responses: '200': description: A list of pets....
Read more >
Data Types - Swagger
The data type of a schema is defined by the type keyword, for example, type: string . OpenAPI defines the following basic types:....
Read more >
Media Types - Swagger
Media Types. Media type is a format of a request or response body data. Web service operations can accept and return data in...
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