Skip to content

Commit

Permalink
refactor: update getTags() function to handle webhooks (#833)
Browse files Browse the repository at this point in the history
* refactor: update getTags() function to handle webhooks

* chore: bump @readme/oas-examples

* chore: update getTags() comment since it supports webhooks

* test: update test to correct casing
  • Loading branch information
darrenyong authored Nov 2, 2023
1 parent f6df1dd commit 00d0e80
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion packages/oas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,6 @@ export default class Oas {
/**
* Return an array of all tag names that exist on this API definition.
*
* Note: This method right now does **not** factor in webhooks that have tags.
*
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#oasObject}
* @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#openapi-object}
Expand All @@ -758,6 +757,20 @@ export default class Oas {
});
});

Object.entries(this.getWebhooks()).forEach(([path, webhooks]) => {
Object.values(webhooks).forEach(webhook => {
const tags = webhook.getTags();
if (setIfMissing && !tags.length) {
allTags.add(path);
return;
}

tags.forEach(tag => {
allTags.add(tag.name);
});
});
});

return Array.from(allTags);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/oas/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ describe('#operation()', () => {
responses: {
200: expect.any(Object),
},
tags: expect.any(Array),
});
});

Expand Down Expand Up @@ -1613,8 +1614,9 @@ describe('#getWebhooks()', () => {
});

describe('#getTags()', () => {
it('should all tags that are present in a definition', () => {
it('should return all tags that are present in a definition', () => {
expect(petstore.getTags()).toStrictEqual(['pet', 'store', 'user']);
expect(webhooks.getTags()).toStrictEqual(['Webhooks']);
});

describe('setIfMissing option', () => {
Expand Down

0 comments on commit 00d0e80

Please sign in to comment.