Skip to content

Commit

Permalink
fix: definition name util (#1865)
Browse files Browse the repository at this point in the history
* fix: definition name util

* update snapshot
  • Loading branch information
Oprysk authored Jan 19, 2022
1 parent 3e47932 commit 95a7347
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"required": Array [
"type",
],
"title": "schemas",
"title": "Dog",
"type": "object",
},
"title": "Dog",
Expand Down Expand Up @@ -1488,7 +1488,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"required": Array [
"type",
],
"title": "schemas",
"title": "Cat",
"type": "object",
},
"title": "Cat",
Expand Down Expand Up @@ -2454,7 +2454,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"required": Array [
"type",
],
"title": "schemas",
"title": "Dog",
"type": "object",
},
"title": "Dog",
Expand Down
11 changes: 11 additions & 0 deletions src/utils/__tests__/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
sortByRequired,
humanizeNumberRange,
getContentWithLegacyExamples,
getDefinitionName,
} from '../';

import { FieldModel, OpenAPIParser, RedocNormalizedOptions } from '../../services';
Expand Down Expand Up @@ -1269,4 +1270,14 @@ describe('Utils', () => {
expect(content['text/plain']).toStrictEqual(info.content['text/plain']);
});
});

describe('getDefinitionName', () => {
test('should return the name if pointer match regex', () => {
expect(getDefinitionName('#/components/schemas/Call')).toEqual('Call');
});
test("should return the `undefined` if pointer not match regex or it's absent", () => {
expect(getDefinitionName('#/test/path/Call')).toBeUndefined();
expect(getDefinitionName()).toBeUndefined();
});
});
});
9 changes: 5 additions & 4 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,15 @@ export function langFromMime(contentType: string): string {
return 'clike';
}

const DEFINITION_NAME_REGEX = /^#\/components\/(schemas|pathItems)\/([^/]+)$/;

export function isNamedDefinition(pointer?: string): boolean {
return /^#\/components\/(schemas|pathItems)\/[^\/]+$/.test(pointer || '');
return DEFINITION_NAME_REGEX.test(pointer || '');
}

export function getDefinitionName(pointer?: string): string | undefined {
if (!pointer) return undefined;
const match = pointer.match(/^#\/components\/(schemas|pathItems)\/([^\/]+)$/);
return match === null ? undefined : match[1];
const [name] = pointer?.match(DEFINITION_NAME_REGEX)?.reverse() || [];
return name;
}

function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined {
Expand Down

0 comments on commit 95a7347

Please sign in to comment.