
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.
Packages
| Package |
Description |
| ogenerror |
Extract status code and body from ogen errors |
Quick Start
ogen-fixnull
Fixes JSON decoding errors when APIs return null for nullable $ref fields.
Install:
go install github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest
Use:
ogen --package api --target internal/api --clean openapi.json
ogen-fixnull internal/api/oas_json_gen.go
Or without installing:
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest internal/api/oas_json_gen.go
See cmd/ogen-fixnull/README.md for detailed documentation.
ogen-fixerror
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.go
Or without installing:
go run github.com/agentplexus/ogen-tools/cmd/ogen-fixerror@latest internal/api/oas_response_decoders_gen.go
ogenerror
Extract 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.
Typical generate.sh
#!/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 ./...
Contributing
Found another ogen issue that needs a workaround? PRs welcome.
License
MIT