Repository navigation
Allow Response return from mock handlers for non-JSON, non-204 responses - #394
Conversation
|
@claude review |
There was a problem hiding this comment.
LGTM. This is a small, type-only widening of the generated MSW handler return types, plus a patch version bump.
What was reviewed:
- The new
isNoContentfield. It is set only ingetOperations, which is the only place anOperationobject is built, so no other construction site is missing the field. - The return-type choice in
msw-handlers.ts. Operations with a 204 response keepStatusCode. Operations with no described response getStatusCode | Response. The existing dispatcher already passesResponseresults through at runtime. - The snapshot. It matches the generator output for the nine affected handlers and the new
StatusOrResponsealias.
Extended reasoning...
The change touches the TypeScript code generator for MSW mock handlers (base.ts and msw-handlers.ts), its regenerated snapshot, and a patch version bump. It has no security-sensitive surface; it only affects types in the generated mock-API code. For endpoints whose success response is undescribed, the handler return type now also allows Response. The runtime wrapper already handled Response results, so generated behaviour does not change. The diff is small and mechanical, there are no CODEOWNERS files, and no reviewer objections are open.
| queryParams, | ||
| bodyType: contentRef(conf.requestBody), | ||
| successType: contentRef(getSuccessResponse(conf.responses)), | ||
| isNoContent: "204" in conf.responses, |
There was a problem hiding this comment.
Rather than putting isNoContent on here, I would put successCode and check successCode === 204 in the calling code. That way this code doesn't need to know what the MSW generator cares about. 🤖 suggests:
const [successCode, successResponse] =
Object.entries(conf.responses).find(([code]) => !/^[45]/.test(code)) ?? [];or maybe this if we want to blow up if there's ever more than one (I like it):
const successes = Object.entries(conf.responses).filter(
([code]) => !/^[45]/.test(code),
);
if (successes.length !== 1) {
throw new Error(
`Expected one non-error response for ${conf.operationId}, got ${successes.length}`,
);
}
const [[successCode, successResponse]] = successes;getSuccessResponse can go away.
There was a problem hiding this comment.
Nice. That does seem more direct / transparent.
| { | ||
| "name": "@oxide/openapi-gen-ts", | ||
| "version": "0.16.0", | ||
| "version": "0.16.1", |
There was a problem hiding this comment.
This should be a minor bump because it is a semantic change to generated output.
https://github.com/oxidecomputer/oxide.ts/tree/main/oxide-openapi-gen-ts#versioning-scheme
david-crespo
left a comment
There was a problem hiding this comment.
Looks good pending the two things.
| `Expected one non-error response for ${conf.operationId}, got ${success ? rest.length + 1 : 0}`, | ||
| ); | ||
| } | ||
| const [successCode, successResponse] = success; |
There was a problem hiding this comment.
Nice, yeah the [[ ... ]] in my suggestion was horrible
Closes #386
Mock handlers for endpoints without a JSON success type had the return type
Promisable<StatusCode>, but some of those endpoints need headers or a body in the response. Console'ssupportBundleHeadhandler returns anHttpResponsewithContent-TypeandContent-Length, and it needed a@ts-expect-errorto do that. (See here.) The dispatcher already passesResponseinstances through at runtime, so accepting only the StatusCode (in the handlers where we needed the response) was too narrow.The generator now makes a distinction between two cases. Operations with a 204 success response keep
Promisable<StatusCode>. Operations that respond with something other than JSON or a 204 (binary download, HEAD, redirect) getPromisable<StatusOrResponse>, whereStatusOrResponseis a new alias forStatusCode | Responsein the generated file.To support this,
Operationnow has asuccessCodefield (the key of the success response in the spec, e.g."200","204","default"), and the MSW generator checkssuccessCode === "204".getOperationsnow throws if an operation does not have exactly one non-error response; this replacesgetSuccessResponse, which picked the first of 200, 201, 202 and 204 without a check. Every operation in the current Nexus spec has exactly one, so the generated client is unchanged.In the current Nexus spec, nine handlers get the new result type; others are unchanged.
supportBundleHead,supportBundleHeadFilesupportBundleDownload,supportBundleDownloadFile,supportBundleIndexdeviceAuthRequest,deviceAccessTokenloginSaml,instanceSerialConsoleStreamWhen we merge this, (and update to the new version in console) we'll be able to remove the
@ts-expect-errorhere.I checked this locally, as I'm working on an OMICRON_VERSION bump as a part of oxidecomputer/console#3211 and I hit an issue with new missing handlers that weren't getting caught by
npm run tscuntil I dug into this. To test: linked the local build ofopenapi-gen-ts(this code) into local console'snode_modules, then rannpm run gen-apiandnpm run tsc. It caught the missing handlers and thentscpassed once I added them in.This also bumps the version to 0.17.0, as the generated handler types change.