OpenAPI 3.2 in ASP.NET Core 11: Document Server-Sent Events
ASP.NET Core 11 can describe the individual events in a server-sent event stream using OpenAPI 3.2 itemSchema. Check both the generated contract and the live stream. They answer different questions: what consumers can expect and what your service sends at runtime.
What changes in OpenAPI 3.2?
An SSE response is a sequence of framed events sent on one HTTP response. With an explicit per-item schema, an API document can describe the data in each event. The ASP.NET Core 11 generator uses the OpenAPI 3.2 shape for supported typed SSE results. It also preserves multiple documented response content types under the same status in supported Minimal API and MVC configurations.
| Layer | Check |
|---|---|
| API handler | Returns the typed SSE result |
| Generated contract | Specifies OpenAPI 3.2, text/event-stream and itemSchema |
| Live response | Includes event framing and the expected payload |
| Consumer tools | Parse the document and handle incremental events |
Build a typed stream
Use .NET 11 and Microsoft.AspNetCore.OpenApi 11.0.0. The sample emits two events representing one job’s lifecycle. It forwards request cancellation to the delay between events. The OpenAPI endpoint is restricted to Development in this sample.
Run in Development and inspect both endpoints.
Look under paths, the job stream GET operation, responses, and text/event-stream. Expect an itemSchema describing event data. Compare it with the two emitted events. The response should include event IDs and JSON data for queued and complete. Plain IAsyncEnumerable returned directly can be serialized as a JSON response. Use TypedResults.ServerSentEvents so the runtime and generated metadata match the intended stream.
Compatibility and troubleshooting
OpenAPI 3.2 is the default for the documented ASP.NET Core 11 generation path. If your gateway or client generator rejects it, configure OpenApiVersion to OpenApi3_1 in AddOpenApi and run the complete pipeline again. A 3.1 document may not describe the same 3.2-specific detail. Coordinate a contract change before upgrading all clients.
Binary payloads need their own content types and verification. The fact that OpenAPI can express a binary format does not mean every response type automatically gets the desired metadata. In MVC, describe actual controller responses with response metadata and inspect the generated contract.
If events arrive only when the response closes, examine intermediary buffering and compression settings. Check idle timeouts on long-lived streams and disconnect cleanup. In production, document retry and replay behavior before assuming the EventId alone enables resuming from the last event.
Add a contract gate in CI
Save the generated document as a CI artifact. Assert that the document version starts with 3.2, the GET path is present, and the 200 response advertises text/event-stream. Resolve the referenced schema in itemSchema before checking field names. An OpenAPI generator changing the placement of a reference can leave the effective schema unchanged.
Run a separate client smoke test with buffering disabled and a small deadline. Assert both event IDs and payloads. A contract parser passing only tells you that it could read the document. A stream integration test should prove that your actual client handles incremental input and cancellation correctly.
Local verification loaded a 3.2 document containing itemSchema and read both queued and complete events. External gateways and code generators were not part of the test.
FAQ
Is OpenAPI 3.2 required to run SSE? SSE is an HTTP response protocol. OpenAPI 3.2 adds a way to document each event payload.
Should the OpenAPI document be public? Decide based on the service’s contract and access policy. The sample exposes it only in Development.
Can I keep a 3.1 document while testing 3.2? Yes, if your API documentation setup supports separate versions and consumers know which document to use.
See the ASP.NET Core 11 release notes, the OpenAPI generation guide, and the ASP.NET PB API versioning tutorial.
Comments
Post a Comment