Skip to content

Commit eada189

Browse files
committed
feat: added name mangler options to Spec analyzer and FlattenOpts
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 351138e commit eada189

File tree

6 files changed

+65
-25
lines changed

6 files changed

+65
-25
lines changed

analyzer.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,27 @@ type Spec struct {
145145
enums enumAnalysis
146146
allSchemas map[string]SchemaRef
147147
allOfs map[string]SchemaRef
148+
mangler mangling.NameMangler
148149
}
149150

150151
// New takes a swagger spec object and returns an analyzed spec document.
151152
// The analyzed document contains a number of indices that make it easier to
152153
// reason about semantics of a swagger specification for use in code generation
153154
// or validation etc.
154-
func New(doc *spec.Swagger) *Spec {
155+
func New(doc *spec.Swagger, opts ...Option) *Spec {
156+
o := &analyzerOptions{}
157+
for _, opt := range opts {
158+
opt(o)
159+
}
160+
155161
a := &Spec{
156162
spec: doc,
157163
references: referenceAnalysis{},
158164
patterns: patternAnalysis{},
159165
enums: enumAnalysis{},
166+
mangler: mangling.NewNameMangler(o.manglerOpts...),
160167
}
168+
161169
a.reset()
162170
a.initialize()
163171

@@ -288,20 +296,6 @@ func (s *Spec) ProducesFor(operation *spec.Operation) []string {
288296
return s.structMapKeys(prod)
289297
}
290298

291-
func mapKeyFromParam(param *spec.Parameter) string {
292-
return fmt.Sprintf("%s#%s", param.In, fieldNameFromParam(param))
293-
}
294-
295-
func fieldNameFromParam(param *spec.Parameter) string {
296-
// TODO: this should be x-go-name
297-
if nm, ok := param.Extensions.GetString("go-name"); ok {
298-
return nm
299-
}
300-
mangler := mangling.NewNameMangler()
301-
302-
return mangler.ToGoName(param.Name)
303-
}
304-
305299
// ErrorOnParamFunc is a callback function to be invoked
306300
// whenever an error is encountered while resolving references
307301
// on parameters.
@@ -651,6 +645,19 @@ func (s *Spec) AllEnums() map[string][]any {
651645
return cloneEnumMap(s.enums.allEnums)
652646
}
653647

648+
func (s *Spec) mapKeyFromParam(param *spec.Parameter) string {
649+
return fmt.Sprintf("%s#%s", param.In, s.fieldNameFromParam(param))
650+
}
651+
652+
func (s *Spec) fieldNameFromParam(param *spec.Parameter) string {
653+
// TODO: this should be x-go-name
654+
if nm, ok := param.Extensions.GetString("go-name"); ok {
655+
return nm
656+
}
657+
658+
return s.mangler.ToGoName(param.Name)
659+
}
660+
654661
func (s *Spec) structMapKeys(mp map[string]struct{}) []string {
655662
if len(mp) == 0 {
656663
return nil
@@ -668,7 +675,7 @@ func (s *Spec) paramsAsMap(parameters []spec.Parameter, res map[string]spec.Para
668675
for _, param := range parameters {
669676
pr := param
670677
if pr.Ref.String() == "" {
671-
res[mapKeyFromParam(&pr)] = pr
678+
res[s.mapKeyFromParam(&pr)] = pr
672679

673680
continue
674681
}
@@ -699,7 +706,7 @@ func (s *Spec) paramsAsMap(parameters []spec.Parameter, res map[string]spec.Para
699706
}
700707

701708
pr = objAsParam
702-
res[mapKeyFromParam(&pr)] = pr
709+
res[s.mapKeyFromParam(&pr)] = pr
703710
}
704711
}
705712

flatten.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ func updateRefParents(allRefs map[string]spec.Ref, r *newRef) {
554554
}
555555
}
556556

557+
//nolint:gocognit,gocyclo,cyclop // legacy from a lot of design choices that led to concentrate the complexity just here.
557558
func stripOAIGenForRef(opts *FlattenOpts, k string, r *newRef) (bool, error) {
558559
replacedWithComplex := false
559560

flatten_name.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ func mangler(o *FlattenOpts) func(string) string {
273273
if o.KeepNames {
274274
return func(in string) string { return in }
275275
}
276-
mangler := mangling.NewNameMangler()
276+
m := mangling.NewNameMangler(o.ManglerOpts...)
277277

278-
return mangler.ToJSONName
278+
return m.ToJSONName
279279
}
280280

281281
func nameFromRef(ref spec.Ref, o *FlattenOpts) string {

flatten_options.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88

99
"github.com/go-openapi/spec"
10+
"github.com/go-openapi/swag/mangling"
1011
)
1112

1213
// FlattenOpts configuration for flattening a swagger specification.
@@ -24,12 +25,13 @@ type FlattenOpts struct {
2425
BasePath string // The location of the root document for this spec to resolve relative $ref
2526

2627
// Flattening options
27-
Expand bool // When true, skip flattening the spec and expand it instead (if Minimal is false)
28-
Minimal bool // When true, do not decompose complex structures such as allOf
29-
Verbose bool // enable some reporting on possible name conflicts detected
30-
RemoveUnused bool // When true, remove unused parameters, responses and definitions after expansion/flattening
31-
ContinueOnError bool // Continue when spec expansion issues are found
32-
KeepNames bool // Do not attempt to jsonify names from references when flattening
28+
Expand bool // When true, skip flattening the spec and expand it instead (if Minimal is false)
29+
Minimal bool // When true, do not decompose complex structures such as allOf
30+
Verbose bool // enable some reporting on possible name conflicts detected
31+
RemoveUnused bool // When true, remove unused parameters, responses and definitions after expansion/flattening
32+
ContinueOnError bool // Continue when spec expansion issues are found
33+
KeepNames bool // Do not attempt to jsonify names from references when flattening
34+
ManglerOpts []mangling.Option // Options for the name mangler used to jsonify names
3335

3436
/* Extra keys */
3537
_ struct{} // require keys

go.work.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
23
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
34
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
45
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
@@ -8,22 +9,30 @@ github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT
89
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
910
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
1011
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
12+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1113
go.mongodb.org/mongo-driver v1.17.6 h1:87JUG1wZfWsr6rIz3ZmpH90rL5tea7O3IHuSwHUpsss=
1214
go.mongodb.org/mongo-driver v1.17.6/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
1315
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
1416
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
1517
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
18+
golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA=
1619
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
1720
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
1821
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
22+
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
1923
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
2024
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
25+
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
2126
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
2227
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
2328
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
29+
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
30+
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4/go.mod h1:g5NllXBEermZrmR51cJDQxmJUHUOfRAaNyWBM+R+548=
2431
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
2532
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg=
2633
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM=
34+
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
2735
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
2836
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
2937
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
38+
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=

options.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package analysis
5+
6+
import "github.com/go-openapi/swag/mangling"
7+
8+
// Option configures the behavior of a new [Spec] analyzer.
9+
type Option func(*analyzerOptions)
10+
11+
type analyzerOptions struct {
12+
manglerOpts []mangling.Option
13+
}
14+
15+
// WithManglerOptions sets the name mangler options used when building
16+
// Go identifiers from specification names (e.g. parameter names).
17+
func WithManglerOptions(opts ...mangling.Option) Option {
18+
return func(o *analyzerOptions) {
19+
o.manglerOpts = append(o.manglerOpts, opts...)
20+
}
21+
}

0 commit comments

Comments
 (0)