aws-cloudformation / aws-cloudformation/cloudformation-cli-python-plugin

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

未关闭
#259 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Python
星标
107
派生
46
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。