Skip to content

Fix openapi greedy param names for API Gateway REST APIs #674

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 3 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,36 @@
/*
* Copyright 2020 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.aws.apigateway.openapi;

import java.util.List;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.openapi.OpenApiConfig;
import software.amazon.smithy.utils.ListUtils;

/**
* REST APIs require that name parameters for greedy labels are not suffixed with "+".
*/
final class AddDefaultRestConfigSettings implements ApiGatewayMapper {
@Override
public List<ApiGatewayConfig.ApiType> getApiTypes() {
return ListUtils.of(ApiGatewayConfig.ApiType.REST);
}

@Override
public void updateDefaultSettings(Model model, OpenApiConfig config) {
config.setRemoveGreedyParameterSuffix(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class ApiGatewayExtension implements Smithy2OpenApiExtension {
public List<OpenApiMapper> getOpenApiMappers() {
return ListUtils.of(
ApiGatewayMapper.wrap(new AddDefaultConfigSettings()),
ApiGatewayMapper.wrap(new AddDefaultRestConfigSettings()),
ApiGatewayMapper.wrap(new AddApiKeySource()),
ApiGatewayMapper.wrap(new AddAuthorizers()),
ApiGatewayMapper.wrap(new AddBinaryTypes()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package software.amazon.smithy.aws.apigateway.openapi;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

import java.util.Optional;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.NodePointer;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.openapi.OpenApiConfig;
import software.amazon.smithy.openapi.fromsmithy.OpenApiConverter;
import software.amazon.smithy.utils.IoUtils;

public class AddDefaultRestConfigSettingsTest {
@Test
public void addsDefaultConfigSettings() {
Model model = Model.assembler()
.discoverModels(getClass().getClassLoader())
.addImport(getClass().getResource("greedy-labels-for-rest.json"))
.assemble()
.unwrap();

OpenApiConfig config = new OpenApiConfig();
config.setService(ShapeId.from("smithy.example#Service"));
ApiGatewayConfig apiGatewayConfig = new ApiGatewayConfig();
apiGatewayConfig.setApiGatewayType(ApiGatewayConfig.ApiType.REST);
config.putExtensions(apiGatewayConfig);
ObjectNode result = OpenApiConverter.create().config(config).convertToNode(model);

Node expectedNode = Node.parse(IoUtils.toUtf8String(
getClass().getResourceAsStream("greedy-labels-for-rest.openapi.json")));

Node.assertEquals(result, expectedNode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"smithy": "1.0",
"shapes": {
"smithy.example#Service": {
"type": "service",
"version": "2006-03-01",
"operations": [
{
"target": "smithy.example#Operation"
}
],
"traits": {
"aws.protocols#restJson1": {}
}
},
"smithy.example#Operation": {
"type": "operation",
"input": {
"target": "smithy.example#OperationInput"
},
"traits": {
"smithy.api#http": {
"uri": "/{baz+}",
"method": "POST"
}
}
},
"smithy.example#OperationInput": {
"type": "structure",
"members": {
"baz": {
"target": "smithy.api#String",
"traits": {
"smithy.api#required": {},
"smithy.api#httpLabel": {}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"openapi": "3.0.2",
"info": {
"title": "Service",
"version": "2006-03-01"
},
"paths": {
"/{baz+}": {
"post": {
"operationId": "Operation",
"parameters": [
{
"name": "baz",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"responses": {
"200": {
"description": "Operation response"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public enum HttpPrefixHeadersStrategy {
private boolean keepUnusedComponents;
private String jsonContentType = "application/json";
private boolean forbidGreedyLabels;
private boolean removeGreedyParameterSuffix;
private HttpPrefixHeadersStrategy onHttpPrefixHeaders = HttpPrefixHeadersStrategy.FAIL;
private boolean ignoreUnsupportedTraits;
private Map<String, Node> substitutions = Collections.emptyMap();
Expand Down Expand Up @@ -218,6 +219,14 @@ public void setForbidGreedyLabels(boolean forbidGreedyLabels) {
this.forbidGreedyLabels = forbidGreedyLabels;
}

public boolean getRemoveGreedyParameterSuffix() {
return removeGreedyParameterSuffix;
}

public void setRemoveGreedyParameterSuffix(boolean removeGreedyParameterSuffix) {
this.removeGreedyParameterSuffix = removeGreedyParameterSuffix;
}

public HttpPrefixHeadersStrategy getOnHttpPrefixHeaders() {
return onHttpPrefixHeaders;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ private List<ParameterObject> createPathParameters(Context<T> context, Operation

// Greedy labels in OpenAPI need to include the label in the generated parameter.
// For example, given "/{foo+}", the parameter name must be "foo+".
String name = label.isGreedyLabel() ? label.getContent() + "+" : label.getContent();
// Some vendors/tooling, require the "+" suffix be excluded in the generated parameter.
// If required, the setRemoveGreedyParameterSuffix config option should be set to `true`.
// When this option is enabled, given "/{foo+}", the parameter name will be "foo".
String name = (label.isGreedyLabel() && !context.getConfig().getRemoveGreedyParameterSuffix())
? label.getContent() + "+" : label.getContent();

result.add(ModelUtils.createParameterMember(context, binding.getMember())
.name(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,29 @@ public void canUseCustomMediaType() {

Assertions.assertTrue(Node.printJson(result.toNode()).contains("application/x-amz-json-1.0"));
}

@Test
public void canRemoveGreedyLabelNameParameterSuffix() {
String smithy = "greedy-labels-name-parameter-without-suffix.json";
Model model = Model.assembler()
.addImport(getClass().getResource(smithy))
.discoverModels()
.assemble()
.unwrap();
OpenApiConfig config = new OpenApiConfig();
config.setService(ShapeId.from("smithy.example#Service"));
config.setRemoveGreedyParameterSuffix(true);
OpenApi result = OpenApiConverter.create()
.config(config)
.convert(model);
String openApiModel = smithy.replace(".json", ".openapi.json");
InputStream openApiStream = getClass().getResourceAsStream(openApiModel);

if (openApiStream == null) {
LOGGER.warning("OpenAPI model not found for test case: " + openApiModel);
} else {
Node expectedNode = Node.parse(IoUtils.toUtf8String(openApiStream));
Node.assertEquals(result, expectedNode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"smithy": "1.0",
"shapes": {
"smithy.example#Service": {
"type": "service",
"version": "2006-03-01",
"operations": [
{
"target": "smithy.example#Operation"
}
],
"traits": {
"aws.protocols#restJson1": {}
}
},
"smithy.example#Operation": {
"type": "operation",
"input": {
"target": "smithy.example#OperationInput"
},
"traits": {
"smithy.api#http": {
"uri": "/{foo}/{baz+}",
"method": "POST"
}
}
},
"smithy.example#OperationInput": {
"type": "structure",
"members": {
"foo": {
"target": "smithy.api#String",
"traits": {
"smithy.api#required": {},
"smithy.api#httpLabel": {}
}
},
"baz": {
"target": "smithy.api#String",
"traits": {
"smithy.api#required": {},
"smithy.api#httpLabel": {}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"openapi": "3.0.2",
"info": {
"title": "Service",
"version": "2006-03-01"
},
"paths": {
"/{foo}/{baz+}": {
"post": {
"operationId": "Operation",
"parameters": [
{
"name": "foo",
"in": "path",
"schema": {
"type": "string"
},
"required": true
},
{
"name": "baz",
"in": "path",
"schema": {
"type": "string"
},
"required": true
}
],
"responses": {
"200": {
"description": "Operation response"
}
}
}
}
},
"components": {}
}