Thanks to visit codestin.com
Credit goes to github.com

Skip to content

contentMediaType on multipart file fields generates string instead of Blob | File (OpenAPI 3.1 / FastAPI) #344

@pas-shubrat-saha

Description

@pas-shubrat-saha

Summary

When an OpenAPI 3.1 spec describes a multipart file upload using contentMediaType (the OpenAPI 3.1 pattern), @openapi-codegen/typescript generates string instead of Blob (or Blob | File). This breaks TypeScript consumers that pass File objects to generated mutation hooks.

format: binary is handled correctly and maps to Blob.

Environment

  • @openapi-codegen/cli: 3.1.0
  • @openapi-codegen/typescript: 11.1.0
  • OpenAPI source: FastAPI app (app.openapi()), OpenAPI 3.1
  • FastAPI: ≥ 0.129 (uses contentMediaType for UploadFile instead of format: binary)

Reproduction

OpenAPI schema (multipart upload):

{
  "components": {
    "schemas": {
      "Body_upload_proposal_document": {
        "type": "object",
        "required": ["document", "opportunity_id"],
        "properties": {
          "document": {
            "type": "string",
            "contentMediaType": "application/octet-stream",
            "description": "Proposal document file"
          },
          "opportunity_id": {
            "type": "string"
          }
        }
      }
    }
  }
}

Generated today:

export type BodyUploadProposalDocument = {
  document: string;  // ❌
  opportunity_id: string;
};

Expected:

export type BodyUploadProposalDocument = {
  document: Blob | File;  // or Blob
  opportunity_id: string;
};

Consumer code that fails typecheck:

await uploadMutation.mutateAsync({
  body: {
    document: file, // File — TS2322: Type 'File' is not assignable to type 'string'
    opportunity_id: opportunityId,
  },
});

Context

Suggested fix

In schema type generation, when a property has:

  • type: string (or no explicit type), and
  • contentMediaType matching a file-like MIME type (application/octet-stream, image/*, etc.),

treat it the same as format: binary and emit Blob or Blob | File.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions