Skip to content

Nested Model in request body producing only default values #4367

@david-saeger

Description

@david-saeger

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

from pydantic import BaseModel, Field
from typing import List, Optional
router = APIRouter()
from fastapi import APIRouter, Depends

class Contract(BaseModel):
    contract_uuid: str = Field(
        None,
        alias="CONTRACT_UUID",
,
    )
    contract_id: Optional[str] = Field(
        None,
        alias="CONTRACT_ID",

    )
    contract_name: Optional[str] = Field(
        None,
        alias="CONTRACT_NAME",
        title="Project/Contract Name",

    )
   
    full_part_time: Optional[EmploymentType] = Field(
        None,
        alias="FULL_PART_TIME",
        title="Employment Type",
 
    )


class UpdateContracts(BaseModel):
    contract_array: List[Contract] = Field(
        [], description="A list of contracts submitted by the employer"
    )
    case_id: str = Field(
        None,
        title="Case ID",
      
    )
    company_id: str = Field(
        None,
        title="Company Identifier",

    )


@router.put("/contracts/")
async def put_contracts(data: UpdateContracts):
    """ Update contracts for one applicant """
    print(data.json())

Description

I am building an API that returns a nested model UpdateContracts which contains some fields and a list of contracts. The fields show up fine but in the nested list of Contracts I can only produce the default null values specified in the Contract model. I can validate that the appropriately shaped data is being sent, here is the payload (it contains some extrainious fields but that should not matter):

from fastapi import APIRouter, Body, Depends
{"company_id":"pae","nvc_siv_case_id":"Ip0XkAZc","contract_array":[{"contract_id":"flomo","contract_name":"asdf","badge_id":"12345","job_location":null,"full_part_time":"FULLTIME","reason_for_separation":"currently_employed","employment_details":null,"employment_details_other":null,"contract_uuid":"909a1d12-43fe-42d9-ba0d-56c74aa167d2"}]}

Operating System

macOS

Operating System Details

No response

FastAPI Version

fastapi==0.61.1

Python Version

3.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions