Incorrect handling of Union types
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
再現コード内のAPISpecおよびMarshmallowPluginのエントリーポイントから始め、Union型のitemsフィールドについて MainSchema.Schema() がどのように変換されるかに焦点を当ててください。提供されているPython例を実行し、list[Food | Drink] と Food | Drink の両方に対するカバレッジを追加してください。生成されたOpenAPIスキーマがitemsを空のままにせず、選択肢を表現していれば完了です。
索引モデルが issue の本文から書いたものです。
説明
It seems that the API spec is not properly generated when using Union typehints.
The below snippet is on python 3.10 and 3.11
from typing import Literal
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from marshmallow_dataclass import dataclass
class BaseItemMixin:
name: str
@dataclass
class BaseItem(BaseItemMixin):
pass
@dataclass
class Food(BaseItem):
label: str
name: Literal["food"] = "food"
@dataclass
class Drink(BaseItem):
size: int
name: Literal["drink"] = "drink"
@dataclass
class MainSchema:
id: int
name: str
items: list[Food | Drink]
spec = APISpec(
title="API",
version="1.0.0",
openapi_version="3.0.2",
plugins=[(MarshmallowPlugin())],
)
spec.path(
"/endpoint",
operations={
"post": {
"parameters": [
{
"name": "Args",
"schema": MainSchema.Schema(),
"in": "body",
}
],
"responses": {
"200": {
"content": {"application/json": {}},
"description": "OK",
},
},
}
},
)
if __name__ == "__main__":
print(spec.to_yaml())
The outputted yaml contains the schema for Main as:
components:
schemas:
Main:
type: object
properties:
name:
type: string
id:
type: integer
items:
type: array
items: {}
required:
- id
- items
- name
Items is correctly marked as an array, but the items is not correct. I'd expect oneOf being used here (although i'm not 100%).
The same happens using item: Food | Drink.
I believe the issue is within this library, and not marshmallow_dataclass, as i'm able to deserialise serialise with the schemas without issue
data = MainSchema(
id=1,
name="test",
items=[
Food(name="food", label="test"),
Drink(name="drink", size=1),
],
)
data_as_dict = MainSchema.Schema().dumps(data)
loaded_data = MainSchema.Schema().loads(data_as_dict)
print("original", data)
print("deserialised", loaded_data)
print("serialised", data_as_dict)
# original MainSchema(id=1, name='test', items=[Food(label='test', name='food'), Drink(size=1, name='drink')])
# deserialised MainSchema(id=1, name='test', items=[Food(label='test', name='food'), Drink(size=1, name='drink')])
# serialised {"id": 1, "items": [{"label": "test", "name": "food"}, {"size": 1, "name": "drink"}], "name": "test"}
Note: I've also tried without the inheritance involved, and the result is the same.
- 主要言語
- Python
- スター
- 1.2k
- フォーク
- 211
- 平均マージ
- 2日 1時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
marshmallow-code/apispec のほかの issue
-
DateTime(format="timestamp") generates invalid OpenAPI schema using min instead of minimum対応中かも @Sanjays2402 が 107 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
marshmallow-code/apispec#1064 ·
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
marshmallow-code/apispec#750 · コメント 1 件 · リアクション 5 件 ·
メンテナーはふだん 1 日以内に返信
-
Dependency Dashboardオープン
難易度 1/5 1時間未満 初心者へのやさしさ 30/100
marshmallow-code/apispec#1033 ·
メンテナーはふだん 1 日以内に返信
-
`apispec` ignores `oneOf` from `marshmallow-oneofschema`再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 38/100
marshmallow-code/apispec#1012 · コメント 6 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
marshmallow-code/apispec#981 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
marshmallow-code/apispec の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
Netcracker/qubership-ai-packages#266 ·
メンテナーはふだん 1 日以内に返信
-
PROCESSED_DATA_FOLDER: strip whitespace and treat empty as unset対応中かも @TorecLuik が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
NL-BioImaging/BIOMERO.importer#63 ·
メンテナーはふだん 1 日以内に返信
-
MLX training-complete chat/merge suggestions omit --backend mlx対応中かも @vaputa が今日担当しました。 オープンneeds-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
MakazhanAlpamys/Soup#2913 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area:security effort:S priority:P3 status:needs-triage type:chore
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
randomparity/kdive#3657 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
aws/mcp-proxy-for-aws#476 ·
メンテナーはふだん 1 日以内に返信