Skip to content

Commit c2f261a

Browse files
authored
feat: added name mangler options to Spec analyzer and FlattenOpts (#183)
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 72c8ae0 commit c2f261a

File tree

8 files changed

+59
-75
lines changed

8 files changed

+59
-75
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: 0 additions & 47 deletions
This file was deleted.

internal/testintegration/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/go-openapi/loads v0.23.2
88
github.com/go-openapi/spec v0.22.4
99
github.com/go-openapi/swag/loading v0.25.5
10-
github.com/go-openapi/testify/v2 v2.4.1
10+
github.com/go-openapi/testify/v2 v2.4.2
1111
)
1212

1313
require (

internal/testintegration/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ github.com/go-openapi/swag/yamlutils v0.25.5 h1:kASCIS+oIeoc55j28T4o8KwlV2S4ZLPT
3030
github.com/go-openapi/swag/yamlutils v0.25.5/go.mod h1:Gek1/SjjfbYvM+Iq4QGwa/2lEXde9n2j4a3wI3pNuOQ=
3131
github.com/go-openapi/testify/enable/yaml/v2 v2.4.0 h1:7SgOMTvJkM8yWrQlU8Jm18VeDPuAvB/xWrdxFJkoFag=
3232
github.com/go-openapi/testify/enable/yaml/v2 v2.4.0/go.mod h1:14iV8jyyQlinc9StD7w1xVPW3CO3q1Gj04Jy//Kw4VM=
33-
github.com/go-openapi/testify/v2 v2.4.1 h1:zB34HDKj4tHwyUQHrUkpV0Q0iXQ6dUCOQtIqn8hE6Iw=
34-
github.com/go-openapi/testify/v2 v2.4.1/go.mod h1:HCPmvFFnheKK2BuwSA0TbbdxJ3I16pjwMkYkP4Ywn54=
33+
github.com/go-openapi/testify/v2 v2.4.2 h1:tiByHpvE9uHrrKjOszax7ZvKB7QOgizBWGBLuq0ePx4=
34+
github.com/go-openapi/testify/v2 v2.4.2/go.mod h1:SgsVHtfooshd0tublTtJ50FPKhujf47YRqauXXOUxfw=
3535
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
3636
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
3737
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

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)