diff --git a/dist/unstable/httpapi/HttpApiSchema.js b/dist/unstable/httpapi/HttpApiSchema.js index c51851b..2100420 100644 --- a/dist/unstable/httpapi/HttpApiSchema.js +++ b/dist/unstable/httpapi/HttpApiSchema.js @@ -151,7 +151,7 @@ export const StreamSse = options => { const events = options.events ?? (options.data === undefined ? undefined : Schema.Struct({ id: Schema.UndefinedOr(Schema.String), event: Schema.String, - data: Schema.fromJsonString(options.data) + data: sseDataJsonSchema(options.data) })); if (events === undefined) { throw new Error("StreamSse requires either an events schema or a data schema"); @@ -166,6 +166,14 @@ export const StreamSse = options => { error: options.error ?? Schema.Never }); }; +const sseDataJsonSchema = data => { + const identifier = SchemaAST.resolveIdentifier(data.ast); + return identifier === undefined ? Schema.fromJsonString(data) : Schema.fromJsonString(data).annotate({ + // The SSE transport field is a JSON string. Give that wrapper its own + // OpenAPI identifier so it does not claim the decoded data schema's name. + identifier: `${identifier}Stream` + }); +}; /** * Creates a streaming `Uint8Array` success response schema. * diff --git a/src/unstable/httpapi/HttpApiSchema.ts b/src/unstable/httpapi/HttpApiSchema.ts index aae6cd5..f05e3ed 100644 --- a/src/unstable/httpapi/HttpApiSchema.ts +++ b/src/unstable/httpapi/HttpApiSchema.ts @@ -407,7 +407,7 @@ export const StreamSse: { const events = options.events ?? (options.data === undefined ? undefined : Schema.Struct({ id: Schema.UndefinedOr(Schema.String), event: Schema.String, - data: Schema.fromJsonString(options.data) + data: sseDataJsonSchema(options.data) })) if (events === undefined) { throw new Error("StreamSse requires either an events schema or a data schema") @@ -423,6 +423,15 @@ export const StreamSse: { }) } +const sseDataJsonSchema = (data: Schema.Constraint) => { + const identifier = SchemaAST.resolveIdentifier(data.ast) + return identifier === undefined ? Schema.fromJsonString(data) : Schema.fromJsonString(data).annotate({ + // The SSE transport field is a JSON string. Give that wrapper its own + // OpenAPI identifier so it does not claim the decoded data schema's name. + identifier: `${identifier}Stream` + }) +} + /** * Creates a streaming `Uint8Array` success response schema. *