Skip to content

Bug: mediaType.example (and other example fields) reject non-object values due to broken #/definitions/any #154

Description

@satojin219

Steps To Reproduce

Add a text/event-stream response with a plain string example:

openapi: 3.0.0
info:
  version: 1.0.0
  title: DEMO
paths:
  /chat:
    get:
      operationId: getChat
      responses:
        "200":
          description: SSE stream
          content:
            text/event-stream:
              schema:
                type: string
              example: |
                event: message
                data: hello

Run the generator against this spec (e.g. genOpenApiTSCode).

The current behavior

Validation fails with:

Correct the validation error before generating the code.
[
  {
    instancePath: '.../content/text~1event-stream/example',
    schemaPath: '#/definitions/any/type',
    keyword: 'type',
    params: { type: 'object' },
    message: 'must be object'
  }
]

The expected behavior

Per the OpenAPI 3.x spec, Media Type Object's example field may be any JSON value (string, number, boolean, array, object, or null), not only an object. The generator should accept it.

Root cause

In src/internal/Validator/openapi.json, the any definition is incorrectly restricted to object:

"any": {
  "type": "object",
  "additionalProperties": true
}

mediaType.properties.example (and a few other example fields) reference this broken definition:

"example": {
  "$ref": "#/definitions/any"
}

so any non-object example value fails validation with must be object.

This is the same class of bug as #9 (Schema Object's properties.<name>.example, fixed in v0.1.6) and the fix in #114 (Parameter Object's examples.<name>.value, which switched the $ref from #/definitions/any to #/definitions/defaultType). This time it's mediaType.properties.example that was missed, and it's still present in the latest release (v2.0.7).

Notably, the correct definition already exists in the same schema file and just isn't reused everywhere:

"defaultType": {
  "oneOf": [
    { "type": "null" },
    { "type": "array" },
    { "type": "object" },
    { "type": "number" },
    { "type": "boolean" },
    { "type": "string" }
  ]
}

There are currently 4 places in src/internal/Validator/openapi.json that still reference #/definitions/any for an example-like field and would need the same fix:

  • Parameter Object example
  • Media Type Object example (the one causing this issue)
  • another Parameter Object example variant
  • anyOrExpression (used by Link Object)

Proposed Fix

Replace $ref: "#/definitions/any" with $ref: "#/definitions/defaultType" in the 4 locations above, consistent with the approach taken in #114.

Environment

  • @himenon/openapi-typescript-code-generator: 2.0.7 (also confirmed on 1.0.9)

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