Skip to content

Fail to build for services with unsupported payloads #1187

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 9 commits into from
Nov 15, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import static software.amazon.awssdk.codegen.RemoveUnusedShapes.removeUnusedShapes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor;
Expand Down Expand Up @@ -53,6 +55,8 @@
*/
public class IntermediateModelBuilder {

private static final List<String> NO_BODY_METHODS = Arrays.asList("GET", "HEAD", "DELETE");

private static final Logger log = LoggerFactory.getLogger(IntermediateModelBuilder.class);
private final CustomizationConfig customConfig;
private final ServiceModel service;
Expand Down Expand Up @@ -157,9 +161,28 @@ public IntermediateModel build() {

setSimpleMethods(trimmedModel);

validateOperations(trimmedModel);

return trimmedModel;
}

/**
* Validates that operations that use GET, DELETE or HEAD do not have an HTTP body.
*/
private void validateOperations(IntermediateModel model) {
List<String> methods = model.getOperations()
.values()
.stream()
.filter(e -> e.getInputShape().hasPayloadMembers())
.filter(e -> NO_BODY_METHODS.contains(e.getInputShape().getMarshaller().getVerb()))
.map(e -> e.getInputShape().getC2jName())
.collect(Collectors.toList());

if (methods.size() > 0) {
throw new RuntimeException("An HTTP body is not allowed on GET/DELETE/HEAD requests. Invalid operations: " + methods);
}
}

/**
* Link the member to it's corresponding shape (if it exists).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2019 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.codegen;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import org.junit.Test;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;

public class IntermediateModelBuilderTest {

@Test(expected = RuntimeException.class)
public void modelWithUnsupportedPayloadMethods_ThrowsException() {
File serviceModel = new File(getClass().getResource("poet/client/c2j/unsupportedpayloads/service-2.json").getFile());
File customizationModel = new File(getClass().getResource("poet/client/c2j/unsupportedpayloads/customization.config").getFile());

C2jModels models = C2jModels.builder()
.serviceModel(ModelLoaderUtils.loadModel(ServiceModel.class, serviceModel))
.customizationConfig(ModelLoaderUtils.loadModel(CustomizationConfig.class, customizationModel))
.build();

try {
new IntermediateModelBuilder(models).build();
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("GetBlacklistReportsRequest"));
assertTrue(e.getMessage().contains("GetDedicatedIpsRequest"));
throw e;
}

fail("Expected RuntimeException");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"authPolicyActions" : {
"skip" : true
}
}
Loading