|
| 1 | +# ogen tools |
| 2 | + |
| 3 | +[![Build Status][build-status-svg]][build-status-url] |
| 4 | +[![Lint Status][lint-status-svg]][lint-status-url] |
| 5 | +[![Go Report Card][goreport-svg]][goreport-url] |
| 6 | +[![Docs][docs-godoc-svg]][docs-godoc-url] |
| 7 | +[![License][license-svg]][license-url] |
| 8 | + |
| 9 | +A collection of tools to make [ogen](https://github.com/ogen-go/ogen) more usable. |
| 10 | + |
| 11 | +ogen is an excellent OpenAPI v3 code generator for Go, but it has some rough edges. This repo provides post-processing tools to work around known issues until they're fixed upstream. |
| 12 | + |
| 13 | +## Tools |
| 14 | + |
| 15 | +| Tool | Description | Issue | |
| 16 | +|------|-------------|-------| |
| 17 | +| [ogen-fixnull](cmd/ogen-fixnull/) | Fix null handling in `Opt*` types | [#1358](https://github.com/ogen-go/ogen/issues/1358) | |
| 18 | +| [ogen-fixerror](cmd/ogen-fixerror/) | Preserve error response bodies | - | |
| 19 | + |
| 20 | +## Packages |
| 21 | + |
| 22 | +| Package | Description | |
| 23 | +|---------|-------------| |
| 24 | +| [ogenerror](ogenerror/) | Extract status code and body from ogen errors | |
| 25 | + |
| 26 | +## Quick Start |
| 27 | + |
| 28 | +### ogen-fixnull |
| 29 | + |
| 30 | +Fixes JSON decoding errors when APIs return `null` for nullable `$ref` fields. |
| 31 | + |
| 32 | +**Install:** |
| 33 | +```bash |
| 34 | +go install github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest |
| 35 | +``` |
| 36 | + |
| 37 | +**Use:** |
| 38 | +```bash |
| 39 | +ogen --package api --target internal/api --clean openapi.json |
| 40 | +ogen-fixnull internal/api/oas_json_gen.go |
| 41 | +``` |
| 42 | + |
| 43 | +**Or without installing:** |
| 44 | +```bash |
| 45 | +go run github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest internal/api/oas_json_gen.go |
| 46 | +``` |
| 47 | + |
| 48 | +See [cmd/ogen-fixnull/README.md](cmd/ogen-fixnull/README.md) for detailed documentation. |
| 49 | + |
| 50 | +### ogen-fixerror |
| 51 | + |
| 52 | +Preserves error response bodies so they can be read after the response is closed. |
| 53 | + |
| 54 | +**Problem:** ogen's `UnexpectedStatusCodeError` contains the `*http.Response`, but the body gets closed by `defer resp.Body.Close()` before callers can read it. |
| 55 | + |
| 56 | +**Use:** |
| 57 | +```bash |
| 58 | +ogen --package api --target internal/api --clean openapi.json |
| 59 | +ogen-fixerror internal/api/oas_response_decoders_gen.go |
| 60 | +``` |
| 61 | + |
| 62 | +**Or without installing:** |
| 63 | +```bash |
| 64 | +go run github.com/agentplexus/ogen-tools/cmd/ogen-fixerror@latest internal/api/oas_response_decoders_gen.go |
| 65 | +``` |
| 66 | + |
| 67 | +### ogenerror |
| 68 | + |
| 69 | +Extract error details from ogen client errors: |
| 70 | + |
| 71 | +```go |
| 72 | +import "github.com/agentplexus/ogen-tools/ogenerror" |
| 73 | + |
| 74 | +resp, err := client.SomeMethod(ctx, req) |
| 75 | +if err != nil { |
| 76 | + if status := ogenerror.Parse(err); status != nil { |
| 77 | + fmt.Printf("Status: %d, Body: %s\n", status.StatusCode, status.Body) |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +See [ogenerror/README.md](ogenerror/README.md) for detailed documentation. |
| 83 | + |
| 84 | +## Typical generate.sh |
| 85 | + |
| 86 | +```bash |
| 87 | +#!/bin/bash |
| 88 | +set -e |
| 89 | + |
| 90 | +# Prerequisites: |
| 91 | +# go install github.com/ogen-go/ogen/cmd/ogen@latest |
| 92 | + |
| 93 | +# Generate API code |
| 94 | +ogen --package api --target internal/api --clean openapi.json |
| 95 | + |
| 96 | +# Post-process: Fix ogen bugs |
| 97 | +go run github.com/agentplexus/ogen-tools/cmd/ogen-fixnull@latest internal/api/oas_json_gen.go |
| 98 | +go run github.com/agentplexus/ogen-tools/cmd/ogen-fixerror@latest internal/api/oas_response_decoders_gen.go |
| 99 | + |
| 100 | +# Verify |
| 101 | +go build ./... |
| 102 | +``` |
| 103 | + |
| 104 | +## Contributing |
| 105 | + |
| 106 | +Found another ogen issue that needs a workaround? PRs welcome. |
| 107 | + |
| 108 | +## License |
| 109 | + |
| 110 | +MIT |
| 111 | + |
| 112 | + [build-status-svg]: https://github.com/agentplexus/ogen-tools/actions/workflows/ci.yaml/badge.svg?branch=main |
| 113 | + [build-status-url]: https://github.com/agentplexus/ogen-tools/actions/workflows/ci.yaml |
| 114 | + [lint-status-svg]: https://github.com/agentplexus/ogen-tools/actions/workflows/lint.yaml/badge.svg?branch=main |
| 115 | + [lint-status-url]: https://github.com/agentplexus/ogen-tools/actions/workflows/lint.yaml |
| 116 | + [goreport-svg]: https://goreportcard.com/badge/github.com/agentplexus/ogen-tools |
| 117 | + [goreport-url]: https://goreportcard.com/report/github.com/agentplexus/ogen-tools |
| 118 | + [docs-godoc-svg]: https://pkg.go.dev/badge/github.com/agentplexus/ogen-tools |
| 119 | + [docs-godoc-url]: https://pkg.go.dev/github.com/agentplexus/ogen-tools |
| 120 | + [license-svg]: https://img.shields.io/badge/license-MIT-blue.svg |
| 121 | + [license-url]: https://github.com/agentplexus/ogen-tools/blob/master/LICENSE |
| 122 | + [used-by-svg]: https://sourcegraph.com/github.com/agentplexus/ogen-tools/-/badge.svg |
| 123 | + [used-by-url]: https://sourcegraph.com/github.com/agentplexus/ogen-tools?badge |
0 commit comments