Skip to content

Commit

Permalink
feat: add case insensitivity for reducer (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcole19 authored Mar 3, 2023
1 parent faf74c0 commit 74672da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions __tests__/lib/reducer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('reducer', () => {

describe('tag reduction', () => {
it('should reduce by tags', () => {
const reduced = reducer(petstore as any, { tags: ['store'] });
const reduced = reducer(petstore as any, { tags: ['Store'] });

expect(reduced.tags).toStrictEqual([{ name: 'store', description: 'Access to Petstore orders' }]);

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('reducer', () => {
it('should reduce by paths', () => {
const reduced = reducer(petstore as any, {
paths: {
'/store/order/{orderId}': ['get'],
'/store/ORDER/{orderId}': ['Get'],
},
});

Expand All @@ -86,7 +86,7 @@ describe('reducer', () => {
it('should support method wildcards', () => {
const reduced = reducer(petstore as any, {
paths: {
'/store/order/{orderId}': '*',
'/STORE/order/{orderId}': '*',
},
});

Expand Down
18 changes: 12 additions & 6 deletions src/lib/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function accumulateUsedRefs(schema: Record<string, unknown>, $refs: Set<string>,
* new definition that just contains those tags or path + methods.
*
* @example <caption>Reduce by an array of tags only.</caption>
* { APIDEFINITION, { tags: ['pet] } }
* { APIDEFINITION, { tags: ['pet'] } }
*
* @example <caption>Reduce by a specific path and methods.</caption>
* { APIDEFINITION, { paths: { '/pet': ['get', 'post'] } } }
Expand All @@ -65,8 +65,14 @@ function accumulateUsedRefs(schema: Record<string, unknown>, $refs: Set<string>,
* @param definition A valid OpenAPI 3.x definition
*/
export default function reducer(definition: OASDocument, opts: ReducerOptions = {}) {
const reduceTags = 'tags' in opts ? opts.tags : [];
const reducePaths = 'paths' in opts ? opts.paths : {};
// Convert tags and paths to lowercase since casing should not matter
const reduceTags = 'tags' in opts ? opts.tags.map(tag => tag.toLowerCase()) : [];
const reducePaths = 'paths' in opts ? Object.entries(opts.paths).reduce((acc: Record<string, string[] | string>, [key, value]) => {
const newKey = key.toLowerCase();
const newValue = Array.isArray(value) ? value.map(v => v.toLowerCase()) : value.toLowerCase();
acc[newKey] = newValue;
return acc;
}, {}) : {};

const $refs: Set<string> = new Set();
const usedTags: Set<string> = new Set();
Expand All @@ -81,7 +87,7 @@ export default function reducer(definition: OASDocument, opts: ReducerOptions =
if ('paths' in reduced) {
Object.keys(reduced.paths).forEach(path => {
if (Object.keys(reducePaths).length) {
if (!(path in reducePaths)) {
if (!(path.toLowerCase() in reducePaths)) {
delete reduced.paths[path];
return;
}
Expand All @@ -91,7 +97,7 @@ export default function reducer(definition: OASDocument, opts: ReducerOptions =
// If this method is `parameters` we should always retain it.
if (method !== 'parameters') {
if (Object.keys(reducePaths).length) {
if (reducePaths[path] !== '*' && Array.isArray(reducePaths[path]) && !reducePaths[path].includes(method)) {
if (reducePaths[path.toLowerCase()] !== '*' && Array.isArray(reducePaths[path.toLowerCase()]) && !reducePaths[path.toLowerCase()].includes(method)) {
delete reduced.paths[path][method];
return;
}
Expand All @@ -105,7 +111,7 @@ export default function reducer(definition: OASDocument, opts: ReducerOptions =
if (!('tags' in operation)) {
delete reduced.paths[path][method];
return;
} else if (!reduceTags.filter(value => operation.tags.includes(value)).length) {
} else if (!operation.tags.filter(tag => reduceTags.includes(tag.toLowerCase())).length) {
delete reduced.paths[path][method];
return;
}
Expand Down

0 comments on commit 74672da

Please sign in to comment.