Skip to content
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

feat(cbor): add protocol resolution priority system #1370

Merged
merged 4 commits into from
Aug 19, 2024
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
2 changes: 2 additions & 0 deletions .changeset/young-eagles-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
10 changes: 5 additions & 5 deletions private/smithy-rpcv2-cbor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@
"@aws-crypto/sha256-js": "5.2.0",
"@aws-sdk/types": "latest",
"@smithy/config-resolver": "^3.0.5",
"@smithy/core": "^2.3.2",
"@smithy/core": "^2.4.0",
"@smithy/fetch-http-handler": "^3.2.4",
"@smithy/hash-node": "^3.0.3",
"@smithy/invalid-dependency": "^3.0.3",
"@smithy/middleware-content-length": "^3.0.5",
"@smithy/middleware-retry": "^3.0.14",
"@smithy/middleware-retry": "^3.0.15",
"@smithy/middleware-serde": "^3.0.3",
"@smithy/middleware-stack": "^3.0.3",
"@smithy/node-config-provider": "^3.1.4",
"@smithy/node-http-handler": "^3.1.4",
"@smithy/protocol-http": "^4.1.0",
"@smithy/smithy-client": "^3.1.12",
"@smithy/smithy-client": "^3.2.0",
"@smithy/types": "^3.3.0",
"@smithy/url-parser": "^3.0.3",
"@smithy/util-base64": "^3.0.0",
"@smithy/util-body-length-browser": "^3.0.0",
"@smithy/util-body-length-node": "^3.0.0",
"@smithy/util-defaults-mode-browser": "^3.0.14",
"@smithy/util-defaults-mode-node": "^3.0.14",
"@smithy/util-defaults-mode-browser": "^3.0.15",
"@smithy/util-defaults-mode-node": "^3.0.15",
"@smithy/util-middleware": "^3.0.3",
"@smithy/util-retry": "^3.0.3",
"@smithy/util-utf8": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -136,7 +136,9 @@ private ProtocolGenerator resolveProtocolGenerator(
TypeScriptSettings settings
) {
// Collect all of the supported protocol generators.
Map<ShapeId, ProtocolGenerator> generators = new HashMap<>();
// Preserve insertion order as default priority order.
Map<ShapeId, ProtocolGenerator> generators = new LinkedHashMap<>();

for (TypeScriptIntegration integration : integrations) {
for (ProtocolGenerator generator : integration.getProtocolGenerators()) {
generators.put(generator.getProtocol(), generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.typescript.codegen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -36,6 +37,7 @@
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.DefaultTrait;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriority;
import software.amazon.smithy.utils.SmithyUnstableApi;

/**
Expand Down Expand Up @@ -450,8 +452,13 @@ public ShapeId resolveServiceProtocol(Model model, ServiceShape service, Set<Sha
+ "generate in smithy-build.json to generate this service.");
}

return resolvedProtocols.stream()
.filter(supportedProtocols::contains)
List<ShapeId> protocolPriority = ProtocolPriority.getProtocolPriority(service.toShapeId());
List<ShapeId> protocolPriorityList = protocolPriority != null && !protocolPriority.isEmpty()
? protocolPriority
: new ArrayList<>(supportedProtocols);

return protocolPriorityList.stream()
.filter(resolvedProtocols::contains)
.findFirst()
.orElseThrow(() -> new UnresolvableProtocolException(String.format(
"The %s service supports the following unsupported protocols %s. The following protocol "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.typescript.codegen.protocols;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import software.amazon.smithy.model.shapes.ShapeId;


/**
* Allows customization of protocol selection for specific services or a global default ordering.
*/
public final class ProtocolPriority {
kuhe marked this conversation as resolved.
Show resolved Hide resolved
private static final Map<ShapeId, List<ShapeId>> SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS = new HashMap<>();
private static List<ShapeId> customDefaultPriority = null;

private ProtocolPriority() {}

/**
* @param serviceShapeId - service scope.
* @param protocolPriorityOrder - priority order of protocols.
*/
public static void setProtocolPriority(ShapeId serviceShapeId, List<ShapeId> protocolPriorityOrder) {
SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.put(serviceShapeId, protocolPriorityOrder);
}

/**
* @param defaultProtocolPriorityOrder - use for all services that don't have a more specific priority order.
*/
public static void setCustomDefaultProtocolPriority(List<ShapeId> defaultProtocolPriorityOrder) {
customDefaultPriority = new ArrayList<>(defaultProtocolPriorityOrder);
}

/**
* @param serviceShapeId - service scope.
* @return priority order of protocols or null if no override exists.
*/
public static List<ShapeId> getProtocolPriority(ShapeId serviceShapeId) {
return SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.getOrDefault(
serviceShapeId,
customDefaultPriority != null ? new ArrayList<>(customDefaultPriority) : null
);
}

/**
* @param serviceShapeId - to unset.
*/
public static void deleteProtocolPriority(ShapeId serviceShapeId) {
SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.remove(serviceShapeId);
}

/**
* Unset the custom default priority order.
*/
public static void deleteCustomDefaultProtocolPriority() {
customDefaultPriority = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package software.amazon.smithy.typescript.codegen;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;

import java.util.stream.Stream;
import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriority;
import software.amazon.smithy.utils.MapUtils;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class TypeScriptSettingsTest {

@Test
Expand Down Expand Up @@ -87,6 +98,121 @@ private static Stream<Arguments> providePackageDescriptionTestCases() {
);
}

@Test
public void resolveServiceProtocol(@Mock Model model,
kuhe marked this conversation as resolved.
Show resolved Hide resolved
@Mock ServiceShape service,
@Mock ServiceIndex serviceIndex) {
TypeScriptSettings subject = new TypeScriptSettings();

// these are mock protocol names.
ShapeId rpcv2Cbor = ShapeId.from("namespace#rpcv2Cbor");
ShapeId json1_0 = ShapeId.from("namespace#json1_0");
ShapeId json1_1 = ShapeId.from("namespace#json1_1");
ShapeId restJson1 = ShapeId.from("namespace#restJson1");
ShapeId restXml = ShapeId.from("namespace#restXml");
ShapeId query = ShapeId.from("namespace#query");
ShapeId serviceQuery = ShapeId.from("namespace#serviceQuery");

when(model.getKnowledge(any(), any())).thenReturn(serviceIndex);
ShapeId serviceShapeId = ShapeId.from("namespace#Service");
when(service.toShapeId()).thenReturn(serviceShapeId);

LinkedHashSet<ShapeId> protocolShapeIds = new LinkedHashSet<>(
List.of(
json1_0, json1_1, restJson1, rpcv2Cbor, restXml, query, serviceQuery
)
);

{
// spec case 1.
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
rpcv2Cbor, null,
json1_0, null
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
// JS customization has JSON at higher default priority than CBOR.
assertEquals(json1_0, protocol);
}

{
// spec case 2.
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
rpcv2Cbor, null
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
assertEquals(rpcv2Cbor, protocol);
}

{
// spec case 3.
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
rpcv2Cbor, null,
json1_0, null,
query, null
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
// JS customization has JSON at higher default priority than CBOR.
assertEquals(json1_0, protocol);
}

{
// spec case 4.
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
json1_0, null,
query, null
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
assertEquals(json1_0, protocol);
}

{
// spec case 5.
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
query, null
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
assertEquals(query, protocol);
}

{
// service override, non-spec
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
json1_0, null,
json1_1, null,
restJson1, null,
rpcv2Cbor, null,
restXml, null,
query, null,
serviceQuery, null
));
ProtocolPriority.setProtocolPriority(serviceShapeId, List.of(
serviceQuery, rpcv2Cbor, json1_1, restJson1, restXml, query
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
ProtocolPriority.deleteProtocolPriority(serviceShapeId);
assertEquals(serviceQuery, protocol);
}

{
// global default override
when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of(
json1_0, null,
json1_1, null,
restJson1, null,
rpcv2Cbor, null,
restXml, null,
query, null,
serviceQuery, null
));
ProtocolPriority.setCustomDefaultProtocolPriority(List.of(
rpcv2Cbor, json1_1, restJson1, restXml, query
));
ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds);
ProtocolPriority.deleteCustomDefaultProtocolPriority();
assertEquals(rpcv2Cbor, protocol);
}
}

@Test
public void resolvesSupportProtocols() {
// TODO
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2266,7 +2266,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/core@^2.3.2, @smithy/core@workspace:packages/core":
"@smithy/core@^2.4.0, @smithy/core@workspace:packages/core":
version: 0.0.0-use.local
resolution: "@smithy/core@workspace:packages/core"
dependencies:
Expand Down Expand Up @@ -2590,7 +2590,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/middleware-retry@^3.0.14, @smithy/middleware-retry@workspace:^, @smithy/middleware-retry@workspace:packages/middleware-retry":
"@smithy/middleware-retry@^3.0.15, @smithy/middleware-retry@workspace:^, @smithy/middleware-retry@workspace:packages/middleware-retry":
version: 0.0.0-use.local
resolution: "@smithy/middleware-retry@workspace:packages/middleware-retry"
dependencies:
Expand Down Expand Up @@ -2784,7 +2784,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/smithy-client@^3.1.12, @smithy/smithy-client@workspace:^, @smithy/smithy-client@workspace:packages/smithy-client":
"@smithy/smithy-client@^3.2.0, @smithy/smithy-client@workspace:^, @smithy/smithy-client@workspace:packages/smithy-client":
version: 0.0.0-use.local
resolution: "@smithy/smithy-client@workspace:packages/smithy-client"
dependencies:
Expand All @@ -2810,25 +2810,25 @@ __metadata:
"@aws-crypto/sha256-js": 5.2.0
"@aws-sdk/types": latest
"@smithy/config-resolver": ^3.0.5
"@smithy/core": ^2.3.2
"@smithy/core": ^2.4.0
"@smithy/fetch-http-handler": ^3.2.4
"@smithy/hash-node": ^3.0.3
"@smithy/invalid-dependency": ^3.0.3
"@smithy/middleware-content-length": ^3.0.5
"@smithy/middleware-retry": ^3.0.14
"@smithy/middleware-retry": ^3.0.15
"@smithy/middleware-serde": ^3.0.3
"@smithy/middleware-stack": ^3.0.3
"@smithy/node-config-provider": ^3.1.4
"@smithy/node-http-handler": ^3.1.4
"@smithy/protocol-http": ^4.1.0
"@smithy/smithy-client": ^3.1.12
"@smithy/smithy-client": ^3.2.0
"@smithy/types": ^3.3.0
"@smithy/url-parser": ^3.0.3
"@smithy/util-base64": ^3.0.0
"@smithy/util-body-length-browser": ^3.0.0
"@smithy/util-body-length-node": ^3.0.0
"@smithy/util-defaults-mode-browser": ^3.0.14
"@smithy/util-defaults-mode-node": ^3.0.14
"@smithy/util-defaults-mode-browser": ^3.0.15
"@smithy/util-defaults-mode-node": ^3.0.15
"@smithy/util-middleware": ^3.0.3
"@smithy/util-retry": ^3.0.3
"@smithy/util-utf8": ^3.0.0
Expand Down Expand Up @@ -2945,7 +2945,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/util-defaults-mode-browser@^3.0.14, @smithy/util-defaults-mode-browser@workspace:packages/util-defaults-mode-browser":
"@smithy/util-defaults-mode-browser@^3.0.15, @smithy/util-defaults-mode-browser@workspace:packages/util-defaults-mode-browser":
version: 0.0.0-use.local
resolution: "@smithy/util-defaults-mode-browser@workspace:packages/util-defaults-mode-browser"
dependencies:
Expand All @@ -2962,7 +2962,7 @@ __metadata:
languageName: unknown
linkType: soft

"@smithy/util-defaults-mode-node@^3.0.14, @smithy/util-defaults-mode-node@workspace:packages/util-defaults-mode-node":
"@smithy/util-defaults-mode-node@^3.0.15, @smithy/util-defaults-mode-node@workspace:packages/util-defaults-mode-node":
version: 0.0.0-use.local
resolution: "@smithy/util-defaults-mode-node@workspace:packages/util-defaults-mode-node"
dependencies:
Expand Down
Loading