Skip to content

Commit

Permalink
feat(rules): update operation-tag-defined rule
Browse files Browse the repository at this point in the history
check for `tags` existence in `Operation` node`
  • Loading branch information
jeremyfiel committed Jan 31, 2025
1 parent 4681d7c commit c9d7e79
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/tasty-cougars-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Updated `operation-tag-defined` configurable rule.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { outdent } from 'outdent';
import { lintDocument } from '../../../lint';
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
import { BaseResolver } from '../../../resolve';

describe('Oas3 operation-tag-defined', () => {
it('should not report on operation object if at least one tag is defined', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.4
tags:
- name: a
paths:
/some:
get:
tags:
- a
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: { 'operation-tag-defined': 'error' } }),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`[]`);
});

it('should report on operation object if no tags are defined', async () => {
const document = parseYamlToDocument(
outdent`
openapi: 3.0.4
tags:
- name: a
paths:
/some:
get:
`,
'foobar.yaml'
);

const results = await lintDocument({
externalRefResolver: new BaseResolver(),
document,
config: await makeConfig({ rules: { 'operation-tag-defined': 'error' } }),
});

expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
[
{
"location": [
{
"pointer": "#/paths/~1some/get/tags",
"reportOnKey": true,
"source": "foobar.yaml",
},
],
"message": "Operation tags should be defined",
"ruleId": "operation-tag-defined",
"severity": "error",
"suggest": [],
},
]
`);
});
});
7 changes: 7 additions & 0 deletions packages/core/src/rules/common/operation-tag-defined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export const OperationTagDefined: Oas3Rule | Oas2Rule = () => {
definedTags = new Set((root.tags ?? []).map((t) => t.name));
},
Operation(operation: Oas2Operation | Oas3Operation, { report, location }: UserContext) {
if (!operation?.hasOwnProperty('tags')) {
report({
message: `Operation tags should be defined`,
location: location.child('tags').key(),
});
return;
}
if (operation.tags) {
for (let i = 0; i < operation.tags.length; i++) {
if (!definedTags.has(operation.tags[i])) {
Expand Down

0 comments on commit c9d7e79

Please sign in to comment.