Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions docs/standards/kit-surface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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 <token>`:
`{ 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

Expand Down
51 changes: 51 additions & 0 deletions src/codegen/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, readonly SecurityRequirement[]> = {
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", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] } },
Expand All @@ -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"')
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down Expand Up @@ -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,
Expand Down
Loading