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
Return typed server-sent events. Generate the OpenAPI document. Check the per-event item schema. Test the client generator.
Return typed server-sent events. Generate the OpenAPI document. Check the per-event item schema. Test the client generator.

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.

Sample.csproj
<Project Sdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net11.0</TargetFramework><OutputType>Exe</OutputType><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><RootNamespace>Sample</RootNamespace></PropertyGroup><ItemGroup><PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="11.0.0" /></ItemGroup></Project>
Program.cs
using System.Net.ServerSentEvents;
using System.Runtime.CompilerServices;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment()) app.MapOpenApi();
app.MapGet("/jobs/stream", (CancellationToken ct) =>
    TypedResults.ServerSentEvents(StreamJobs(ct))).WithName("StreamJobs");
app.Run();

static async IAsyncEnumerable<SseItem<JobEvent>> StreamJobs(
    [EnumeratorCancellation] CancellationToken ct)
{
    yield return new SseItem<JobEvent>(new(17, "queued")) { EventId = "1" };
    await Task.Delay(250, ct);
    yield return new SseItem<JobEvent>(new(17, "complete")) { EventId = "2" };
}

public sealed record JobEvent(int JobId, string State);

Run in Development and inspect both endpoints.

BASH
ASPNETCORE_ENVIRONMENT=Development dotnet run --urls http://localhost:5102
curl -fsS http://localhost:5102/openapi/v1.json
curl -iN http://localhost:5102/jobs/stream

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