[typescript-fetch] BlobApiResponse returns an HttpFile, keeps a non-native Blob intact, tokenizes Content-Disposition

Merged
AN
2mo ago
+1258-1468

Follow-up to #24957 (implements #24956), not yet released.

Regression fixed

BlobApiResponse.value() wrapped whatever raw.blob() returned in new File([blob]). When the Blob is not the native one, which is what an injected fetch implementation returns (new Configuration({ fetchApi: nodeFetch }), cross-fetch, isomorphic-fetch), the File constructor stringifies it: on Node.js 20+ every file download became a 13-byte body reading [object Blob]. Reproduced with the node-fetch dependency of samples/client/petstore/typescript-fetch/tests/default.

The File is now built only from a native Blob, where File exists and can be constructed. Otherwise the Blob itself is given the name, the way the typescript generator's getBodyAsFile() already falls back.

Type

value() was typed Promise<File>, which the fallback branches did not honour. It is now Promise<HttpFile>, with

export type HttpFile = Blob & { readonly name: string };

exported from the runtime: a File wherever one can be built, a named Blob otherwise, name present in every case. The name and shape are those of the typescript generator's browser HttpFile. Operations with a file response are typed runtime.HttpFile instead of Blob (Promise<runtime.HttpFile>, ApiResponse<runtime.HttpFile>, and inside content-type variant unions), through a new apisReturnType.mustache partial used by apis.mustache. HttpFile is a subtype of Blob, so existing callers keep compiling and can now read .name without a cast. The doc template still shows Blob; sagas.mustache is untouched, as neither imports the runtime.

Parsing

The Content-Disposition parameters are tokenized by one global regex that consumes a name=value at a time, replacing the character loop of #24957: a quoted-string is taken whole (; included), and the two malformed forms the loop got wrong are tolerated (filename="a.pdf" junk gave a.pdf" junk; a trailing backslash lost the parameter).

Tests

Added to tests/default/test/BlobApiResponse.ts: a node-fetch Response (name set, body intact, not wrapped), a global File that throws Illegal constructor, and the malformed quoted-strings. The Node.js < 20 test now asserts the name too.

How to validate

./bin/generate-samples.sh bin/configs/typescript-fetch*.yaml   # no diff beyond the committed samples
cd samples/client/petstore/typescript-fetch/tests/default && npm install && npm test

Files: typescript-fetch/runtime.mustache, apis.mustache, the new apisReturnType.mustache, the 24 regenerated runtime.ts, the 3 API files of split-by-content-type (the only sample with file responses), and the test.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Commit all changed files.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328