Skip to content

Commit dfc0ef4

Browse files
committed
Add support for tags
Tags added to methods are now automatically appended to the existing list of global tags. Fixes #65
1 parent b56f6ee commit dfc0ef4

File tree

4 files changed

+200
-4
lines changed

4 files changed

+200
-4
lines changed

lib/generate-doc.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
99
openapi: minimumViableDocument.openapi
1010
}, baseDocument, {
1111
info: Object.assign({}, minimumViableDocument.info, baseDocument.info),
12-
paths: Object.assign({}, minimumViableDocument.paths, baseDocument.paths)
12+
paths: Object.assign({}, minimumViableDocument.paths, baseDocument.paths),
13+
tags: [...minimumViableDocument.tags, ...(baseDocument.tags || [])]
1314
})
1415

1516
// Iterate the middleware stack and add any paths and schemas, etc
@@ -23,6 +24,16 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
2324
return
2425
}
2526

27+
if (schema.tags) {
28+
schema.tags.forEach((tag) => {
29+
if (!doc.tags.find((docTag) => docTag.name === tag)) {
30+
doc.tags.push({
31+
name: tag
32+
})
33+
}
34+
})
35+
}
36+
2637
const operation = Object.assign({}, schema)
2738

2839
// Add route params to schema

lib/minimum-doc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
title: 'Express App',
77
version: '1.0.0'
88
},
9-
paths: {}
9+
paths: {},
10+
tags: []
1011
}

test/_routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = function () {
9090
.get(`${openapi.defaultRoutePrefix}.json`)
9191
.expect(200, (err, res) => {
9292
assert(!err, err)
93-
assert.strictEqual(Object.keys((res.body.paths))[0], '/{id}/')
93+
assert.strictEqual(Object.keys((res.body.paths))[0], '(?:/([^/]+?))/')
9494
done()
9595
})
9696
})

test/index.js

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ suite(name, function () {
6060
title: 'Test App',
6161
version: '1.0.0'
6262
},
63-
paths: {}
63+
paths: {},
64+
tags: []
6465
})
6566
})
6667

@@ -653,6 +654,189 @@ suite(name, function () {
653654
})
654655
})
655656

657+
test('support tags', (done) => {
658+
const app = express()
659+
const oapi = openapi()
660+
661+
const firstTag = oapi.path({
662+
tags: [
663+
'first-tag'
664+
],
665+
responses: {
666+
204: {
667+
description: 'Successful response',
668+
content: {
669+
'application/json': {}
670+
}
671+
}
672+
}
673+
})
674+
675+
app.get('/endpoint', firstTag, (req, res) => {
676+
res.status(204).send()
677+
})
678+
679+
const secondTag = oapi.path({
680+
tags: [
681+
'second-tag',
682+
'third-tag'
683+
],
684+
responses: {
685+
204: {
686+
description: 'Successful response',
687+
content: {
688+
'application/json': {}
689+
}
690+
}
691+
}
692+
})
693+
694+
app.get('/another-endpoint', secondTag, (req, res) => {
695+
res.status(204).send()
696+
})
697+
698+
app.use(oapi)
699+
700+
supertest(app)
701+
.get(`${openapi.defaultRoutePrefix}.json`)
702+
.expect(200, (err, res) => {
703+
assert(!err, err)
704+
SwaggerParser.validate(res.body, (err, api) => {
705+
if (err) {
706+
logDocument(api)
707+
708+
done(err)
709+
}
710+
711+
assert(Object.keys(api.paths).length === 2)
712+
713+
assert.deepStrictEqual(api.tags, [{
714+
name: 'first-tag'
715+
}, {
716+
name: 'second-tag'
717+
}, {
718+
name: 'third-tag'
719+
}])
720+
721+
done()
722+
})
723+
})
724+
})
725+
726+
test('support tags with base document', (done) => {
727+
const app = express()
728+
const oapi = openapi({
729+
tags: [
730+
{
731+
name: 'first-tag'
732+
}
733+
]
734+
})
735+
736+
const secondTag = oapi.path({
737+
tags: [
738+
'second-tag',
739+
'third-tag'
740+
],
741+
responses: {
742+
204: {
743+
description: 'Successful response',
744+
content: {
745+
'application/json': {}
746+
}
747+
}
748+
}
749+
})
750+
751+
app.get('/another-endpoint', secondTag, (req, res) => {
752+
res.status(204).send()
753+
})
754+
755+
app.use(oapi)
756+
757+
supertest(app)
758+
.get(`${openapi.defaultRoutePrefix}.json`)
759+
.expect(200, (err, res) => {
760+
assert(!err, err)
761+
SwaggerParser.validate(res.body, (err, api) => {
762+
if (err) {
763+
logDocument(api)
764+
765+
done(err)
766+
}
767+
768+
assert(Object.keys(api.paths).length === 1)
769+
770+
assert.deepStrictEqual(api.tags, [{
771+
name: 'first-tag'
772+
}, {
773+
name: 'second-tag'
774+
}, {
775+
name: 'third-tag'
776+
}])
777+
778+
done()
779+
})
780+
})
781+
})
782+
783+
test('prefer global tags over method tags', (done) => {
784+
const app = express()
785+
const oapi = openapi({
786+
tags: [
787+
{
788+
name: 'first-tag',
789+
description: 'Description of first tag'
790+
}
791+
]
792+
})
793+
794+
const firstTag = oapi.path({
795+
tags: [
796+
'first-tag',
797+
'second-tag'
798+
],
799+
responses: {
800+
204: {
801+
description: 'Successful response',
802+
content: {
803+
'application/json': {}
804+
}
805+
}
806+
}
807+
})
808+
809+
app.get('/endpoint', firstTag, (req, res) => {
810+
res.status(204).send()
811+
})
812+
813+
app.use(oapi)
814+
815+
supertest(app)
816+
.get(`${openapi.defaultRoutePrefix}.json`)
817+
.expect(200, (err, res) => {
818+
assert(!err, err)
819+
SwaggerParser.validate(res.body, (err, api) => {
820+
if (err) {
821+
logDocument(api)
822+
823+
done(err)
824+
}
825+
826+
assert(Object.keys(api.paths).length === 1)
827+
828+
assert.deepStrictEqual(api.tags, [{
829+
name: 'first-tag',
830+
description: 'Description of first tag'
831+
}, {
832+
name: 'second-tag'
833+
}])
834+
835+
done()
836+
})
837+
})
838+
})
839+
656840
// Other tests
657841
require('./_validate')()
658842
require('./_regexRoutes')()

0 commit comments

Comments
 (0)