Skip to content

feat: support string SSE event IDs - #1086

Open
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:feat/1040-sse-string-event-ids
Open

feat: support string SSE event IDs#1086
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:feat/1040-sse-string-event-ids

Conversation

@stareezy-1

@stareezy-1 stareezy-1 commented Aug 5, 2026

Copy link
Copy Markdown

Summary

Fixes #1040

Problem

The SSE specification describes the event ID as a string (WHATWG: "event ID string"), but sse.Message only models it as a non-zero integer:

type Message struct {
	ID    int
	Data  any
	Retry int
}

This prevents spec-valid use cases such as UUIDs, opaque cursor tokens, composite identifiers (e.g. stream-123:456), or explicitly using 0 as an event ID — the wire output is only id: <int>, and 0 is silently omitted (if msg.ID > 0).

Fix

Add an IDString field alongside the existing ID (per the approach confirmed by the maintainers in the issue):

  1. IDString, when set, is written to the id: field and takes precedence over ID.
  2. ID is marked deprecated and used only when IDString is empty.
  3. The generated OpenAPI id property is now oneOf: [integer, string].
  4. CR/LF in IDString are stripped — they are SSE line terminators and could otherwise inject extra fields into the stream (same hardening already applied to Comment).

Tests

TestSSE/sse_string_event_ids added to sse_test.go:

Case Verifies
IDString set id: stream-123:456 on the wire
Both ID and IDString set string takes precedence
Only ID set legacy numeric ID unchanged
IDString: "0" zero and other non-int values supported
IDString: "ab\r\ncd" line breaks stripped, no field injection
OpenAPI document id property is oneOf with integer and string

All existing tests continue to pass (go test -race ./...).

The SSE specification allows any string as the event ID, but the Message
struct only accepted a non-zero integer, preventing UUIDs, cursor tokens,
composite keys, and the value 0 from being used. Add an IDString field
that takes precedence over the now-deprecated numeric ID, and document
both types in the generated OpenAPI schema. Line breaks in IDString are
stripped so they cannot inject extra SSE fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strings as SSE event IDs

1 participant