Skip to content

feat: Adding Support for SyntaxHighlight options & Fix swagger_test.go #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {
Layout SwaggerLayout
DefaultModelsExpandDepth ModelsExpandDepthType
ShowExtensions bool
SyntaxHighlight bool
}

// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
Expand Down Expand Up @@ -105,6 +106,13 @@ func BeforeScript(js string) func(*Config) {
}
}

// SyntaxHighlight true, false.
func SyntaxHighlight(syntaxHighlight bool) func(*Config) {
return func(c *Config) {
c.SyntaxHighlight = syntaxHighlight
}
}

// AfterScript holds JavaScript to be run right after the Swagger UI object is created
// and set on the window.
func AfterScript(js string) func(*Config) {
Expand Down Expand Up @@ -161,6 +169,7 @@ func newConfig(configFns ...func(*Config)) *Config {
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
ShowExtensions: false,
SyntaxHighlight: true,
}

for _, fn := range configFns {
Expand Down Expand Up @@ -312,6 +321,7 @@ window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "{{.URL}}",
syntaxHighlight: {{.SyntaxHighlight}},
deepLinking: {{.DeepLinking}},
docExpansion: "{{.DocExpansion}}",
dom_id: "#{{.DomID}}",
Expand Down
18 changes: 12 additions & 6 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,20 @@ func TestUIConfigOptions(t *testing.T) {
desc: "default configuration",
cfg: &Config{
URL: "doc.json",
SyntaxHighlight: true,
DeepLinking: true,
DocExpansion: "list",
DomID: "swagger-ui",
PersistAuthorization: false,
Layout: StandaloneLayout,
DefaultModelsExpandDepth: ShowModel,
ShowExtensions: true,
},
exp: `window.onload = function() {

const ui = SwaggerUIBundle({
url: "doc.json",
syntaxHighlight: true ,
deepLinking: true ,
docExpansion: "list",
dom_id: "#swagger-ui",
Expand All @@ -406,7 +409,8 @@ func TestUIConfigOptions(t *testing.T) {
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
defaultModelsExpandDepth: 1
defaultModelsExpandDepth: 1 ,
showExtensions: true
})

window.ui = ui
Expand All @@ -416,11 +420,13 @@ func TestUIConfigOptions(t *testing.T) {
desc: "script configuration",
cfg: &Config{
URL: "swagger.json",
SyntaxHighlight: true,
DeepLinking: false,
PersistAuthorization: true,
DocExpansion: "none",
DomID: "swagger-ui-id",
Layout: StandaloneLayout,
ShowExtensions: true,
BeforeScript: `const SomePlugin = (system) => ({
// Some plugin
});
Expand All @@ -434,7 +440,6 @@ func TestUIConfigOptions(t *testing.T) {
"AnotherPlugin",
},
UIConfig: map[template.JS]template.JS{
"showExtensions": "true",
"onComplete": `() => { window.ui.setBasePath('v3'); }`,
"defaultModelRendering": `"model"`,
},
Expand All @@ -448,6 +453,7 @@ func TestUIConfigOptions(t *testing.T) {

const ui = SwaggerUIBundle({
url: "swagger.json",
syntaxHighlight: true ,
deepLinking: false ,
docExpansion: "none",
dom_id: "#swagger-ui-id",
Expand All @@ -464,9 +470,9 @@ func TestUIConfigOptions(t *testing.T) {
],
defaultModelRendering: "model",
onComplete: () => { window.ui.setBasePath('v3'); },
showExtensions: true,
layout: "StandaloneLayout",
defaultModelsExpandDepth: -1
defaultModelsExpandDepth: -1 ,
showExtensions: true
})

window.ui = ui
Expand Down Expand Up @@ -529,12 +535,12 @@ func TestUIConfigOptions(t *testing.T) {
for i, expln := range explns {
if i >= buflen {
printContext(i)
t.Fatalf(`first unequal line: expected "%s" but got EOF`, expln)
t.Fatalf(`first unequal line on line %d: expected "%s" but got EOF`, i, expln)
}
bufln := buflns[i]
if bufln != expln {
printContext(i)
t.Fatalf(`first unequal line: expected "%s" but got "%s"`, expln, bufln)
t.Fatalf(`first unequal line on line %d: expected "%s" but got "%s"`, i, expln, bufln)
}
}

Expand Down