[typescript-fetch] name downloaded files from Content-Disposition, BlobApiResponse returns a File

Merged
AN
2mo ago
+2107-50

Implements #24956

Changes

For a file response (isResponseFile), BlobApiResponse.value() returned an anonymous Blob: the name the server
advertises in Content-Disposition was dropped, and every client wanting to save a download under its real name hadto call the xxxRaw() variant and parse the header itself. Every other runtime of this repository already names the download for the user (typescript generator's getBodyAsFile(), Java prepareDownloadFile, Python __deserialize_file, C# ClientUtils).

BlobApiResponse.value() now returns a File:

  • named after Content-Disposition, RFC 5987 filename*=UTF-8''… form first, plain filename= otherwise, empty name when the header is absent;
  • typed with the blob's type (the response Content-Type).

The parsing lives in an exported parseContentDispositionFilename(headers: Headers) (RFC 6266): parameters are split on ; outside quoted-strings and matched by name case-insensitively, filename* wins over filename and falls back to it when malformed or not UTF-8. The name is reduced to its last path segment (/ or \), as the Java and Python runtimes already do, so a client writing file.name to disk cannot be steered outside its directory; ., .. and an empty name give no name.

Backward compatible: File extends Blob, so the generated methods still satisfy their declared Promise<Blob> return type and existing callers keep working; callers that want the name read file.name. File is a global in browsers and Node.js 20+ (the CI matrix for these samples runs Node 20).

Files: typescript-fetch/runtime.mustache, and the 24 regenerated runtime.ts samples (identical diff in each).

How to validate

./bin/generate-samples.sh bin/configs/typescript-fetch*.yaml   # no diff beyond the committed runtime.ts files

Behaviour, with the regenerated samples/client/petstore/typescript-fetch/builds/default/runtime.ts transpiled and run on Node 22: parseContentDispositionFilename on filename="invoice.pdf", filename=plain.csv, filename*=UTF-8''facture%20%C3%A9t%C3%A9.pdf, both forms together (encoded wins), inline and no header; BlobApiResponse.value() on a Response with Content-Type: application/pdf and Content-Disposition: attachment; filename="doc.pdf" gives a File named doc.pdf of type application/pdf with the original body, and an unnamed response gives an empty name. tsc --strict --lib dom passes on the regenerated runtime.

PR checklist

  • [ X ] Read the contribution guidelines.
  • [ X ] 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
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • [ X ] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04) @joscha (2024/10) @KannaKim (2026/07)


Summary by cubic

Makes BlobApiResponse.value() return a File named from the Content-Disposition header instead of an anonymous Blob, so downloaded files keep the server-advertised name. Implements #24956.

Backward compatibility

  • File extends Blob, so existing callers keep working and can read the name from file.name.
  • Runtimes without a global File (Node.js before 20) still receive the bare Blob.
  • The exported parseContentDispositionFilename(headers) follows RFC 6266: filename* first, then filename, with case-insensitive parameter matching and quoted-string support.
  • Adds unit tests for the parser and BlobApiResponse to the default test project.