Skip to content

Commit

Permalink
feat(config): add DefaultModelsExpandDepth set the default expansion …
Browse files Browse the repository at this point in the history
…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 <zamzam.saeful@efishery.com>
  • Loading branch information
zsbahtiar and Zam Zam Saeful Bahtiar committed Aug 23, 2023
1 parent bc83795 commit 7a7a18f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
56 changes: 37 additions & 19 deletions swagger.go
Expand Up @@ -16,17 +16,18 @@ var WrapHandler = Handler()
// Config stores httpSwagger configuration variables.
type Config struct {
// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
DocExpansion string
DomID string
InstanceName string
BeforeScript template.JS
AfterScript template.JS
Plugins []template.JS
UIConfig map[template.JS]template.JS
DeepLinking bool
PersistAuthorization bool
Layout SwaggerLayout
URL string
DocExpansion string
DomID string
InstanceName string
BeforeScript template.JS
AfterScript template.JS
Plugins []template.JS
UIConfig map[template.JS]template.JS
DeepLinking bool
PersistAuthorization bool
Layout SwaggerLayout
DefaultModelsExpandDepth ModelsExpandDepthType
}

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

type ModelsExpandDepthType int

const (
ShowModel ModelsExpandDepthType = 1
HideModel ModelsExpandDepthType = -1
)

// DefaultModelsExpandDepth presents the model of response and request.
// set the default expansion depth for models
func DefaultModelsExpandDepth(defaultModelsExpandDepth ModelsExpandDepthType) func(*Config) {
return func(c *Config) {
c.DefaultModelsExpandDepth = defaultModelsExpandDepth
}
}

func newConfig(configFns ...func(*Config)) *Config {
config := Config{
URL: "doc.json",
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DeepLinking: true,
PersistAuthorization: false,
Layout: StandaloneLayout,
URL: "doc.json",
DocExpansion: "list",
DomID: "swagger-ui",
InstanceName: "swagger",
DeepLinking: true,
PersistAuthorization: false,
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
}

for _, fn := range configFns {
Expand Down Expand Up @@ -296,7 +313,8 @@ window.onload = function() {
{{- range $k, $v := .UIConfig}}
{{$k}}: {{$v}},
{{- end}}
layout: "{{$.Layout}}"
layout: "{{$.Layout}}",
defaultModelsExpandDepth: {{.DefaultModelsExpandDepth}}
})
window.ui = ui
Expand Down
34 changes: 26 additions & 8 deletions swagger_test.go
Expand Up @@ -377,12 +377,13 @@ func TestUIConfigOptions(t *testing.T) {
{
desc: "default configuration",
cfg: &Config{
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
Layout: StandaloneLayout,
URL: "doc.json",
DeepLinking: true,
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
},
exp: `window.onload = function() {
Expand All @@ -400,7 +401,8 @@ func TestUIConfigOptions(t *testing.T) {
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
layout: "StandaloneLayout",
defaultModelsExpandDepth: 1
})
window.ui = ui
Expand Down Expand Up @@ -432,6 +434,7 @@ func TestUIConfigOptions(t *testing.T) {
"onComplete": `() => { window.ui.setBasePath('v3'); }`,
"defaultModelRendering": `"model"`,
},
DefaultModelsExpandDepth: HideModel,
},
exp: `window.onload = function() {
const SomePlugin = (system) => ({
Expand All @@ -458,7 +461,8 @@ func TestUIConfigOptions(t *testing.T) {
defaultModelRendering: "model",
onComplete: () => { window.ui.setBasePath('v3'); },
showExtensions: true,
layout: "StandaloneLayout"
layout: "StandaloneLayout",
defaultModelsExpandDepth: -1
})
window.ui = ui
Expand Down Expand Up @@ -537,3 +541,17 @@ func TestUIConfigOptions(t *testing.T) {
})
}
}

func TestDefaultModelsExpandDepth(t *testing.T) {
cfg := newConfig()
// Default value
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)

// Set hide
DefaultModelsExpandDepth(HideModel)(cfg)
assert.Equal(t, HideModel, cfg.DefaultModelsExpandDepth)

// Set show
DefaultModelsExpandDepth(ShowModel)(cfg)
assert.Equal(t, ShowModel, cfg.DefaultModelsExpandDepth)
}

0 comments on commit 7a7a18f

Please sign in to comment.