Skip to content

Commit b9463f9

Browse files
committed
fix(flatten): added option to skip automatic camel-case
* contributes go-swagger/go-swagger#2334 Signed-off-by: Frederic BIDON <[email protected]>
1 parent a50691f commit b9463f9

File tree

7 files changed

+85
-9
lines changed

7 files changed

+85
-9
lines changed

fixtures/bugs/2334/bar.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
paths: {}
2+
3+
definitions:
4+
Bar:
5+
type: object
6+
properties:
7+
Baz:
8+
type: string

fixtures/bugs/2334/swagger.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
swagger: '2.0'
2+
info:
3+
title: Object
4+
version: 0.1.0
5+
paths:
6+
/bar:
7+
get:
8+
responses:
9+
200:
10+
description: OK
11+
schema:
12+
$ref: "./bar.yaml#/definitions/Bar"

flatten.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func importNewRef(entry sortref.RefRevIdx, refStr string, opts *FlattenOpts) err
331331
}
332332

333333
// generate a unique name - isOAIGen means that a naming conflict was resolved by changing the name
334-
newName, isOAIGen = uniqifyName(opts.Swagger().Definitions, nameFromRef(entry.Ref))
334+
newName, isOAIGen = uniqifyName(opts.Swagger().Definitions, nameFromRef(entry.Ref, opts))
335335
debugLog("new name for [%s]: %s - with name conflict:%t", strings.Join(entry.Keys, ", "), newName, isOAIGen)
336336

337337
opts.flattenContext.resolved[refStr] = newName

flatten_name.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func (isn *InlineSchemaNamer) Name(key string, schema *spec.Schema, aschema *Ana
3333
}
3434

3535
// create unique name
36-
newName, isOAIGen := uniqifyName(isn.Spec.Definitions, swag.ToJSONName(name))
36+
mangle := mangler(isn.opts)
37+
newName, isOAIGen := uniqifyName(isn.Spec.Definitions, mangle(name))
3738

3839
// clone schema
3940
sch := schutils.Clone(schema)
@@ -256,25 +257,35 @@ func partAdder(aschema *AnalyzedSchema) sortref.PartAdder {
256257
}
257258
}
258259

259-
func nameFromRef(ref spec.Ref) string {
260+
func mangler(o *FlattenOpts) func(string) string {
261+
if o.KeepNames {
262+
return func(in string) string { return in }
263+
}
264+
265+
return swag.ToJSONName
266+
}
267+
268+
func nameFromRef(ref spec.Ref, o *FlattenOpts) string {
269+
mangle := mangler(o)
270+
260271
u := ref.GetURL()
261272
if u.Fragment != "" {
262-
return swag.ToJSONName(path.Base(u.Fragment))
273+
return mangle(path.Base(u.Fragment))
263274
}
264275

265276
if u.Path != "" {
266277
bn := path.Base(u.Path)
267278
if bn != "" && bn != "/" {
268279
ext := path.Ext(bn)
269280
if ext != "" {
270-
return swag.ToJSONName(bn[:len(bn)-len(ext)])
281+
return mangle(bn[:len(bn)-len(ext)])
271282
}
272283

273-
return swag.ToJSONName(bn)
284+
return mangle(bn)
274285
}
275286
}
276287

277-
return swag.ToJSONName(strings.ReplaceAll(u.Host, ".", " "))
288+
return mangle(strings.ReplaceAll(u.Host, ".", " "))
278289
}
279290

280291
// GenLocation indicates from which section of the specification (models or operations) a definition has been created.

flatten_name_test.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,27 @@ func TestName_FromRef(t *testing.T) {
2828
}
2929

3030
for _, v := range values {
31-
assert.Equal(t, v.Expected, nameFromRef(spec.MustCreateRef(v.Source)))
31+
assert.Equal(t, v.Expected, nameFromRef(spec.MustCreateRef(v.Source), &FlattenOpts{}))
32+
}
33+
}
34+
35+
func TestName_FromRefMangle(t *testing.T) {
36+
t.Parallel()
37+
38+
values := []struct{ Source, Expected, ExpectedKeepName string }{
39+
{"#/definitions/ErrorModel", "errorModel", "ErrorModel"},
40+
{"#/definitions/Error_Model", "errorModel", "Error_Model"},
41+
{"http://somewhere.com/definitions/errorModel", "errorModel", "errorModel"},
42+
{"http://somewhere.com/definitions/ErrorModel.json", "errorModel", "ErrorModel"},
43+
{"/definitions/ErrorModel", "errorModel", "ErrorModel"},
44+
{"/definitions/ErrorModel.json", "errorModel", "ErrorModel"},
45+
{"http://somewhere.com", "somewhereCom", "somewhere com"},
46+
{"#", "", ""},
47+
}
48+
49+
for _, v := range values {
50+
assert.Equal(t, v.Expected, nameFromRef(spec.MustCreateRef(v.Source), &FlattenOpts{}))
51+
assert.Equal(t, v.ExpectedKeepName, nameFromRef(spec.MustCreateRef(v.Source), &FlattenOpts{KeepNames: true}))
3252
}
3353
}
3454

@@ -47,7 +67,7 @@ func TestName_Definition(t *testing.T) {
4767
}
4868

4969
for _, v := range values {
50-
u, _ := uniqifyName(v.Definitions, nameFromRef(spec.MustCreateRef(v.Source)))
70+
u, _ := uniqifyName(v.Definitions, nameFromRef(spec.MustCreateRef(v.Source), &FlattenOpts{}))
5171
assert.Equal(t, v.Expected, u)
5272
}
5373
}

flatten_options.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type FlattenOpts struct {
2626
Verbose bool // enable some reporting on possible name conflicts detected
2727
RemoveUnused bool // When true, remove unused parameters, responses and definitions after expansion/flattening
2828
ContinueOnError bool // Continue when spec expansion issues are found
29+
KeepNames bool // Do not attempt to jsonify names from references when flattening
2930

3031
/* Extra keys */
3132
_ struct{} // require keys

flatten_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,30 @@ func TestFlatten_2113(t *testing.T) {
13321332
require.JSONEq(t, string(expected), jazon)
13331333
}
13341334

1335+
func TestFlatten_2334(t *testing.T) {
1336+
// flatten $ref without altering case
1337+
log.SetOutput(io.Discard)
1338+
defer log.SetOutput(os.Stdout)
1339+
1340+
bp := filepath.Join("fixtures", "bugs", "2334", "swagger.yaml")
1341+
sp := antest.LoadOrFail(t, bp)
1342+
an := New(sp)
1343+
1344+
require.NoError(t, Flatten(FlattenOpts{
1345+
Spec: an, BasePath: bp, Verbose: false,
1346+
Expand: false,
1347+
Minimal: true,
1348+
RemoveUnused: true,
1349+
KeepNames: true,
1350+
}))
1351+
1352+
jazon := antest.AsJSON(t, sp)
1353+
1354+
assert.Contains(t, jazon, `"$ref": "#/definitions/Bar"`)
1355+
assert.Contains(t, jazon, `"Bar":`)
1356+
assert.Contains(t, jazon, `"Baz":`)
1357+
}
1358+
13351359
func getDefinition(t testing.TB, sp *spec.Swagger, key string) string {
13361360
d, ok := sp.Definitions[key]
13371361
require.Truef(t, ok, "Expected definition for %s", key)

0 commit comments

Comments
 (0)