Skip to content

Commit 49491e4

Browse files
committed
feat: ogen-fixerror, ogen-fixnull: initial commit
0 parents  commit 49491e4

File tree

20 files changed

+1369
-0
lines changed

20 files changed

+1369
-0
lines changed

.github/dependabot.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: gomod
4+
directory: /
5+
schedule:
6+
interval: daily
7+
- package-ecosystem: github-actions
8+
directory: /
9+
schedule:
10+
interval: daily

.github/workflows/ci.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
jobs:
11+
test:
12+
strategy:
13+
matrix:
14+
go-version: [1.25.x, 1.24.x]
15+
platform: [ubuntu-latest, macos-latest, windows-latest]
16+
runs-on: ${{ matrix.platform }}
17+
steps:
18+
- name: Install Go
19+
if: success()
20+
uses: actions/setup-go@v6
21+
with:
22+
go-version: ${{ matrix.go-version }}
23+
- name: Checkout code
24+
uses: actions/checkout@v6
25+
- name: Run tests
26+
run: go test -v -covermode=count ./...

.github/workflows/lint.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: lint
2+
on: [push, pull_request]
3+
jobs:
4+
lint:
5+
strategy:
6+
matrix:
7+
go-version: [1.x]
8+
platform: [ubuntu-latest]
9+
runs-on: ${{ matrix.platform }}
10+
steps:
11+
- uses: actions/checkout@v6
12+
- name: golangci-lint
13+
uses: golangci/golangci-lint-action@v9
14+
with:
15+
version: latest
16+
args: --timeout 3m --max-same-issues 0 --max-issues-per-linter 0 --verbose

.github/workflows/sast_codeql.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: "CodeQL SAST Analysis"
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
schedule:
11+
- cron: '30 1 * * 0'
12+
workflow_dispatch:
13+
14+
jobs:
15+
analyze:
16+
name: Analyze
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 360
19+
permissions:
20+
actions: read
21+
contents: read
22+
security-events: write
23+
24+
strategy:
25+
fail-fast: false
26+
matrix:
27+
language: ['go']
28+
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@v6
32+
33+
- name: Initialize CodeQL
34+
uses: github/codeql-action/init@v4
35+
with:
36+
languages: ${{ matrix.language }}
37+
queries: security-extended,security-and-quality
38+
39+
- name: Set up Go
40+
uses: actions/setup-go@v6
41+
with:
42+
go-version: '1.24.x'
43+
44+
- name: Autobuild
45+
uses: github/codeql-action/autobuild@v4
46+
47+
- name: Perform CodeQL Analysis
48+
uses: github/codeql-action/analyze@v4
49+
with:
50+
category: "/language:${{matrix.language}}"

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
.env
3+
_*
4+
*.tar
5+
coverage.txt
6+
debug.test
7+
main
8+
main.zip

.golangci.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: "2"
2+
run:
3+
go: "1.23"
4+
linters:
5+
enable:
6+
- dogsled
7+
- dupl
8+
- gosec
9+
- misspell
10+
- nakedret
11+
- staticcheck
12+
- unconvert
13+
- unparam
14+
- whitespace
15+
exclusions:
16+
generated: lax
17+
presets:
18+
- comments
19+
- common-false-positives
20+
- legacy
21+
- std-error-handling
22+
paths:
23+
- third_party$
24+
- builtin$
25+
- examples$
26+
settings:
27+
staticcheck:
28+
checks:
29+
- "-QF1008" # Disable the "could remove embedded field" check
30+
formatters:
31+
enable:
32+
- gofmt
33+
- goimports
34+
exclusions:
35+
generated: lax
36+
paths:
37+
- third_party$
38+
- builtin$
39+
- examples$

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2025 John Wang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)