Skip to content

Don't generate constants for undefined scopes#2372

Merged
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2367
May 16, 2026
Merged

Don't generate constants for undefined scopes#2372
mromaszewicz merged 1 commit into
mainfrom
fix/issue-2367

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

A spec that lists a scheme under top-level or operation security: without declaring it in components/securitySchemes previously emitted a constant like Api_keyScopes apiKeyContextKey = "api_key.Scopes" while the apiKeyContextKey type itself was never generated. The output failed to compile with undefined: apiKeyContextKey, and per-framework middleware templates emitted matching dangling context.WithValue(ctx, Api_keyScopes, ...) references when servers were generated.

Filter operation SecurityDefinitions against the set of schemes actually defined in components/securitySchemes in OperationDefinitions. Both the constants block and the per-framework middleware now stay in sync with the types that get emitted, so generated code compiles whether or not the spec defines every referenced scheme.

Closes: #2367

A spec that lists a scheme under top-level or operation `security:` without
declaring it in `components/securitySchemes` previously emitted a constant
like `Api_keyScopes apiKeyContextKey = "api_key.Scopes"` while the
`apiKeyContextKey` type itself was never generated. The output failed to
compile with `undefined: apiKeyContextKey`, and per-framework middleware
templates emitted matching dangling `context.WithValue(ctx, Api_keyScopes,
...)` references when servers were generated.

Filter operation `SecurityDefinitions` against the set of schemes actually
defined in `components/securitySchemes` in `OperationDefinitions`. Both
the constants block and the per-framework middleware now stay in sync with
the types that get emitted, so generated code compiles whether or not the
spec defines every referenced scheme.

Closes: #2367

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mromaszewicz mromaszewicz requested a review from a team as a code owner May 16, 2026 15:57
@greptile-apps

greptile-apps Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR keeps generated security-scope constants in sync with declared OpenAPI security schemes. The main changes are:

  • Filters operation security definitions to schemes present in components/securitySchemes.
  • Prevents middleware templates from referencing scope constants for undefined schemes.
  • Adds an issue-2367 regression fixture with generated gin and strict-server output.
  • Prunes stale checksum entries from test and example module sums.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • The filtering happens in the shared operation model, so all middleware templates consume the same corrected security metadata.
  • The generated fixture matches the intended compile fix for an undefined security scheme.

Important Files Changed

Filename Overview
pkg/codegen/operations.go Adds central filtering for undefined security schemes before constants and middleware templates consume operation security metadata.
internal/test/issues/issue-2367/spec.yaml Adds a minimal spec that references a security scheme without defining it.
internal/test/issues/issue-2367/api.gen.go Adds generated output showing the undefined scheme no longer creates dangling scope constants or middleware references.

Reviews (1): Last reviewed commit: "Don't generate constants for undefined s..." | Re-trigger Greptile

@mromaszewicz mromaszewicz merged commit 52acac4 into main May 16, 2026
28 checks passed
@mromaszewicz mromaszewicz deleted the fix/issue-2367 branch May 16, 2026 16:00
Comment thread examples/go.sum
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why are these being removed? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't know. I run "make tidy" before staging every commit, and I don't ask questions when 'go.sum' changes, because it's never wrong :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's because I broke CI with 35a6e0c :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fix incoming

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jamietanna jamietanna May 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(Looks like the first recent commit that didn't run make tidy was fbc8e0d, FYI)

jamietanna added a commit to oapi-codegen/actions that referenced this pull request May 17, 2026
As noted in oapi-codegen/oapi-codegen#2372,
otherwise this leads to cases where we're not correctly `go mod tidy`ing
across the project.

As all our projects have a `tidy-ci`, we can use that task.
jamietanna added a commit to oapi-codegen/actions that referenced this pull request May 17, 2026
As noted in oapi-codegen/oapi-codegen#2372,
otherwise this leads to cases where we're not correctly `go mod tidy`ing
across the project.

As all our projects have a `tidy-ci`, we can use that task.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

undefined security scheme context key type when securitySchemes component is missing

2 participants