Skip to content

[BUG][typescript-fetch] any-typed multipart form parameter emits unqualified anyToJSON (does not compile) #24552

Description

@donald

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

A multipart/form-data parameter that has no type at all ("any type") makes typescript-fetch
emit a call to a bare anyToJSON(...). The generated apis/*.ts only imports the runtime as
* as runtime, so that identifier does not exist and the generated code does not compile.

The free-form-object case is already handled — runtime.anyToJSON — but the any-type case falls through
to {{dataType}}ToJSON, and for an any-typed parameter dataType is any, so it renders as anyToJSON.

openapi-generator version

7.25.0-SNAPSHOT, built from master at commit 4aed9c1c635.

OpenAPI declaration file content or url
openapi: 3.0.3
info: { title: t, version: 1.0.0 }
paths:
  /upload:
    post:
      operationId: upload
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file: { type: string, format: binary }
                meta: { description: can be any type }   # no type -> "any type"
      responses: { "200": { description: ok } }
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
  -i spec.yaml -g typescript-fetch -o out
Steps to reproduce
  1. Save the spec above as spec.yaml
  2. Run the command above
  3. Look at out/apis/DefaultApi.ts

Actual output

import * as runtime from '../runtime';
...
formParams.append('meta', new Blob([JSON.stringify(anyToJSON(requestParameters['meta']))], { type: "application/json", }));
//                                                 ^^^^^^^^^ not imported, not defined

tsc fails with TS2304: Cannot find name 'anyToJSON'.

Expected output

formParams.append('meta', new Blob([JSON.stringify(runtime.anyToJSON(requestParameters['meta']))], { type: "application/json", }));

anyToJSON is exported by runtime.ts (added in #1877) and is already referenced correctly for
free-form objects; only the any-type branch is missing the namespace qualifier.

Suggest a fix

https://github.com/OpenAPITools/openapi-generator/blob/4aed9c1c635/modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache#L288

{{#isFreeFormObject}}runtime.anyToJSON{{/isFreeFormObject}}{{^isFreeFormObject}}{{{dataType}}}ToJSON{{/isFreeFormObject}}

Adding an isAnyType branch that also uses runtime.anyToJSON fixes it.

I have a patch with a regression test ready and will open a PR shortly.

Related issues/PRs

#1877 added anyToJSON and the isFreeFormObject branch. I could not find an existing report covering the
any-type case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions