Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
46fe1f1
fix: in GenerateGoSchema(), if a type isn't resolved, the error is sw…
perbu Jan 15, 2022
b9268e3
Merge branch 'oapi-codegen:main' into master
perbu Sep 18, 2024
16e0888
first stab at a streaming example.
perbu Sep 18, 2024
b07e6bf
generate flushing code if content-type is text/event-stream.
perbu Sep 19, 2024
ca59293
cleanup streaming example.
perbu Sep 19, 2024
ea8e762
go mod tidy
perbu Sep 19, 2024
694b38a
comment cleanup.
perbu Sep 19, 2024
0d1d47f
cleanup after changing the build dependency to 1.21.
perbu Sep 19, 2024
0862b28
try to match the tmpl style of the rest of the template.
perbu Sep 19, 2024
67068ea
changes in template introduced a blank line in the generated code.
perbu Sep 19, 2024
a34c647
add a basic README.
perbu Sep 19, 2024
030070b
remove toolchain directive
perbu Sep 19, 2024
d7bb13b
add the tools package to help fix deps.
perbu Sep 19, 2024
22d95cf
add a test for a streaming endpoint.
perbu Sep 20, 2024
e4ffe33
update generated code in tests
perbu Sep 20, 2024
8c6473a
add the streaming endpoint to all the strict servers
perbu Sep 20, 2024
e36d547
Merge branch 'main' of github.com:oapi-codegen/oapi-codegen into stre…
perbu Sep 23, 2024
70d4fc9
update mod
perbu Sep 23, 2024
079879f
update mod
perbu Sep 23, 2024
edef8d8
updated generate code due to upstream changes.
perbu Sep 23, 2024
ceb7db1
Undo changes to strict-server example
mromaszewicz Apr 21, 2026
7c45ede
Merge remote-tracking branch 'upstream/main' into pr/1765
mromaszewicz Apr 21, 2026
5724c8d
Add a streaming client to example
mromaszewicz Apr 21, 2026
799a2e2
Fix lint issues
mromaszewicz Apr 21, 2026
a690377
Parameterize streaming media types
mromaszewicz Apr 21, 2026
fdd8f23
streaming support in fiber/iris
mromaszewicz Apr 21, 2026
117d254
fix slog key typo in streaming example
mromaszewicz Apr 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@
"type": "string"
}
},
"streaming-content-types": {
"type": "array",
"description": "Additional regex patterns matched against response Content-Type to decide when the strict server should generate a flush-per-chunk streaming response. Merged with the defaults (text/event-stream, application/jsonl, application/x-ndjson). Invalid regexes fail configuration validation.",
"items": {
"type": "string"
}
},
"nullable-type": {
"type": "boolean",
"description": "Whether to generate nullable type for nullable fields"
Expand Down
17 changes: 17 additions & 0 deletions examples/streaming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
OpenAPI Code Generation Example - Streaming
-------------------------------------------

This directory contains an example server using our code generator which implements
a simple streaming API with a single endpoint.

This is the structure:
- `sse.yaml`: Contains the OpenAPI 3.0 specification
- `stdhttp/`: Contains the written and generated code for the server using the standard http package
- `client/`: Contains a client which reads the server stream and prints out the messages

You can run both together to demonstrate the end-to-end behavior from
both client and server side. Run these commands in parallel:

go run ./stdhttp
go run ./client

52 changes: 52 additions & 0 deletions examples/streaming/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"bufio"
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/oapi-codegen/oapi-codegen/v2/examples/streaming/client/sse"
)

func main() {
serverURL := flag.String("url", "http://localhost:8080", "server base URL")
flag.Parse()

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

client, err := sse.NewClient(*serverURL)
if err != nil {
slog.Error("NewClient failed", "error", err)
os.Exit(1)
}

// Use the plain Client (not ClientWithResponses) so the response body stays
// an open io.Reader — ClientWithResponses would io.ReadAll the stream.
resp, err := client.GetStream(ctx)
if err != nil {
slog.Error("GetStream failed", "error", err)
os.Exit(1)
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
slog.Error("unexpected status", "status", resp.Status)
os.Exit(1)
}

scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil && ctx.Err() == nil {
slog.Error("scan failed", "error", err)
os.Exit(1)
}
}
6 changes: 6 additions & 0 deletions examples/streaming/client/sse/cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: sse
output: streaming.gen.go
generate:
client: true
models: true
3 changes: 3 additions & 0 deletions examples/streaming/client/sse/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sse

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml ../../sse.yaml
222 changes: 222 additions & 0 deletions examples/streaming/client/sse/streaming.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions examples/streaming/sse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.0.0
info:
title: Simple JSONL Streaming Service
version: 1.0.0
paths:
/:
get:
summary: JSON Lines Stream
description: Provides a stream of JSON documents (one per line, application/jsonl) containing a timestamp and sequence number.
operationId: getStream
responses:
200:
description: JSONL Stream
content:
application/jsonl:
schema:
type: object
properties:
time:
type: string
format: date-time
description: Timestamp of the event.
sequence:
type: integer
description: Sequence number of the event.
example:
time: "2023-11-20T10:30:00Z"
sequence: 1
36 changes: 36 additions & 0 deletions examples/streaming/stdhttp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
SHELL:=/bin/bash

YELLOW := \e[0;33m
RESET := \e[0;0m

GOVER := $(shell go env GOVERSION)
GOMINOR := $(shell bash -c "cut -f2 -d. <<< $(GOVER)")

define execute-if-go-122
@{ \
if [[ 22 -le $(GOMINOR) ]]; then \
$1; \
else \
echo -e "$(YELLOW)Skipping task as you're running Go v1.$(GOMINOR).x which is < Go 1.22, which this module requires$(RESET)"; \
fi \
}
endef

lint:
$(call execute-if-go-122,$(GOBIN)/golangci-lint run ./...)

lint-ci:

$(call execute-if-go-122,$(GOBIN)/golangci-lint run ./... --out-format=colored-line-number --timeout=5m)

generate:
$(call execute-if-go-122,go generate ./...)

test:
$(call execute-if-go-122,go test -cover ./...)

tidy:
$(call execute-if-go-122,go mod tidy)

tidy-ci:
$(call execute-if-go-122,tidied -verbose)
Loading
Loading