A collection of tools to enable ogen to accommodate some specific spec features.
This repo provides post-processing tools to work around known issues until they're fixed upstream. These tools are designed to be able to be able to run on all code without side effects.
| Tool | Description | Issue |
|---|---|---|
| ogen-fixnull | Fix null handling in Opt* types |
#1358 |
| ogen-fixerror | Preserve error response bodies | - |
| Package | Description |
|---|---|
| ogenerror | Extract status code and body from ogen errors |
Fixes JSON decoding errors when APIs return null for nullable $ref fields.
Install:
go install github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latestUse:
ogen --package api --target internal/api --clean openapi.json
ogen-fixnull internal/api/oas_json_gen.goOr without installing:
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest internal/api/oas_json_gen.goSee cmd/ogen-fixnull/README.md for detailed documentation.
Preserves error response bodies so they can be read after the response is closed.
Problem: ogen's UnexpectedStatusCodeError contains the *http.Response, but the body gets closed by defer resp.Body.Close() before callers can read it.
Use:
ogen --package api --target internal/api --clean openapi.json
ogen-fixerror internal/api/oas_response_decoders_gen.goOr without installing:
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixerror@latest internal/api/oas_response_decoders_gen.goExtract error details from ogen client errors:
import "github.com/agentplexus/ogen-tools/ogenerror"
resp, err := client.SomeMethod(ctx, req)
if err != nil {
if status := ogenerror.Parse(err); status != nil {
fmt.Printf("Status: %d, Body: %s\n", status.StatusCode, status.Body)
}
}See ogenerror/README.md for detailed documentation.
#!/bin/bash
set -e
# Prerequisites:
# go install github.com/ogen-go/ogen/cmd/ogen@latest
# Generate API code
ogen --package api --target internal/api --clean openapi.json
# Post-process: Fix ogen bugs
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest internal/api/oas_json_gen.go
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixerror@latest internal/api/oas_response_decoders_gen.go
# Verify
go build ./...Found another ogen issue that needs a workaround? PRs welcome.
MIT