Skip to content

Share security scheme context keys across import-mapped packages#2452

Merged
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2383
Jul 12, 2026
Merged

Share security scheme context keys across import-mapped packages#2452
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2383

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes: #2383

Typed context keys (#1187) gave each generated package its own unexported key type per security scheme, so a scheme shared across specs via $ref no longer matched in context.Value lookups between packages.

A security scheme that $refs a spec covered by import-mapping now aliases the scopes constant declared by the mapped package instead of declaring its own context key type (BearerAuthScopes = externalRef0.BearerAuthScopes). The constant carries the shared package's key type, so lookups match across packages, while locally declared schemes keep their per-spec types. Scopes constants are also derived from components/securitySchemes rather than operation security requirements, so a shared spec with no paths exports the constants that other packages alias.

The enable-auth-scopes-on-context option is now formally marked as deprecated in its Go doc comment and in the configuration JSON schema.

Co-Authored-By: Claude Fable 5 noreply@anthropic.com

Closes: #2383

Typed context keys (#1187) gave each generated package its own unexported
key type per security scheme, so a scheme shared across specs via $ref no
longer matched in context.Value lookups between packages.

A security scheme that $refs a spec covered by import-mapping now aliases
the scopes constant declared by the mapped package instead of declaring
its own context key type (`BearerAuthScopes = externalRef0.BearerAuthScopes`).
The constant carries the shared package's key type, so lookups match
across packages, while locally declared schemes keep their per-spec types.
Scopes constants are also derived from components/securitySchemes rather
than operation security requirements, so a shared spec with no paths
exports the constants that other packages alias.

The enable-auth-scopes-on-context option is now formally marked as
deprecated in its Go doc comment and in the configuration JSON schema.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 12, 2026 15:13
@mromaszewicz mromaszewicz added the bug Something isn't working label Jul 12, 2026
@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes issue #2383 where typed context keys for security schemes broke context.Value lookups across packages when a scheme was $ref'd from an import-mapped spec. The fix makes each referencing package emit a constant alias (BearerAuthScopes = externalRef0.BearerAuthScopes) instead of declaring its own context key type, so the key's identity is shared. Constants are now derived from components/securitySchemes rather than operation security requirements, enabling specs with no paths to export constants that other packages alias.

  • GenerateConstants now accepts *openapi3.T (was []OperationDefinition) and iterates components/securitySchemes, calling importedSecuritySchemeScopes to determine whether to emit a local typed constant or a package-qualified alias.
  • schema.go introduces SecuritySchemeProvider with an ImportedScopes field; constants.tmpl branches on it to choose the correct declaration form.
  • enable-auth-scopes-on-context is now formally marked Deprecated in both the Go doc comment and the JSON schema; both must be enabled (referencing package and referenced package) for the aliasing to work.

Confidence Score: 3/5

The change correctly fixes cross-package context key sharing, but it breaks the exported pkg/codegen API surface in two places that direct library consumers will hit at compile time.

The algorithm and generated output are correct, and the test coverage across unit and integration tests is thorough. However, GenerateConstants is an exported function whose parameter type changed from []OperationDefinition to *openapi3.T, and Constants.SecuritySchemeProviderNames was renamed to SecuritySchemeProviders — both without a prior deprecation notice. Any downstream tool or plugin that imports pkg/codegen and calls either of these will fail to compile. The template data model also changed in a way that silently breaks custom constants.tmpl overrides.

pkg/codegen/codegen.go and pkg/codegen/schema.go carry the exported API changes that affect direct library consumers.

Important Files Changed

Filename Overview
pkg/codegen/codegen.go Core logic change: GenerateConstants now derives security scheme providers from components/securitySchemes instead of operations, and uses importedSecuritySchemeScopes to emit alias constants for import-mapped schemes. Logic is sound, but the exported GenerateConstants signature changed — a breaking change for direct library consumers.
pkg/codegen/schema.go New SecuritySchemeProvider struct added; Constants.SecuritySchemeProviderNames renamed to SecuritySchemeProviders — exported field rename breaks direct library consumers.
pkg/codegen/templates/constants.tmpl Template updated to iterate SecuritySchemeProviders and emit either a typed local constant or an alias constant for imported schemes. Template override users will silently lose output if they haven't migrated.
pkg/codegen/configuration.go Adds Deprecated Go doc comment to EnableAuthScopesOnContext and documents the cross-package aliasing behavior. JSON schema also updated in configuration-schema.json.
internal/test/references/multipackage/imports_test.go New test TestSecuritySchemeScopesShared correctly validates that context.Value lookups are interchangeable between the two packages' BearerAuthScopes constants, directly reproducing the issue.
pkg/codegen/codegen_test.go Four new unit tests cover: import-mapped alias, no-import-mapping fallback, current-package mapping, and shared spec with no paths. Test coverage is thorough for the new behavior.
internal/test/servers/middleware/fiber/middleware.gen.go BearerAuthScopes constant now generated from components/securitySchemes (was previously absent because the scheme had no operations); correct side effect of the logic change.
internal/test/references/multipackage/packageA/externalref.gen.go New const BearerAuthScopes and unexported bearerAuthContextKey type correctly generated from the new packageA spec that declares BearerAuth in components/securitySchemes.
internal/test/references/multipackage/externalref.gen.go BearerAuthScopes alias constant correctly generated as = externalRef0.BearerAuthScopes, sharing the context key type from packageA; embedded spec updated to match new security scheme in spec.yaml.
configuration-schema.json JSON schema description for enable-auth-scopes-on-context updated to surface the Deprecated status and document the cross-package aliasing behavior. Kept in sync with configuration.go as required.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
pkg/codegen/codegen.go:1043-1063
**Breaking signature change to exported `GenerateConstants`**

The function signature changed from `GenerateConstants(t *template.Template, ops []OperationDefinition)` to `GenerateConstants(t *template.Template, swagger *openapi3.T)`. Any external code that imports `pkg/codegen` and calls `GenerateConstants` directly (e.g. custom codegen plugins or wrappers) will fail to compile after this change. Similarly, `Constants.SecuritySchemeProviderNames []string` was renamed to `Constants.SecuritySchemeProviders []SecuritySchemeProvider` in `schema.go`, breaking any external code that constructs or reads `Constants` by field name.

Both are exported symbols with no prior deprecation notice. If there are known external consumers of this API surface, a compatibility shim or a CHANGELOG note is worth adding; if `pkg/codegen` is intentionally considered an unstable API, a comment or doc update would prevent future confusion.

### Issue 2 of 2
pkg/codegen/templates/constants.tmpl:1-11
**Template override users need to migrate to `.SecuritySchemeProviders`**

The data passed to this template changed from `.SecuritySchemeProviderNames` (a `[]string`) to `.SecuritySchemeProviders` (a `[]SecuritySchemeProvider`). Any project that has overridden `constants.tmpl` via a custom template directory will get a silent empty output (the range loop iterates over a nil/zero-value field) rather than a compile error, potentially hiding the generated constants entirely.

Reviews (1): Last reviewed commit: "Share security scheme context keys acros..." | Re-trigger Greptile

Comment thread pkg/codegen/codegen.go
Comment thread pkg/codegen/templates/constants.tmpl
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label Jul 12, 2026
@mromaszewicz
mromaszewicz merged commit 5b7486a into main Jul 12, 2026
30 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-2383 branch July 12, 2026 15:21
@mromaszewicz mromaszewicz removed the notable changes Used for release notes to highlight these more highly label Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent security scheme context keys across multiple specs when using shared YAML

1 participant