Support OpenAPI apiKey security (header/query/cookie) in generated route types and $.auth (and resolve merge conflicts with main)#2045
Conversation
apiKey security in generated route types and $.auth
pmcelhaney
left a comment
There was a problem hiding this comment.
Make sure we're implementing the spec correctly. For example, the key can also be in a cookie.
Implemented in ca9906e. |
apiKey security in generated route types and $.authapiKey security (header/query/cookie) in generated route types and $.auth
|
@copilot resolve the merge conflicts in this pull request |
Resolved in |
apiKey security (header/query/cookie) in generated route types and $.authapiKey security (header/query/cookie) in generated route types and $.auth (and resolve merge conflicts with main)
Summary
Counterfact previously ignored OpenAPI
securitySchemesforapiKey, so generated handlers did not require API key inputs and could not access them via auth context. This change makesapiKeybehave like a first-class operation input and exposes it as$.auth.apiKey.Generator: synthesize required API key params
OperationTypeCoderderives required synthetic parameters fromcomponents.securitySchemesentries wheretype: "apiKey"andin: "header" | "query" | "cookie".headers/query/cookietypes include required API key fields without duplicate keys.Generator: typed auth context
$arg includesauth.authisneverwhen no auth scheme exists,{ username?: string; password?: string }for basic auth,{ apiKey?: string }for API key, or a merged shape when both exist.Runtime: resolve and expose API key
OpenApiDocumentloads/storescomponents.securitySchemes.Dispatcher:auth.apiKey.Runtime: validate cookie API keys
cookieparameters so missing cookie-based API keys fail consistently with header/query-based keys.Merge conflict resolution with latest
mainoperation-type-coderby preserving both:querystringparameter typing introduced onmain, andauthtyping introduced in this PR.Original Prompt
Support for API key
Counterfact doesn't currently read the OpenAPI security object of the OpenAPI schema.
When generating types, it should take that object into account.
For example, if the security object is defined as:
{ "type": "apiKey", "name": "api_key", "in": "header" }Then Counterfact should act as if every operation has a required request header named "api_key" when generating code.
Follow-up feedback:
apiKeybehavior for cookie location as well (in: cookie)Manual acceptance tests
components.securitySchemes.apiKeyAuthastype: apiKey,in: header,name: api_key, generated route types require$.headers.api_key.components.securitySchemes.apiKeyAuthastype: apiKey,in: cookie,name: api_key_cookie, generated route types require$.cookie.api_key_cookie.$.auth.apiKey.$.auth.usernameand$.auth.passwordstill populate fromAuthorization: Basic ....main, generated operation types still include bothquerystringtyping andauthtyping (no regression from conflict resolution).Tasks
OperationTypeCoderand merged synthetic required params into effective operation parameters.in: cookiein addition toheaderandquery.authtyping (basic/apiKey/combined) to operation$argument shape.components.securitySchemes.auth.apiKeyfrom header/query/cookie input.maininto the branch, resolved theoperation-type-coderconflict by keeping bothquerystringandauthfields, and updated related snapshots/tests.