Skip to content

Validate required params present before running rules #3946

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

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.utils.CompletableFutureUtils;
import software.amazon.awssdk.utils.Validate;

public class EndpointProviderSpec implements ClassSpec {
private static final String RULE_SET_FIELD_NAME = "ENDPOINT_RULE_SET";
Expand Down Expand Up @@ -132,6 +133,8 @@ private MethodSpec resolveEndpointMethod() {
.addAnnotation(Override.class)
.addParameter(endpointRulesSpecUtils.parametersClassName(), paramsName);

b.addCode(validateRequiredParams());

b.addStatement("$T res = new $T().evaluate($N, toIdentifierValueMap($N))",
endpointRulesSpecUtils.rulesRuntimeClassName("Value"),
endpointRulesSpecUtils.rulesRuntimeClassName("DefaultRuleEngine"),
Expand Down Expand Up @@ -161,4 +164,21 @@ private MethodSpec ruleSetBuildMethod(TypeSpec.Builder classBuilder) {
ruleSetCreationSpec.helperMethods().forEach(classBuilder::addMethod);
return b.build();
}

private CodeBlock validateRequiredParams() {
CodeBlock.Builder b = CodeBlock.builder();

Map<String, ParameterModel> parameters = intermediateModel.getEndpointRuleSetModel().getParameters();
parameters.entrySet().stream()
.filter(e -> Boolean.TRUE.equals(e.getValue().isRequired()))
.forEach(e -> {
b.addStatement("$T.notNull($N.$N(), $S)",
Validate.class,
"endpointParams",
endpointRulesSpecUtils.paramMethodName(e.getKey()),
String.format("Parameter '%s' must not be null", e.getKey()));
});

return b.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import software.amazon.awssdk.services.query.endpoints.QueryEndpointProvider;
import software.amazon.awssdk.utils.CompletableFutureUtils;
import software.amazon.awssdk.utils.MapUtils;
import software.amazon.awssdk.utils.Validate;

@Generated("software.amazon.awssdk:codegen")
@SdkInternalApi
Expand All @@ -19,6 +20,7 @@ public final class DefaultQueryEndpointProvider implements QueryEndpointProvider

@Override
public CompletableFuture<Endpoint> resolveEndpoint(QueryEndpointParams endpointParams) {
Validate.notNull(endpointParams.region(), "Parameter 'region' must not be null");
Value res = new DefaultRuleEngine().evaluate(ENDPOINT_RULE_SET, toIdentifierValueMap(endpointParams));
try {
return CompletableFuture.completedFuture(AwsEndpointProviderUtils.valueAsEndpointOrThrow(res));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 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.awssdk.services.endpointproviders;

import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.restjsonendpointproviders.endpoints.RestJsonEndpointProvidersEndpointProvider;

public class EndpointProviderTest {
@Test
public void resolveEndpoint_requiredParamNotPresent_throws() {
assertThatThrownBy(() -> RestJsonEndpointProvidersEndpointProvider.defaultProvider()
.resolveEndpoint(r -> {}))
.hasMessageContaining("must not be null");
}

@Test
public void resolveEndpoint_optionalParamNotPresent_doesNotThrow() {
assertThatNoException().isThrownBy(() ->
RestJsonEndpointProvidersEndpointProvider.defaultProvider()
.resolveEndpoint(r -> r.useFips(null)
.region(Region.of("us-mars-1"))));
}
}