swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

[Bug] type: ["null", "object"] with additionalProperties generates object instead of Record<string, string>

Open 7amou3 opened this issue 3 weeks ago • 0 comments

Summary

swagger-typescript-api does not correctly interpret OpenAPI 3.1 schemas that use a nullable object type expressed as a union:

{
  "type": ["null", "object"],
  "additionalProperties": { "type": "string" }
}

This is valid OpenAPI 3.1 syntax, and indicates: a nullable object whose keys are strings and whose values are strings.

However, the current generator outputs:

  labels?: null | object;

Which loses all typing information.

Expected Output

labels?: Record<string, string> | null;

Actual Output

labels?: null | object;

Why This Is Incorrect

  • "object" with "additionalProperties": { "type": "string" } should always map to Record<string, string>.

  • Wrapping "type": ["null", "object"] should become Record<string, string> | null.

  • Instead, the union collapses to generic object, discarding key/value types.

Minimal Reproduction

{
  "openapi": "3.1.0",
  "components": {
    "schemas": {
      "Test": {
        "type": ["null", "object"],
        "additionalProperties": { "type": "string" }
      }
    }
  }
}

Generated TypeScript:

export type Test = null | object;

Expected:

export type Test = Record<string, string> | null;

Root of the Issue

The generator appears to:

  • Recognize "type": "object" case

  • But not recognize "type": ["object"] or "type": ["null", "object"] cases

  • And therefore falls back to generic object

This breaks any nullable dictionary schema.

Impact

All APIs using OpenAPI 3.1 unions for nullable dictionaries generate unsafe and incorrect TypeScript definitions

7amou3 avatar Nov 30 '25 15:11 7amou3