Skip to content

Commit 7a7a18f

Browse files
zsbahtiarZam Zam Saeful Bahtiar
and
Zam Zam Saeful Bahtiar
authored
feat(config): add DefaultModelsExpandDepth set the default expansion depth for models (#107)
* feat(config): add defaultModelsExpandDepth * test(DefaultModelsExpandDepth): add test for DefaultModelsExpandDepth * chore(DefaultModelsExpandDepth): add more desc * chore(DefaultModelsExpandDepth): change words --------- Co-authored-by: Zam Zam Saeful Bahtiar <[email protected]>
1 parent bc83795 commit 7a7a18f

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

swagger.go

+37-19
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ var WrapHandler = Handler()
1616
// Config stores httpSwagger configuration variables.
1717
type Config struct {
1818
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
19-
URL string
20-
DocExpansion string
21-
DomID string
22-
InstanceName string
23-
BeforeScript template.JS
24-
AfterScript template.JS
25-
Plugins []template.JS
26-
UIConfig map[template.JS]template.JS
27-
DeepLinking bool
28-
PersistAuthorization bool
29-
Layout SwaggerLayout
19+
URL string
20+
DocExpansion string
21+
DomID string
22+
InstanceName string
23+
BeforeScript template.JS
24+
AfterScript template.JS
25+
Plugins []template.JS
26+
UIConfig map[template.JS]template.JS
27+
DeepLinking bool
28+
PersistAuthorization bool
29+
Layout SwaggerLayout
30+
DefaultModelsExpandDepth ModelsExpandDepthType
3031
}
3132

3233
// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
@@ -124,15 +125,31 @@ func Layout(layout SwaggerLayout) func(*Config) {
124125
}
125126
}
126127

128+
type ModelsExpandDepthType int
129+
130+
const (
131+
ShowModel ModelsExpandDepthType = 1
132+
HideModel ModelsExpandDepthType = -1
133+
)
134+
135+
// DefaultModelsExpandDepth presents the model of response and request.
136+
// set the default expansion depth for models
137+
func DefaultModelsExpandDepth(defaultModelsExpandDepth ModelsExpandDepthType) func(*Config) {
138+
return func(c *Config) {
139+
c.DefaultModelsExpandDepth = defaultModelsExpandDepth
140+
}
141+
}
142+
127143
func newConfig(configFns ...func(*Config)) *Config {
128144
config := Config{
129-
URL: "doc.json",
130-
DocExpansion: "list",
131-
DomID: "swagger-ui",
132-
InstanceName: "swagger",
133-
DeepLinking: true,
134-
PersistAuthorization: false,
135-
Layout: StandaloneLayout,
145+
URL: "doc.json",
146+
DocExpansion: "list",
147+
DomID: "swagger-ui",
148+
InstanceName: "swagger",
149+
DeepLinking: true,
150+
PersistAuthorization: false,
151+
Layout: StandaloneLayout,
152+
DefaultModelsExpandDepth: ShowModel,
136153
}
137154

138155
for _, fn := range configFns {
@@ -296,7 +313,8 @@ window.onload = function() {
296313
{{- range $k, $v := .UIConfig}}
297314
{{$k}}: {{$v}},
298315
{{- end}}
299-
layout: "{{$.Layout}}"
316+
layout: "{{$.Layout}}",
317+
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
300318
})
301319
302320
window.ui = ui

swagger_test.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,13 @@ func TestUIConfigOptions(t *testing.T) {
377377
{
378378
desc: "default configuration",
379379
cfg: &Config{
380-
URL: "doc.json",
381-
DeepLinking: true,
382-
DocExpansion: "list",
383-
DomID: "swagger-ui",
384-
PersistAuthorization: false,
385-
Layout: StandaloneLayout,
380+
URL: "doc.json",
381+
DeepLinking: true,
382+
DocExpansion: "list",
383+
DomID: "swagger-ui",
384+
PersistAuthorization: false,
385+
Layout: StandaloneLayout,
386+
DefaultModelsExpandDepth: ShowModel,
386387
},
387388
exp: `window.onload = function() {
388389
@@ -400,7 +401,8 @@ func TestUIConfigOptions(t *testing.T) {
400401
plugins: [
401402
SwaggerUIBundle.plugins.DownloadUrl
402403
],
403-
layout: "StandaloneLayout"
404+
layout: "StandaloneLayout",
405+
defaultModelsExpandDepth: 1
404406
})
405407
406408
window.ui = ui
@@ -432,6 +434,7 @@ func TestUIConfigOptions(t *testing.T) {
432434
"onComplete": `() => { window.ui.setBasePath('v3'); }`,
433435
"defaultModelRendering": `"model"`,
434436
},
437+
DefaultModelsExpandDepth: HideModel,
435438
},
436439
exp: `window.onload = function() {
437440
const SomePlugin = (system) => ({
@@ -458,7 +461,8 @@ func TestUIConfigOptions(t *testing.T) {
458461
defaultModelRendering: "model",
459462
onComplete: () => { window.ui.setBasePath('v3'); },
460463
showExtensions: true,
461-
layout: "StandaloneLayout"
464+
layout: "StandaloneLayout",
465+
defaultModelsExpandDepth: -1
462466
})
463467
464468
window.ui = ui
@@ -537,3 +541,17 @@ func TestUIConfigOptions(t *testing.T) {
537541
})
538542
}
539543
}
544+
545+
func TestDefaultModelsExpandDepth(t *testing.T) {
546+
cfg := newConfig()
547+
// Default value
548+
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)
549+
550+
// Set hide
551+
DefaultModelsExpandDepth(HideModel)(cfg)
552+
assert.Equal(t, HideModel, cfg.DefaultModelsExpandDepth)
553+
554+
// Set show
555+
DefaultModelsExpandDepth(ShowModel)(cfg)
556+
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)
557+
}

0 commit comments

Comments
 (0)