Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

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

Open
#259 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start with the generated AccessControl model and trace how a model is returned in a ProgressEvent through Python's JSON serialization. Reproduce the failure with the shown uniqueItems schema, then verify that returning a model containing AbstractSet values serializes successfully without manual set-to-list conversions.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
107
Forks
46
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aws-cloudformation/cloudformation-cli-python-plugin

All issues in aws-cloudformation/cloudformation-cli-python-plugin

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.