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.

[BUG] openapi.json fails to be generated for nested models

See original GitHub issue

Describe the bug Fails to autogenerate docs.

To Reproduce

  1. Create a response model with other nested models and try to generate openapi.json or open autogenerated docs.
  2. Application throws an error

Expected behavior Generate openapi.json and open autogenerated docs without problems.

Environment:

  • OS: macOS
  • FastAPI Version: 0.30.0
  • Python version: 3.7.3

Additional context

My models look like this:

class PaginatedItems(Generic[Item], BaseModel, abc.ABC):
    items: List[Any]
    has_after: bool = False
    has_before: bool = False

class OrderUser(BaseModel):
    name: Optional[str] = None
    email: EmailStr


class Order(BaseModel):
    order_id: UUID
    amount: Decimal
    currency: str

    # Users part
    merchants: List[OrderUser]
    handlers: List[OrderUser]

class PaginatedOrders(PaginatedItems[Order]):
    items: List[Order] = list()

My route definition looks like that:

@router.get(orders_uri,
            response_model=PaginatedOrders
async def list():
	...

This results in the following exception when trying to open autogenerated docs:

pydantic.error_wrappers.ValidationError: 4 validation errors
schemas -> OrderUser
  value is not a valid dict (type=type_error.dict)
schemas -> OrderUser
  value is not a valid dict (type=type_error.dict)
components -> schemas
  value is not none (type=type_error.none.allowed)
components
  value is not none (type=type_error.none.allowed)

When I comment out the response_model=PaginatedOrders part from route definition everything works, but docs obviously miss response type.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
euri10commented, Jul 23, 2019

oups sorry I think your mistake is putting response_model=SimilarProducts, in the wrong spot, it’s in the @router part

2reactions
LKaycommented, Jul 15, 2019

I can confirm it now works fine! Thanks for fixing it so quickly!

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenApi required property in nested objects not working
I need to describe an api having in request body an object with required fields and one of these fields it's an object...
Read more >
Usage - OpenAPI Generator
The batch command allows you to move all CLI arguments supported by the generate command into a YAML or JSON file. NOTE: This...
Read more >
Unrecognized field, not marked as ignorable - JSON Parsing ...
This error was occurring because my nested class Person was not static, which means it cannot be instantiated because of having any Outer...
Read more >
Additional Responses in OpenAPI - FastAPI - tiangolo
FastAPI will take that model, generate its JSON Schema and include it in the correct place in OpenAPI. For example, to declare another...
Read more >
Components Section - Swagger
OAS 3 This guide is for OpenAPI 3.0. ... description: An error occurred. content: application/json: schema: $ref: '#/components/schemas/Error' ...
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