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)
Steps To Reproduce
Add a
text/event-streamresponse with a plain stringexample:Run the generator against this spec (e.g.
genOpenApiTSCode).The current behavior
Validation fails with:
The expected behavior
Per the OpenAPI 3.x spec, Media Type Object's
examplefield 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, theanydefinition is incorrectly restricted toobject:mediaType.properties.example(and a few otherexamplefields) reference this broken definition:so any non-object
examplevalue fails validation withmust 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'sexamples.<name>.value, which switched the$reffrom#/definitions/anyto#/definitions/defaultType). This time it'smediaType.properties.examplethat 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:
There are currently 4 places in
src/internal/Validator/openapi.jsonthat still reference#/definitions/anyfor anexample-like field and would need the same fix:exampleexample(the one causing this issue)examplevariantanyOrExpression(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)