JSON schema arrays with uniqueItems=True creates a model using Python sets which are not serializable by default

オープン
#259 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
devtools

調査の方向性

生成された AccessControl モデルから始め、Python の JSON シリアライゼーションを通じてモデルが ProgressEvent 内で返される流れを追跡します。示されている uniqueItems スキーマで失敗を再現し、AbstractSet 値を含むモデルを返した場合に、手動で set-to-list 変換を行わなくても正常にシリアライズされることを確認します。

索引モデルが issue の本文から書いたものです。

説明

When an definition in the JSON schema is an array with uniqueItems set to True, CFN generates a model with that attribute typed as an AbstractSet (or Python set in real terms) to guarantee that the items are unique. However the JSON module by default does not serialize sets and when the model is returned from the handler it raises an exception when trying to do so.

In order for the model to be returned I need to convert every set to a list in the model so it can be serialized back into JSON.

For example JSON Schema with the following definition:

"AccessControl": {
    "type": "object",
    "properties": {
        "Producers": {
            "type": "array",
            "uniqueItems": true,
            "insertionOrder": false,
            "items": { "type": "string" }  
        },
        "Consumers": {
            "type": "array",
            "uniqueItems": true,
            "insertionOrder": false,
            "items": { "type": "string" }  
        }
    },
    "required": [ "Producers", "Consumers" ],
    "additionalProperties": false
}

Generates a model with the following class

@dataclass
class AccessControl(BaseModel):
    Producers: Optional[AbstractSet[str]]
    Consumers: Optional[AbstractSet[str]]

    @classmethod
    def _deserialize(
        cls: Type["_AccessControl"],
        json_data: Optional[Mapping[str, Any]],
    ) -> Optional["_AccessControl"]:
        if not json_data:
            return None
        return cls(
            Producers=set_or_none(json_data.get("Producers")),
            Consumers=set_or_none(json_data.get("Consumers")),
        )

If the incoming model to the handler is populated and returned in a ProgressEvent the handler fails with an exception trying to serialize a set which it cant do by default. In order to return the model I have to convert every set to a list ie model.AccessControl.Producers = list(model.AccessControl.Producers). This can be tedious if there are multiple exit points in the handler and so should be handled by the model or by adding a set serializer to JSON.

主要言語
Python
スター
107
フォーク
46
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aws-cloudformation/cloudformation-cli-python-plugin のほかの issue

aws-cloudformation/cloudformation-cli-python-plugin の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。