Skip to content

cfn generate references incorrect class name #96

Closed

Description

@richardhboyd

If I have the following resource specification

{
  "typeName": "Richard::Cloud9::Environment",
  "description": "An example resource schema demonstrating some basic constructs and validation rules.",
  "sourceUrl": "https://github.com/aws-cloudformation/aws-cloudformation-rpdk.git",
  "definitions": {
    "String": { "type": "string" },
    "PolicyStatement": {
      "type": "object",
      "properties": {
        "Effect": {"type": "string"},
        "Action": {"type": "array", "items": {"type": "string" }},
        "Resource": {"type": "array", "items": {"type": "string" }}
      }
    },
    "PolicyDocument": {
      "type": "object",
      "properties": {
        "Version": {"type": "string"},
        "Statement": {
          "type": "array",
          "items": { "$ref": "#/definitions/PolicyStatement" }
        }
      }          
    },
    "Cloud9InstancePolicy": {
      "$comment": "Policy to apply to Cloud9 Instance",
      "type": "object",
      "properties": {
        "PolicyName": {"type": "string"},
        "PolicyDocument": { "$ref": "#/definitions/PolicyDocument" }
      }
    }
  },
  "properties": {
    "InstanceId": {
      "description": "EC2 Instance Id used by the underlying EC2 instance.",
      "type": "string",
      "pattern": "^i-[a-z0-9]{17}$"
    },
    "Cloud9InstancePolicy": {
      "description": "Inline Policy to provide fine-grained IAM access control to the Cloud9 instance",
      "$ref": "#/definitions/Cloud9InstancePolicy"
    }
  },
  "additionalProperties": false,
  "required": [],
  "readOnlyProperties": [
      "/properties/InstanceId"
  ],
  "primaryIdentifier": [
      "/properties/InstanceId"
  ],
  "handlers": { ... }
}

cfn generate generates the following models.py

...

# work around possible type aliasing issues when variable has same name as a model
_Cloud9InstancePolicy = Cloud9InstancePolicy


@dataclass
class PolicyDocument(BaseModel):
    Version: Optional[str]
    Statement: Optional[Sequence["_PolicyStatement"]]

    @classmethod
    def _deserialize(
        cls: Type["_PolicyDocument"],
        json_data: Optional[Mapping[str, Any]],
    ) -> Optional["_PolicyDocument"]:
        if not json_data:
            return None
        return cls(
            Version=json_data.get("Version"),
            ##################################################
            Statement=deserialize_list(json_data.get("Statement"), Statement),  ##### This should be PolicyStatement
            ##################################################
        )


# work around possible type aliasing issues when variable has same name as a model
_PolicyDocument = PolicyDocument


@dataclass
class PolicyStatement(BaseModel):
    Effect: Optional[str]
    Action: Optional[Sequence[str]]
    Resource: Optional[Sequence[str]]

    @classmethod
    def _deserialize(
        cls: Type["_PolicyStatement"],
        json_data: Optional[Mapping[str, Any]],
    ) -> Optional["_PolicyStatement"]:
        if not json_data:
            return None
        return cls(
            Effect=json_data.get("Effect"),
            Action=json_data.get("Action"),
            Resource=json_data.get("Resource"),
        )


# work around possible type aliasing issues when variable has same name as a model
_PolicyStatement = PolicyStatement

Activity

  1. linked a pull request that will close this issuefixed resource reference #98on Jun 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions