Fastapi response returning 422 - Error: Unprocessable Entity, wrong model picked for validation

43 views Asked by At

I am building a custom project using fastapi. I am using pydantic models for defining inputs to the api. The data is stored in MongoDB.

I am getting 422 response code: Unprocessable Entity for a PUT api. I have not missed any values and moreover the fields mentioned are not present in the model defined.

Below are the models in my app -

class AttributeUpdateSpecific(BaseModel):
    scriptname: str
    attribute: str = Field(default="THREADS", description="Accepted values - THREADS, INPUT_FILE, THROUGHPUT, DURATION, JMX")
    value: str

class UpdateAttributeMultiple(BaseModel):
    update: list[AttributeUpdateSpecific]

class Script(BaseModel):
    environment: str
    component: str
    scriptname: str
    scriptdesc: str
    group: list[str]
    scriptstatus: str = Field(default="DONE")
    runid: int
    jmx: str
    variables: dict = Field(default=defaultvariables)

Route:

@script_router.put("/attributes")
async def update_individual_attributes(records: UpdateAttributeMultiple, test_db: str = Header(default=None)):
    updatecount = 0
    records = records.model_dump()["update"]

    if not test_db:
        raise ex.empty_header_exception()
    
    async for record in records:
        if record["attribute"] not in ["THREADS", "DURATION", "THROUGHPUT", "INPUT_FILE", "JMX"]:
            raise ex.invalid_option_exception()
        try:
            updatecount += await sch.script_update_attribute(test_db, record)

            return {
                "status": "success",
                "description": str(updatecount) + " record(s) updated."
            }
        
        except ValueError:
            raise ex.value_exception()
        except Exception as exception:
            raise ex.internal_server_exception(exception)

Request:

curl -X 'PUT' \
  'http://127.0.0.1:8000/scripts/attributes' \
  -H 'accept: application/json' \
  -H 'test-db: pcm_hpt_master' \
  -H 'Content-Type: application/json' \
  -d '{
  "update": [
    {
      "scriptname": "HL_TESTSCRIPT_CO_TEST",
      "attribute": "THREADS",
      "value": "150"
    }
  ]
}'

422: Unprocessable Entity - Response

{
  "detail": [
    {
      "type": "missing",
      "loc": [
        "body",
        "environment"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "component"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "scriptdesc"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "group"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "runid"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    },
    {
      "type": "missing",
      "loc": [
        "body",
        "jmx"
      ],
      "msg": "Field required",
      "input": {
        "update": [
          {
            "scriptname": "HL_TESTSCRIPT_CO_TEST",
            "attribute": "THREADS",
            "value": "150"
          }
        ]
      },
      "url": "https://errors.pydantic.dev/2.5/v/missing"
    }
  ]
}

The fields mentioned as missing are not part of the model defined (UpdateAttributeMultiple), but of another model used for a different request - Script. What might be going wrong here?

0

There are 0 answers