我试图在该对象的 JSON 架构中允许 null:
from pydantic import BaseModel
from typing import Optional
class NextSong(BaseModel):
song_title: Optional[str] = ...
Run Code Online (Sandbox Code Playgroud)
但结果的模式如下:
{
"title": "NextSong",
"type": "object",
"properties": {
"song_title": {
"title": "Song Title",
"type": "string"
}
},
"required": ["song_title"]
}
Run Code Online (Sandbox Code Playgroud)
生成的架构不允许 Song_title 的值为 null,这不符合预期,但我不确定如何指定允许 null,但该字段仍然是必需的。
我认为您需要 OpenAPI 可空标志。它应该更改架构并设置可为空标志,但该字段仍然是必需的。
from pydantic import BaseModel, Field
from typing import Optional
class NextSong(BaseModel):
song_title: Optional[str] = Field(..., nullable=True)
Run Code Online (Sandbox Code Playgroud)
结果架构:
{
"title": "NextSong",
"type": "object",
"properties": {
"song_title": {
"title": "Song Title",
"nullable": true,
"type": "string"
}
},
"required": ["song_title"]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5871 次 |
| 最近记录: |