Skip to content

Conversation

xiwhuang
Copy link
Member

@xiwhuang xiwhuang commented Apr 3, 2020

Issue Description

When a sub-property uses consecutive capitalized string as name in schema, it will trigger an unexpected error in handling request while validating template against the schema. Test template and schema have attached

Error displays on CloudFormation console

Model validation failed (#/VPCs/0: 2 schema violations found) #/VPCs/0: extraneous key [vpcregion] is not permitted (#/VPCs/0) #/VPCs/0: extraneous key [vpcid] is not permitted (#/VPCs/0)

Root cause

Same root cause as PR#235. Duplicated sub-properties being generated unexpectedly. Since additional properties in a sub-property are disallowed as well, then it will occur errors as above.

{@link software.amazon.cloudformation.resource.Serializer#serialize} invoked by {@link software.amazon.cloudformation.LambdaWrapper#validateModel}

Unit test on Serializer & Validator

@Test
public void testModel() throws JsonProcessingException {
    ResourceModel model = ResourceModel.builder()
            .tPSCode("TPSCode")
            .vPCs(Arrays.asList(VPC.builder().vPCId("id").vPCRegion("region").build()))
            .build();
    Serializer serializer = new Serializer();
    final String jsonString = serializer.serialize(model);

    System.out.println(jsonString);

    Validator validator = new Validator();
    validator.validateObject(new JSONObject(jsonString),
            new JSONObject(new JSONTokener(this.getClass().getResourceAsStream("/test-duplicate-cases.json"))));
}

Output:

{"TPSCode":"TPSCode","VPCs":[{"vpcid":"id","vpcregion":"region","VPCId":"id","VPCRegion":"region"}]}

software.amazon.cloudformation.resource.exceptions.ValidationException: #/VPCs/0: 2 schema violations found

Fix

Turn off @JsonAutoDetect on getters and setters to only use the fields as the canonical source of serialization config.

Since SubProperties are generated by POJO.java template. Ref: codegen.py

By adding the below annotation before POJO Class template would fix the issue.

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

After Fix

  • Unit test output displays as expected.
    {"TPSCode":"TPSCode","VPCs":[{"VPCId":"id","VPCRegion":"region"}]}
    
    
    Process finished with exit code 0
    
  • Manual test against CloudFormation has passed as well.

Test Schema

{
    "typeName": "Test::Duplicate::Cases",
    "description": "An example resource schema demonstrating some basic constructs and validation rules.",
    "sourceUrl": "https://github.com/aws-cloudformation/aws-cloudformation-rpdk.git",
    "definitions": {
        "VPC": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "VPCId": {
                    "type": "string"
                },
                "VPCRegion": {
                    "type": "string"
                }
            },
            "required": [
                "VPCId",
                "VPCRegion"
            ]
        }
    },
    "properties": {
        "TPSCode": {
            "type": "string"
        },
        "VPCs": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/VPC"
            }
        }
    },
    "additionalProperties": false,
    "readOnlyProperties": [
        "/properties/TPSCode"
    ],
    "primaryIdentifier": [
        "/properties/TPSCode"
    ],
    "handlers": {
        "create": {
            "permissions": [
                "initech:CreateReport"
            ]
        },
        "read": {
            "permissions": [
                "initech:DescribeReport"
            ]
        },
        "update": {
            "permissions": [
                "initech:UpdateReport"
            ]
        },
        "delete": {
            "permissions": [
                "initech:DeleteReport"
            ]
        },
        "list": {
            "permissions": [
                "initech:ListReports"
            ]
        }
    }
}

Test template

Resources:
  DuplicateTest:
    Type: Test::Duplicate::Cases
    Properties:
      TPSCode: test
      VPCs : 
        - VPCId: id
          VPCRegion: region

@xiwhuang xiwhuang added bug Something isn't working codegen It's like magic! and removed codegen It's like magic! labels Apr 3, 2020
@xiwhuang xiwhuang self-assigned this Apr 7, 2020
Copy link

@wbingli wbingli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wbingli wbingli merged commit ddfbcbf into aws-cloudformation:master Apr 8, 2020
@xiwhuang xiwhuang deleted the quick-fix branch April 8, 2020 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants