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

fix(codegen): use TopDownIndex::getContainedOperations() for operation iterations #1109

Merged
merged 1 commit into from
Dec 12, 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 @@ -28,6 +28,7 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.typescript.codegen.auth.AuthUtils;
Expand Down Expand Up @@ -229,8 +230,9 @@ private void generateHttpAuthSchemeConfig(

// feat(experimentalIdentityAndAuth): gather HttpAuthSchemes to generate
ServiceIndex serviceIndex = ServiceIndex.of(model);
TopDownIndex topDownIndex = TopDownIndex.of(model);
Map<ShapeId, HttpAuthScheme> allEffectiveHttpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(service, serviceIndex, authIndex);
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(service, serviceIndex, authIndex, topDownIndex);
List<HttpAuthSchemeTarget> targetAuthSchemes = getHttpAuthSchemeTargets(target, allEffectiveHttpAuthSchemes);

// Generate only if the "inherited" target is different than the current target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.ServiceIndex.AuthSchemeMode;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.Trait;
Expand Down Expand Up @@ -75,15 +76,16 @@ private AuthUtils() {}
public static Map<ShapeId, HttpAuthScheme> getAllEffectiveNoAuthAwareAuthSchemes(
ServiceShape serviceShape,
ServiceIndex serviceIndex,
SupportedHttpAuthSchemesIndex authIndex
SupportedHttpAuthSchemesIndex authIndex,
TopDownIndex topDownIndex
) {
Map<ShapeId, HttpAuthScheme> effectiveAuthSchemes = new TreeMap<>();
var serviceEffectiveAuthSchemes =
serviceIndex.getEffectiveAuthSchemes(serviceShape, AuthSchemeMode.NO_AUTH_AWARE);
for (ShapeId shapeId : serviceEffectiveAuthSchemes.keySet()) {
effectiveAuthSchemes.put(shapeId, authIndex.getHttpAuthScheme(shapeId));
}
for (var operation : serviceShape.getAllOperations()) {
for (var operation : topDownIndex.getContainedOperations(serviceShape)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there anything going in place to warn that serviceShape.getAllOperations() is not how to get service operations? 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically the javadoc for ServiceShape::getAllOperations() mentions that it doesn't contain everything and references TopDownIndex::getContainedOperations(), unfortunately there isn't a way to enforce it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Albeit there is documentation on smithy.io that documents using the TopDownIndex.

var operationEffectiveAuthSchemes =
serviceIndex.getEffectiveAuthSchemes(serviceShape, operation, AuthSchemeMode.NO_AUTH_AWARE);
for (ShapeId shapeId : operationEffectiveAuthSchemes.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.ServiceIndex.AuthSchemeMode;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.Trait;
Expand Down Expand Up @@ -59,6 +61,7 @@ public class HttpAuthSchemeProviderGenerator implements Runnable {

private final SupportedHttpAuthSchemesIndex authIndex;
private final ServiceIndex serviceIndex;
private final TopDownIndex topDownIndex;
private final ServiceShape serviceShape;
private final Symbol serviceSymbol;
private final String serviceName;
Expand Down Expand Up @@ -88,11 +91,12 @@ public HttpAuthSchemeProviderGenerator(

this.authIndex = new SupportedHttpAuthSchemesIndex(integrations, model, settings);
this.serviceIndex = ServiceIndex.of(model);
this.topDownIndex = TopDownIndex.of(model);
this.serviceShape = settings.getService(model);
this.serviceSymbol = symbolProvider.toSymbol(serviceShape);
this.serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider);
this.effectiveHttpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex);
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex);
this.httpAuthSchemeParameters =
AuthUtils.collectHttpAuthSchemeParameters(effectiveHttpAuthSchemes.values());
}
Expand Down Expand Up @@ -395,7 +399,8 @@ private void generateDefaultHttpAuthSchemeProviderFunction() {
w.openBlock("switch (authParameters.operation) {", "};", () -> {
var serviceAuthSchemes = serviceIndex.getEffectiveAuthSchemes(
serviceShape, AuthSchemeMode.NO_AUTH_AWARE);
for (ShapeId operationShapeId : serviceShape.getAllOperations()) {
for (OperationShape operationShape : topDownIndex.getContainedOperations(serviceShape)) {
ShapeId operationShapeId = operationShape.getId();
var operationAuthSchemes = serviceIndex.getEffectiveAuthSchemes(
serviceShape, operationShapeId, AuthSchemeMode.NO_AUTH_AWARE);
// Skip operation generation if operation auth schemes are equivalent to the default service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
Expand Down Expand Up @@ -145,8 +146,9 @@ private getIdentityProviderConfigProvider() {
s.getModel(),
s.getSettings());
ServiceIndex serviceIndex = ServiceIndex.of(s.getModel());
Map<ShapeId, HttpAuthScheme> httpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(s.getService(), serviceIndex, authIndex);
TopDownIndex topDownIndex = TopDownIndex.of(s.getModel());
Map<ShapeId, HttpAuthScheme> httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(
s.getService(), serviceIndex, authIndex, topDownIndex);
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
if (scheme == null) {
continue;
Expand Down Expand Up @@ -177,8 +179,9 @@ public void customize(TypeScriptCodegenContext codegenContext) {
codegenContext.settings());
ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model());
ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model());
TopDownIndex topDownIndex = TopDownIndex.of(codegenContext.model());
Map<ShapeId, HttpAuthScheme> httpAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex);
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex);
Map<String, ConfigField> configFields =
AuthUtils.collectConfigFields(httpAuthSchemes.values());
Map<Symbol, ResolveConfigFunction> resolveConfigFunctions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.typescript.codegen.CodegenUtils;
Expand Down Expand Up @@ -60,11 +61,12 @@ public void customize(TypeScriptCodegenContext codegenContext) {
codegenContext.model(),
codegenContext.settings());
ServiceIndex serviceIndex = ServiceIndex.of(codegenContext.model());
TopDownIndex topDownIndex = TopDownIndex.of(codegenContext.model());
String serviceName = CodegenUtils.getServiceName(
codegenContext.settings(), codegenContext.model(), codegenContext.symbolProvider());
ServiceShape serviceShape = codegenContext.settings().getService(codegenContext.model());
Map<ShapeId, HttpAuthScheme> effectiveAuthSchemes =
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex);
AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(serviceShape, serviceIndex, authIndex, topDownIndex);
Map<String, ConfigField> configFields = AuthUtils.collectConfigFields(effectiveAuthSchemes.values());

generateHttpAuthExtensionConfigurationInterface(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.ServiceIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.HttpApiKeyAuthTrait;
import software.amazon.smithy.model.traits.OptionalAuthTrait;
import software.amazon.smithy.typescript.codegen.CodegenUtils;
Expand Down Expand Up @@ -155,8 +155,8 @@ private static boolean hasEffectiveHttpApiKeyAuthTrait(
ServiceShape service
) {
ServiceIndex serviceIndex = ServiceIndex.of(model);
for (ShapeId id : service.getAllOperations()) {
OperationShape operation = model.expectShape(id, OperationShape.class);
TopDownIndex topDownIndex = TopDownIndex.of(model);
for (OperationShape operation : topDownIndex.getContainedOperations(service)) {
if (operation.hasTrait(OptionalAuthTrait.ID)) {
continue;
}
Expand Down
Loading