Skip to content

Request: Support List[pydantic.BaseModel] as response #56

Closed

Description

@guneemwelloeux

Environment:

  • Python version: 3.9.13
  • Operating system: MacOS Monterey 12.4
  • Flask version: 2.1.3
  • flask-openapi3 version: 2.2.2

Request

Add support of List[] in responses.

Explanation

I'm trying to migrate an existing Flask API to Flask-OpenAPI, and I'm hitting an issue.

  • Most of my endpoints return a JSON object, with the proper content type. This is compatible with the responses parameter in the @app.get decorator ✅
  • Other endpoints return a simple string, with text content type. This is compatible with the extra_responses parameter ✅
  • However, I do have a number of endpoints that return a JSON encoded list of objects, with content type JSON. This is not compatible with responses.

Since this API is used in production:

  • I can't afford to wrap these lists in "dummy" object with a data key that would contain the list.
  • I also don't really want to have to write a special dictionary to use in extra_responses. I tried using pydantic's schema_of function inside extra_responses, but that's not compatible because of the use of definitions.

Note that pydantic.schema_of(List[<child of pydantic.BaseModel>]) does work.

Activity

  1. luolingchun commented on Feb 3, 2023

    @luolingchun
    Owner

    I think __root__ can solve your problem:

    class ResponseWithList(BaseModel):
        __root__: List[Any]

    More information see https://docs.pydantic.dev/usage/models/#custom-root-types

  2. guneemwelloeux commented on Feb 3, 2023

    @guneemwelloeux
    Author

    Hi @luolingchun,

    That's exactly what I needed. Thanks for pointing it out.
    Do you think it would be worth adding a few lines on this in the Flask-OpenAPI documentation?
    I understand it's not anything to do with Flask-openapi itself, but it might be useful to mention it.

    Thanks

  3. luolingchun commented on Feb 6, 2023

    @luolingchun
    Owner

    It's worth it because developers can understand your API more easily.

  4. added
    questionFurther information is requested
    and removed
    bugSomething isn't working
    on Feb 12, 2023
  5. Repository owner locked and limited conversation to collaborators on Feb 12, 2023
  6. converted this issue into a discussion #57 on Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions