From 6ff5765557a3dd203960a7320d2d7f04d0de6ed3 Mon Sep 17 00:00:00 2001 From: Jeff Lewis Date: Tue, 17 Jun 2025 10:55:04 -0600 Subject: [PATCH 1/3] fix node null check for maps/lists --- .../smithy/model/knowledge/NullableIndex.java | 5 +++-- .../validation/NodeValidationVisitorTest.java | 18 ++++++++++++++++++ .../model/validation/node-validator.json | 19 +++++++++++++++++-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NullableIndex.java b/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NullableIndex.java index 3d675a8e16b..aee20f29215 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NullableIndex.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/knowledge/NullableIndex.java @@ -179,8 +179,9 @@ public boolean isMemberNullable(MemberShape member, CheckMode checkMode) { } // fall-through. case LIST: - // Map values and list members are only null if they have the @sparse trait. - return container.hasTrait(SparseTrait.ID); + // Map values and list members are only null if they have the @sparse trait + // OR if the target is a Document since Document can hold a null value + return container.hasTrait(SparseTrait.ID) || target.isDocumentShape(); default: return false; } diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java index 1d7ee51bacc..3b4f502d72e 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java @@ -674,6 +674,24 @@ public void nullNonSparseMapValue() { containsString( "a: Non-sparse map shape `ns.foo#Map` cannot contain null values")); } + + @Test + public void nullNonSparseDocumentMapValue() { + // This should not raise any errors since null is a valid Document value + ObjectNode map = ObjectNode.builder() + .withMember("a", Node.nullNode()) + .build(); + NodeValidationVisitor visitor = NodeValidationVisitor.builder() + .value(map) + .model(MODEL) + .allowOptionalNull(true) + .build(); + List events = MODEL + .expectShape(ShapeId.from("ns.foo#DocumentMap")) + .accept(visitor); + + assertThat(events, hasSize(0)); + } @Test public void nullSparseMapValue() { diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json b/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json index 753bc144e57..cb8ff860f51 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json @@ -109,8 +109,14 @@ "type": "string", "traits": { "smithy.api#enum": [ - {"value": "foo", "name": "FOO"}, - {"value": "bar", "name": "BAR"} + { + "value": "foo", + "name": "FOO" + }, + { + "value": "bar", + "name": "BAR" + } ] } }, @@ -207,6 +213,15 @@ "smithy.api#sparse": {} } }, + "ns.foo#DocumentMap": { + "type": "map", + "key": { + "target": "ns.foo#KeyString" + }, + "value": { + "target": "smithy.api#Document" + } + }, "ns.foo#Structure": { "type": "structure", "members": { From ae0c6ff472d67bfdb3f5602b18b998e701bf6b16 Mon Sep 17 00:00:00 2001 From: Jeff Lewis Date: Tue, 17 Jun 2025 10:58:53 -0600 Subject: [PATCH 2/3] restore json formatting --- .../amazon/smithy/model/validation/node-validator.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json b/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json index cb8ff860f51..0fc6576140a 100644 --- a/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json +++ b/smithy-model/src/test/resources/software/amazon/smithy/model/validation/node-validator.json @@ -109,14 +109,8 @@ "type": "string", "traits": { "smithy.api#enum": [ - { - "value": "foo", - "name": "FOO" - }, - { - "value": "bar", - "name": "BAR" - } + {"value": "foo", "name": "FOO"}, + {"value": "bar", "name": "BAR"} ] } }, From 5b59e0587ed9e8459d6fd9cd3d6cffcb2c2a02b1 Mon Sep 17 00:00:00 2001 From: Jeff Lewis Date: Tue, 17 Jun 2025 13:20:20 -0600 Subject: [PATCH 3/3] fix formatting --- .../smithy/model/validation/NodeValidationVisitorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java index 3b4f502d72e..97b0262e388 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/validation/NodeValidationVisitorTest.java @@ -674,7 +674,7 @@ public void nullNonSparseMapValue() { containsString( "a: Non-sparse map shape `ns.foo#Map` cannot contain null values")); } - + @Test public void nullNonSparseDocumentMapValue() { // This should not raise any errors since null is a valid Document value