fix: HttpApi explicit AuthorizationScopes: [] is not overridden by authorizer default - #3980
Conversation
…thorizer default OpenApiEditor.add_auth_to_method defaulted an unset AuthorizationScopes to [], then _set_method_authorizer checked it with `if authorization_scopes:`. Since [] is falsy, this couldn't distinguish "not set" from an explicit empty-list override, so a method-level `AuthorizationScopes: []` (meant to require no scopes, overriding the authorizer's default) was silently ignored and the authorizer's default AuthorizationScopes were enforced instead. SwaggerEditor's equivalent REST API code path (swagger.py) already gets this right by using None as the "not set" sentinel and an `is not None` check. This applies the same fix to OpenApiEditor so HTTP APIs behave consistently with REST APIs, and also aligns the authorizer-presence check (`authorizers.get(authorizer_name) is not None`) with swagger.py's pattern. Fixes aws#3979
|
Maintainer review pass. I checked this out locally at Why this is
|
Summary
Fixes #3979.
For
AWS::Serverless::HttpApi, an event/function-levelAuth.AuthorizationScopes: []is meant to override the named authorizer's defaultAuthorizationScopes, requiring no scopes for that method — this already works correctly forAWS::Serverless::Api(REST APIs).OpenApiEditor.add_auth_to_method(samtranslator/open_api/open_api.py) defaulted an unsetAuthorizationScopesto[]:_set_method_authorizerthen checked it withif authorization_scopes:— since[]is falsy, this is indistinguishable from "not set," so the explicit override was silently dropped and the authorizer's default scopes were enforced instead. This means requests the template author intended to allow without those scopes get rejected by API Gateway.SwaggerEditor's equivalent REST API code path (samtranslator/swagger/swagger.py) already handles this correctly — it usesNoneas the "not set" sentinel (auth.get("AuthorizationScopes"), no default) and checksis not None. This PR applies the identical fix toOpenApiEditor, and also aligns the authorizer-presence check (authorizers.get(authorizer_name) is not None) withswagger.py's pattern, replacing an unguardedauthorizers[authorizer_name]dict-subscript.Test plan
tests/translator/input/http_api_with_auth_with_default_scopes.yaml, a direct HttpApi port of the existing REST API regression testtests/translator/input/api_with_auth_with_default_scopes.yaml(default authorizer, explicit authorizer, scope overwrite, and bothAuthorizationScopes: []cases), with expected output for all three partitions (aws,aws-cn,aws-us-gov).AuthorizationScopes: []cases produced the authorizer's default scopes instead of[]— and passes with the fix, with output matching the semantics already proven correct for REST APIs.python -m pytest tests/translator tests/plugins tests/parser tests/openapi tests/swagger tests/model— 3241 passed (same 5 pre-existing, unrelated SAR-timing/region failures as ondevelop).ruff check/black --checkclean on the changed file.