diff --git a/CHANGELOG.md b/CHANGELOG.md index 2703a54..1cc8b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,16 @@ Versions before 0.8.0 are in the [git tags](https://github.com/WireCatLabs/cli-b Every entry says what changed as a caller sees it, why, and what to watch for — the rules are [`docs/dev/CONVENTIONS.md`](docs/dev/CONVENTIONS.md#the-changelog). +## Unreleased + +### Fixed + +- **`generate` with `auth` signs in an operation whose spec says `security: []`**, the same as one with no + `security`. Before, the empty list stayed, cli-core read it as "no sign-in", and the call went out without the key: + HubSpot's object specs mark 115 calls so, and each answered 401. An operation keeps its own `security` only when + it signs in by `auth`'s schemes. A CLI that drops an empty `security` from the generated manifest at runtime can + stop once it regenerates on this version. + ## 0.114.0 — 17.11.2026 ### Added diff --git a/docs/standards/kit-surface.md b/docs/standards/kit-surface.md index f4dc7c2..1878e96 100644 --- a/docs/standards/kit-surface.md +++ b/docs/standards/kit-surface.md @@ -87,8 +87,9 @@ From the source the generator reads: - **Effect**: GET and HEAD read, POST, PUT and PATCH write, DELETE is destructive. - **Servers and sign-in**: `servers` (each URL once, as written), `components/securitySchemes` and `security` go into the manifest as `servers`, `securitySchemes` and `security`, and an operation's own `security` (`[]` for one that - needs no sign-in) onto that operation. A CLI then does not copy its base URL or OAuth endpoints by hand. They appear - only when the source has them. A requirement naming a scheme the document lacks stops generation. + needs no sign-in, unless `auth` below replaces it) onto that operation. A CLI then does not copy its base URL or + OAuth endpoints by hand. They appear only when the source has them. A requirement naming a scheme the document lacks + stops generation. ### Corrections a CLI passes to `generate` @@ -126,7 +127,9 @@ From the source the generator reads: parameter the operation does not take stops generation. - **`auth`**: `{ securitySchemes, security, reason }` replaces the source's sign-in, the operations' own requirements included. Yandex Disk's Swagger 1.2 has no scheme, and its header is `Authorization: OAuth `: - `{ oauth: { type: "http", scheme: "OAuth" } }`. HubSpot's spec has no private app token. + `{ oauth: { type: "http", scheme: "OAuth" } }`. HubSpot's spec has no private app token. An operation keeps its own + `security` only when it signs in by `auth`'s schemes. One the source marks as needing no sign-in (`[]`) gets `auth` + as well: HubSpot's object specs mark 115 calls that way, and each answered 401 without the key. ### A strict fake for tests diff --git a/src/codegen/codegen.test.ts b/src/codegen/codegen.test.ts index e681401..eddd5a5 100644 --- a/src/codegen/codegen.test.ts +++ b/src/codegen/codegen.test.ts @@ -6,11 +6,15 @@ import * as v from "valibot" import { afterAll, beforeAll, describe, expect, it } from "vitest" import { type ApiModel, + type ApiOperation, + type AuthOverride, CodegenError, coverageGenerator, generate, identifier, manifestGenerator, + type SecurityRequirement, + signInRequirements, typesGenerator, valibotGenerator, writeArtifacts, @@ -236,6 +240,53 @@ describe("generate", () => { "| `answerOnCallback` | `POST /answers` | `x api answer-on-callback` | write |", ) }) + + it("gives the CLI's auth to an operation the source marks as needing no sign-in, keeping one that uses it", () => { + const base = model({ + securitySchemes: { + hapikey: { type: "apiKey", name: "hapikey", in: "query" }, + token: { type: "oauth2", flows: {} }, + }, + }) + const [template] = base.operations + const securities: Record = { + none: [], + emptyRequirement: [{}], + sourceScheme: [{ hapikey: [] }], + token: [{ token: ["crm.read"] }], + optionalToken: [{}, { token: [] }], + } + const operations = Object.entries(securities).map(([id, security]) => ({ + ...(template as ApiOperation), + id, + binding: { kind: "http" as const, method: "GET" as const, path: `/${id}` }, + security, + })) + const signing = (auth?: AuthOverride) => { + let seen: ApiModel | undefined + const capture = (prepared: ApiModel) => { + seen = prepared + return [] + } + generate({ ...base, operations }, [capture], { banner, ...(auth ? { auth } : {}) }) + return Object.fromEntries( + (seen?.operations ?? []).map((operation) => [operation.id, signInRequirements(operation, seen?.security)]), + ) + } + const auth: AuthOverride = { + securitySchemes: { token: { type: "http", scheme: "bearer" } }, + security: [{ token: [] }], + reason: "x", + } + expect(signing(auth)).toEqual({ + none: [{ token: [] }], + emptyRequirement: [{ token: [] }], + sourceScheme: [{ token: [] }], + token: [{ token: ["crm.read"] }], + optionalToken: [{}, { token: [] }], + }) + expect(signing()).toMatchObject({ none: undefined, emptyRequirement: undefined, sourceScheme: [{ hapikey: [] }] }) + }) }) describe("identifier", () => { diff --git a/src/codegen/openapi.test.ts b/src/codegen/openapi.test.ts index 91daf15..cde0a7e 100644 --- a/src/codegen/openapi.test.ts +++ b/src/codegen/openapi.test.ts @@ -680,7 +680,7 @@ describe("adaptOpenApi reads what a CLI would otherwise write by hand", () => { ]) }) - it("replaces the source's sign-in everywhere with auth, and refuses a requirement naming no scheme", () => { + it("replaces the source's sign-in everywhere with auth, its no-sign-in [] included, and refuses a requirement naming no scheme", () => { const paths = { "/me": { get: { operationId: "getMe", security: [{ bearer: [] }] } }, "/health": { get: { operationId: "health", security: [] } }, @@ -703,7 +703,7 @@ describe("adaptOpenApi reads what a CLI would otherwise write by hand", () => { { banner: [], auth }, ) expect(manifest?.content).not.toContain('"bearer"') - expect(manifest?.content).toContain('"security": []') + expect(manifest?.content).not.toContain('"security": []') expect(coverage?.content).toContain("Not from the source: private app tokens are not in the spec") const dangling = { ...document({}, paths), security: [{ missing: [] }] } expect(() => generate(adaptOpenApi(dangling), [], { banner: [] })).toThrow('security names "missing"') diff --git a/src/codegen/pipeline.ts b/src/codegen/pipeline.ts index 038d8bd..9c1dc99 100644 --- a/src/codegen/pipeline.ts +++ b/src/codegen/pipeline.ts @@ -16,6 +16,7 @@ import type { } from "./model.js" import { identifier, kebab } from "./names.js" import { answerPaths, detectPagination, requestParameters } from "./pagination.js" +import { signInRequirements } from "./sign-in.js" export class CodegenError extends Error { constructor(readonly problems: readonly string[]) { @@ -283,8 +284,10 @@ const applyAuth = (model: ApiModel, auth: AuthOverride): ApiModel => { securitySchemes: auth.securitySchemes, security: auth.security, securityReason: auth.reason, - // A requirement naming the source's own schemes is replaced; `[]`, "no sign-in", stays. + // `[]` is replaced too: `auth` says the source's sign-in is wrong, and a key sent to a public call is ignored while + // a key left off a private one always fails. operations: model.operations.map(({ security, ...operation }) => + signInRequirements({ security }, undefined) && security?.every((requirement) => Object.keys(requirement).every((name) => names.has(name))) ? { ...operation, security } : operation,