如何修复 - UserWarning:Pydantic V2 中的 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
投票

当存在循环嵌套模型时,我遇到了类似的问题。如果这是您的问题,您需要致电:

YourSchemaContainingNestedModel.model_rebuild()

声明模型后。

但不确定这是否是您的情况,因为我看不到您正在使用的模式。

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