Skip to content

Commit b0ac126

Browse files
SamerJaser96dpopp07
authored andcommitted
fix: raises warning for tag that hasnt been defined in global tags list
1 parent f8fa549 commit b0ac126

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ The supported rules are described below:
182182
##### operations
183183
| Rule | Description | Spec |
184184
| ---------------------------- | ----------------------------------------------------------------------------------- | -------- |
185+
| unused_tag | Flag a tag that is in operations and not listed in `tags` on the top level. | shared |
185186
| no_consumes_for_put_or_post | Flag `put` or `post` operations that do not have a `consumes` field. | swagger2 |
186187
| get_op_has_consumes | Flag `get` operations that contain a `consumes` field. | swagger2 |
187188
| no_produces | Flag operations that do not have a `produces` field (except for `head` and operations that return a 204). | swagger2 |
@@ -344,6 +345,7 @@ The default values for each rule are described below.
344345
###### operations
345346
| Rule | Default |
346347
| ---------------------------- | ------- |
348+
| unused_tag | warning |
347349
| no_operation_id | warning |
348350
| operation_id_case_convention | warning, lower_snake_case |
349351
| no_summary | warning |

src/.defaultsForValidator.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const defaults = {
2525
'operation_id_case_convention': ['warning', 'lower_snake_case'],
2626
'no_summary': 'warning',
2727
'no_array_responses': 'error',
28-
'parameter_order': 'warning'
28+
'parameter_order': 'warning',
29+
'unused_tag': 'warning'
2930
},
3031
'parameters': {
3132
'no_parameter_description': 'error',

src/plugins/validation/2and3/semantic-validators/operations-shared.js

+19
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
119119
});
120120
}
121121
}
122+
const hasOperationTags = op.tags && op.tags.length > 0;
123+
const hasGlobalTags = resolvedSpec.tags && resolvedSpec.tags.length > 0;
124+
const resolvedTags = [];
125+
if (hasOperationTags && hasGlobalTags) {
126+
for (let i = 0; i < resolvedSpec.tags.length; i++) {
127+
resolvedTags.push(resolvedSpec.tags[i].name);
128+
}
129+
for (let i = 0, len = op.tags.length; i < len; i++) {
130+
if (!resolvedTags.includes(op.tags[i])) {
131+
const checkStatus = config.unused_tag;
132+
if (checkStatus !== 'off') {
133+
result[checkStatus].push({
134+
path: `paths.${pathKey}.${opKey}.tags`,
135+
message: 'tag is not defined at the global level: ' + op.tags[i]
136+
});
137+
}
138+
}
139+
}
140+
}
122141

123142
const hasSummary =
124143
op.summary && op.summary.length > 0 && !!op.summary.toString().trim();

test/plugins/validation/2and3/operations-shared.js

+43
Original file line numberDiff line numberDiff line change
@@ -715,5 +715,48 @@ describe('validation plugin - semantic - operations-shared', function() {
715715
);
716716
expect(res.warnings.length).toEqual(0);
717717
});
718+
719+
it('should complain about an unused tag', function() {
720+
const spec = {
721+
tags: [
722+
{
723+
name: 'some tag'
724+
},
725+
{
726+
name: 'some other tag'
727+
}
728+
],
729+
paths: {
730+
'/': {
731+
get: {
732+
operationId: 'get_everything',
733+
tags: ['not a tag'],
734+
summary: 'get everything as a string',
735+
responses: {
736+
'200': {
737+
content: {
738+
'text/plain': {
739+
schema: {
740+
type: 'string'
741+
}
742+
}
743+
}
744+
}
745+
}
746+
}
747+
}
748+
}
749+
};
750+
751+
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
752+
console.log(res);
753+
expect(res.errors.length).toEqual(0);
754+
expect(res.warnings.length).toEqual(1);
755+
756+
expect(res.warnings[0].path).toEqual('paths./.get.tags');
757+
expect(res.warnings[0].message).toEqual(
758+
'tag is not defined at the global level: not a tag'
759+
);
760+
});
718761
});
719762
});

0 commit comments

Comments
 (0)