Skip to content
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
13 changes: 12 additions & 1 deletion lib/generate-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = function generateDocument (baseDocument, router, basePath) {
openapi: minimumViableDocument.openapi
}, baseDocument, {
info: Object.assign({}, minimumViableDocument.info, baseDocument.info),
paths: Object.assign({}, minimumViableDocument.paths, baseDocument.paths)
paths: Object.assign({}, minimumViableDocument.paths, baseDocument.paths),
tags: [...minimumViableDocument.tags, ...(baseDocument.tags || [])]
})

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

if (schema.tags) {
schema.tags.forEach((tag) => {
if (!doc.tags.find((docTag) => docTag.name === tag)) {
doc.tags.push({
name: tag
})
}
})
}

const operation = Object.assign({}, schema)

// Add route params to schema
Expand Down
3 changes: 2 additions & 1 deletion lib/minimum-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
title: 'Express App',
version: '1.0.0'
},
paths: {}
paths: {},
tags: []
}
2 changes: 1 addition & 1 deletion test/_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = function () {
.get(`${openapi.defaultRoutePrefix}.json`)
.expect(200, (err, res) => {
assert(!err, err)
assert.strictEqual(Object.keys((res.body.paths))[0], '/{id}/')
assert.strictEqual(Object.keys((res.body.paths))[0], '(?:/([^/]+?))/')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this test was complaining for me, but when I run npm test on main, it was failing. By updating it to this regex, it was passing for me again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is failing for me too. I just created a pull request to fix it #74

done()
})
})
Expand Down
186 changes: 185 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ suite(name, function () {
title: 'Test App',
version: '1.0.0'
},
paths: {}
paths: {},
tags: []
})
})

Expand Down Expand Up @@ -653,6 +654,189 @@ suite(name, function () {
})
})

test('support tags', (done) => {
const app = express()
const oapi = openapi()

const firstTag = oapi.path({
tags: [
'first-tag'
],
responses: {
204: {
description: 'Successful response',
content: {
'application/json': {}
}
}
}
})

app.get('/endpoint', firstTag, (req, res) => {
res.status(204).send()
})

const secondTag = oapi.path({
tags: [
'second-tag',
'third-tag'
],
responses: {
204: {
description: 'Successful response',
content: {
'application/json': {}
}
}
}
})

app.get('/another-endpoint', secondTag, (req, res) => {
res.status(204).send()
})

app.use(oapi)

supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.expect(200, (err, res) => {
assert(!err, err)
SwaggerParser.validate(res.body, (err, api) => {
if (err) {
logDocument(api)

done(err)
}

assert(Object.keys(api.paths).length === 2)

assert.deepStrictEqual(api.tags, [{
name: 'first-tag'
}, {
name: 'second-tag'
}, {
name: 'third-tag'
}])

done()
})
})
})

test('support tags with base document', (done) => {
const app = express()
const oapi = openapi({
tags: [
{
name: 'first-tag'
}
]
})

const secondTag = oapi.path({
tags: [
'second-tag',
'third-tag'
],
responses: {
204: {
description: 'Successful response',
content: {
'application/json': {}
}
}
}
})

app.get('/another-endpoint', secondTag, (req, res) => {
res.status(204).send()
})

app.use(oapi)

supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.expect(200, (err, res) => {
assert(!err, err)
SwaggerParser.validate(res.body, (err, api) => {
if (err) {
logDocument(api)

done(err)
}

assert(Object.keys(api.paths).length === 1)

assert.deepStrictEqual(api.tags, [{
name: 'first-tag'
}, {
name: 'second-tag'
}, {
name: 'third-tag'
}])

done()
})
})
})

test('prefer global tags over method tags', (done) => {
const app = express()
const oapi = openapi({
tags: [
{
name: 'first-tag',
description: 'Description of first tag'
}
]
})

const firstTag = oapi.path({
tags: [
'first-tag',
'second-tag'
],
responses: {
204: {
description: 'Successful response',
content: {
'application/json': {}
}
}
}
})

app.get('/endpoint', firstTag, (req, res) => {
res.status(204).send()
})

app.use(oapi)

supertest(app)
.get(`${openapi.defaultRoutePrefix}.json`)
.expect(200, (err, res) => {
assert(!err, err)
SwaggerParser.validate(res.body, (err, api) => {
if (err) {
logDocument(api)

done(err)
}

assert(Object.keys(api.paths).length === 1)

assert.deepStrictEqual(api.tags, [{
name: 'first-tag',
description: 'Description of first tag'
}, {
name: 'second-tag'
}])

done()
})
})
})

// Other tests
require('./_validate')()
require('./_regexRoutes')()
Expand Down
Loading