From b30e7598959d7ac28ecb01dffbd2f588e2fdf72e Mon Sep 17 00:00:00 2001 From: Maxim Korolkov Date: Thu, 25 Jul 2024 13:10:32 -0700 Subject: [PATCH 1/2] Add intEnum validation to NodeValidationVisitor --- .../model/validation/node/IntEnumPlugin.java | 42 +++++++++++++++++++ .../validation/node/NodeValidatorPlugin.java | 1 + .../validation/NodeValidationVisitorTest.java | 7 ++++ .../model/validation/node-validator.json | 17 ++++++++ 4 files changed, 67 insertions(+) create mode 100644 smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java new file mode 100644 index 00000000000..049e1ce4b80 --- /dev/null +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java @@ -0,0 +1,42 @@ +/* + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.smithy.model.validation.node; + +import java.util.Collection; +import software.amazon.smithy.model.node.NumberNode; +import software.amazon.smithy.model.shapes.IntEnumShape; +import software.amazon.smithy.model.validation.ValidationUtils; + + +/** + * Validates NumberNodes against intEnum shapes' allowed enum values. + */ +final class IntEnumPlugin extends FilteredPlugin { + + IntEnumPlugin() { + super(IntEnumShape.class, NumberNode.class); + } + + @Override + protected void check(IntEnumShape shape, NumberNode node, Context context, Emitter emitter) { + Collection values = shape.getEnumValues().values(); + if (!values.contains(node.getValue().intValue())) { + emitter.accept(node, String.format( + "Integer value provided for `%s` must be one of the following values: %s, but found %s", + shape.getId(), ValidationUtils.tickedList(values), node.getValue())); + } + } +} diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/NodeValidatorPlugin.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/NodeValidatorPlugin.java index 5398f3529c3..2042ece5e88 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/NodeValidatorPlugin.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/NodeValidatorPlugin.java @@ -64,6 +64,7 @@ static List getBuiltins() { new PatternTraitPlugin(), new RangeTraitPlugin(), new StringEnumPlugin(), + new IntEnumPlugin(), new StringLengthPlugin(), new UniqueItemsPlugin()); } 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 779a5f7b3c9..1712d51032d 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 @@ -132,6 +132,13 @@ public static Collection data() { {"ns.foo#Integer", "9", new String[] {"Value provided for `ns.foo#Integer` must be greater than or equal to 10, but found 9"}}, {"ns.foo#Integer", "10.2", new String[] {"integer shapes must not have floating point values, but found `10.2` provided for `ns.foo#Integer`"}}, + // intEnum + {"ns.foo#IntEnum", "1", null}, + {"ns.foo#IntEnum", "2", null}, + {"ns.foo#IntEnum", "3", new String[] {"Integer value provided for `ns.foo#IntEnum` must be one of the following values: `1`, `2`, but found 3"}}, + {"ns.foo#IntEnum", "true", new String[] {"Expected number value for intEnum shape, `ns.foo#IntEnum`; found boolean value, `true`"}}, + {"ns.foo#IntEnum", "1.1", new String[] {"intEnum shapes must not have floating point values, but found `1.1` provided for `ns.foo#IntEnum`"}}, + // long {"ns.foo#Long", "10", null}, {"ns.foo#Long", "true", new String[] {"Expected number value for long shape, `ns.foo#Long`; found boolean value, `true`"}}, 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 3b3375b42cc..f4100e4c524 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 @@ -28,6 +28,23 @@ } } }, + "ns.foo#IntEnum": { + "type": "intEnum", + "members": { + "V1": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": 1 + } + }, + "V2": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": 2 + } + } + } + }, "ns.foo#Long": { "type": "long", "traits": { From 327e3e11b326e5b3aea0f277053515545eb2a5bf Mon Sep 17 00:00:00 2001 From: maximk000 <57971916+maximk000@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:52:05 -0700 Subject: [PATCH 2/2] Use new copyright notice Co-authored-by: Manuel Sugawara --- .../model/validation/node/IntEnumPlugin.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java index 049e1ce4b80..e33be0e8c14 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/node/IntEnumPlugin.java @@ -1,16 +1,6 @@ /* - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"). - * You may not use this file except in compliance with the License. - * A copy of the License is located at - * - * http://aws.amazon.com/apache2.0 - * - * or in the "license" file accompanying this file. This file is distributed - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either - * express or implied. See the License for the specific language governing - * permissions and limitations under the License. + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 */ package software.amazon.smithy.model.validation.node;