Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for null members in structures, lists and maps #2393

Merged
merged 4 commits into from
Sep 9, 2024

Conversation

maximk000
Copy link
Contributor

@maximk000 maximk000 commented Sep 4, 2024

Background

Previously, a NullNode could be provided as a required structure member without emitting a ValidationEvent. So an example test below would fail.

    public void nullRequiredStructureMember() {
        ObjectNode structure = ObjectNode.builder()
                .withMember("foo", Node.nullNode())
                .build();
        NodeValidationVisitor visitor = NodeValidationVisitor.builder()
                .value(structure)
                .model(MODEL)
                .allowOptionalNull(true)
                .build();
        List<ValidationEvent> events = MODEL
                .expectShape(ShapeId.from("ns.foo#Structure"))
                .accept(visitor);

        assertThat(events, hasSize(1));
        assertThat(events.get(0).getMessage(), containsString(
                "foo: Required member `foo` for structure ns.foo#Structure for cannot be null"));
    }

ns.foo#Structure schema:
"ns.foo#Structure": {
    "type": "structure",
    "members": {
        "foo": {
            "target": "ns.foo#String",
            "traits": {
                "smithy.api#required": {}
            }
        },
...

This happens because the memberShape function would check the member's target instead of itself, which always satisfies the !shape.isMemberShape() condition in the invalidShape function.

To fix this I added a new function to check null member nodes for structures, lists and maps. It relies on the isMemberNullable function, which checks that:

  • If a structure member, member cannot be required
  • If a list member, the list must have the sparse trait
  • If a map value, the map must have the sparse trait (this case is not explicit but happens due to a fall through to the default case)

Testing

Added testcases:

  • One that emits ValidationEvent when NullNode is provided as a required structure member
  • One that emits ValidationEvent when NullNode is provided as member for a non-sparse list
  • One that doesn't emit ValidationEvent when NullNode is provided as member for a sparse list
  • One that emits ValidationEvent when NullNode is provided as a value for a non-sparse map
  • One that doesn't emit ValidationEvent when NullNode is provided as a value for a sparse map

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@maximk000 maximk000 requested a review from a team as a code owner September 4, 2024 22:32
Copy link
Contributor

@kstich kstich left a comment

Choose a reason for hiding this comment

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

Some comments on event message formatting (that will also require test updates) and a simplification of the conditional check.

maximk000 and others added 3 commits September 5, 2024 12:51
Simplify conditional check, return immediately for each case, change msg formatting

Co-authored-by: Kevin Stich <kevin@kstich.com>
Remove unnecessary breaks
@kstich kstich merged commit 8418149 into smithy-lang:main Sep 9, 2024
13 checks passed
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants