如何修复 - UserWarning:Pydantic 序列化器警告?

问题描述 投票:0回答:1

这些警告的含义是什么以及如何解决它们?它会产生什么样的问题?

用户警告:

  Expected `Union[list[definition-ref], definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
  Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
  Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
  Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
  return self.__pydantic_serializer__.to_python(

重现:

pets.json

{
    "pets": [
      {
        "name": "dog",
        "age": 2
      },
      {
        "name": "cat",
        "age": 1
      },
      {
        "name": "snake",
        "age": 3,
        "nickname": "python"
      }
    ],
    "status": 200
  }

命令:

datamodel-codegen  --input pets.json --input-file-type json --output model.py

版本:

python - 3.11.4
pydantic==2.1.1
datamodel-code-generator==0.21.4
genson==1.2.2
python code-generation pydantic genson
1个回答
0
投票

要知道到底出了什么问题,您应该提供解析该对象的 pydantic 模型。

© www.soinside.com 2019 - 2024. All rights reserved.