Skip to content

Commit e816055

Browse files
authored
Merge pull request #61 from moredure/patch-1
chore: go generate ./...
2 parents e9fbcc0 + 9521303 commit e816055

22 files changed

Lines changed: 53773 additions & 16287 deletions

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ require (
5252
github.com/mtibben/percent v0.2.1 // indirect
5353
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
5454
github.com/quic-go/qpack v0.6.0 // indirect
55-
github.com/quic-go/quic-go v0.57.1 // indirect
55+
github.com/quic-go/quic-go v0.59.1 // indirect
5656
github.com/segmentio/asm v1.2.0 // indirect
5757
github.com/stretchr/objx v0.5.2 // indirect
5858
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
109109
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
110110
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
111111
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
112-
github.com/quic-go/quic-go v0.57.1 h1:25KAAR9QR8KZrCZRThWMKVAwGoiHIrNbT72ULHTuI10=
113-
github.com/quic-go/quic-go v0.57.1/go.mod h1:ly4QBAjHA2VhdnxhojRsCUOeJwKYg+taDlos92xb1+s=
112+
github.com/quic-go/quic-go v0.59.1 h1:0Gmua0HW1Tv7ANR7hUYwRyD0MG5OJfgvYSZasGZzBic=
113+
github.com/quic-go/quic-go v0.59.1/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
114114
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
115115
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
116116
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=

kinde/management_api/compat.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Hand-written compatibility shim: not generated by ogen, not touched by `go generate ./...`.
2+
package management_api
3+
4+
import "context"
5+
6+
// Deprecated: use DeleteAPIApplicationScopeOperation. Kept for source
7+
// compatibility with the pre-typo-fix operation name.
8+
const DeleteAPIAppliationScopeOperation = DeleteAPIApplicationScopeOperation
9+
10+
// Deprecated: use DeleteAPIApplicationScopeParams.
11+
type DeleteAPIAppliationScopeParams = DeleteAPIApplicationScopeParams
12+
13+
// Deprecated: use DeleteAPIApplicationScopeRes.
14+
type DeleteAPIAppliationScopeRes = DeleteAPIApplicationScopeRes
15+
16+
// Deprecated: use DeleteAPIApplicationScopeOK.
17+
type DeleteAPIAppliationScopeOK = DeleteAPIApplicationScopeOK
18+
19+
// Deprecated: use DeleteAPIApplicationScopeBadRequest.
20+
type DeleteAPIAppliationScopeBadRequest = DeleteAPIApplicationScopeBadRequest
21+
22+
// Deprecated: use DeleteAPIApplicationScopeForbidden.
23+
type DeleteAPIAppliationScopeForbidden = DeleteAPIApplicationScopeForbidden
24+
25+
// Deprecated: use DeleteAPIApplicationScopeTooManyRequests.
26+
type DeleteAPIAppliationScopeTooManyRequests = DeleteAPIApplicationScopeTooManyRequests
27+
28+
// DeleteAPIAppliationScope invokes DeleteAPIApplicationScope operation.
29+
//
30+
// Deprecated: use DeleteAPIApplicationScope.
31+
func (c *Client) DeleteAPIAppliationScope(ctx context.Context, params DeleteAPIAppliationScopeParams) (DeleteAPIAppliationScopeRes, error) {
32+
return c.DeleteAPIApplicationScope(ctx, params)
33+
}

kinde/management_api/fix_oneof.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//go:build ignore
2+
// +build ignore
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"os"
10+
"regexp"
11+
"strings"
12+
)
13+
14+
// This tool patches the generated oas_json_gen.go file to stop ambiguous oneOf
15+
// decoding from silently defaulting to variant 0. When one oneOf variant has
16+
// no fields unique to it, ogen makes it the sum type's default mapping and
17+
// falls back to it whenever no discriminating key matches, which can misroute
18+
// a payload (e.g. decode a SAML/enterprise connection config as a plain OAuth
19+
// one) once sibling variants share field names. This makes it fail closed.
20+
21+
const targetFile = "oas_json_gen.go"
22+
23+
// Matches the generated "if !found { s.Type = <value> }" fallback. <value> is
24+
// checked separately for the "X0X" sum-type-variant-0 shape (Go's regexp
25+
// package has no backreferences), e.g.
26+
// CreateConnectionReqOptions0CreateConnectionReqOptions.
27+
var fallbackPattern = regexp.MustCompile(`if !found \{\n\t\ts\.Type = (\w+)\n\t\}`)
28+
29+
// remainingFallbackPattern is a looser tripwire: any `if !found` block that
30+
// still assigns s.Type before its closing brace. If it matches after the
31+
// patch pass, the generated shape has drifted past fallbackPattern (or a sum
32+
// type gained a non-variant-0 default) and the generate run must fail rather
33+
// than ship the silent-default behaviour.
34+
var remainingFallbackPattern = regexp.MustCompile(`if !found \{[^}]*s\.Type\s*=`)
35+
36+
// isVariant0Assignment reports whether value has the "X0X" shape ogen uses
37+
// for a sum type's zero-variant constant.
38+
func isVariant0Assignment(value string) bool {
39+
n := len(value)
40+
if n < 3 || n%2 == 0 {
41+
return false
42+
}
43+
half := (n - 1) / 2
44+
return value[half] == '0' && value[:half] == value[half+1:]
45+
}
46+
47+
func main() {
48+
fmt.Printf("Patching %s for ambiguous oneOf handling...\n", targetFile)
49+
50+
content, err := os.ReadFile(targetFile)
51+
if err != nil {
52+
fmt.Fprintf(os.Stderr, "Error reading file: %v\n", err)
53+
os.Exit(1)
54+
}
55+
56+
patched := 0
57+
newContent := fallbackPattern.ReplaceAllFunc(content, func(match []byte) []byte {
58+
sub := fallbackPattern.FindSubmatch(match)
59+
if sub == nil || !isVariant0Assignment(string(sub[1])) {
60+
return match
61+
}
62+
patched++
63+
return []byte(`if !found {
64+
return errors.New("unable to detect sum type variant")
65+
}`)
66+
})
67+
68+
// Validate before writing so a failed run leaves the file exactly as ogen
69+
// generated it, never partially patched.
70+
if locs := remainingFallbackPattern.FindAllIndex(newContent, -1); len(locs) > 0 {
71+
fmt.Fprintf(os.Stderr, "Error: %d oneOf fallback(s) would still assign s.Type after patching:\n", len(locs))
72+
for _, loc := range locs {
73+
line := 1 + bytes.Count(newContent[:loc[0]], []byte("\n"))
74+
snippet := strings.Join(strings.Fields(string(newContent[loc[0]:loc[1]])), " ")
75+
fmt.Fprintf(os.Stderr, " %s:%d: %s\n", targetFile, line, snippet)
76+
}
77+
fmt.Fprintf(os.Stderr, "The generated shape has likely changed (ogen upgrade?) - update fix_oneof.go\n")
78+
fmt.Fprintf(os.Stderr, "%s left unmodified\n", targetFile)
79+
os.Exit(1)
80+
}
81+
82+
if patched == 0 {
83+
fmt.Println("No ambiguous oneOf fallback found - already patched or no longer generated")
84+
return
85+
}
86+
87+
if err := os.WriteFile(targetFile, newContent, 0644); err != nil {
88+
fmt.Fprintf(os.Stderr, "Error writing patched file: %v\n", err)
89+
os.Exit(1)
90+
}
91+
fmt.Printf("✅ Patched %d ambiguous oneOf fallback(s) to fail closed\n", patched)
92+
}

kinde/management_api/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ package management_api
22

33
//go:generate go run github.com/ogen-go/ogen/cmd/ogen --target . -package management_api --clean https://api-spec.kinde.com/kinde-management-api-spec.yaml
44
//go:generate go run fix_optstring.go
5+
//go:generate go run fix_oneof.go

0 commit comments

Comments
 (0)