diff --git a/__tests__/operation.test.ts b/__tests__/operation.test.ts index 8998c13d..246176ea 100644 --- a/__tests__/operation.test.ts +++ b/__tests__/operation.test.ts @@ -988,12 +988,48 @@ describe('#getOperationId()', () => { expect(operation.getOperationId()).toBe('get_multiple-combo-auths-duped'); }); + it('should not sanitize underscores by default', () => { + const spec = Oas.init({ + openapi: '3.1.0', + info: { + title: 'testing', + version: '1.0.0', + }, + paths: { + '/ac_eq_hazard/18.0': { + post: {}, + }, + }, + }); + + const operation = spec.operation('/ac_eq_hazard/18.0', 'post'); + expect(operation.getOperationId()).toBe('post_ac-eq-hazard-18-0'); + }); + describe('`camelCase` option', () => { it('should create a camel cased operationId if one does not exist', () => { const operation = multipleSecurities.operation('/multiple-combo-auths-duped', 'get'); expect(operation.getOperationId({ camelCase: true })).toBe('getMultipleComboAuthsDuped'); }); + it('should sanitize underscores in a path', () => { + const spec = Oas.init({ + openapi: '3.1.0', + info: { + title: 'testing', + version: '1.0.0', + }, + paths: { + '/ac_eq_hazard/18.0': { + post: {}, + }, + }, + }); + + const operation = spec.operation('/ac_eq_hazard/18.0', 'post'); + expect(operation.getOperationId({ camelCase: true })).toBe('postAc_eq_hazard180'); + }); + it('should not double up on a method prefix if the path starts with the method', () => { const spec = Oas.init({ openapi: '3.1.0', diff --git a/src/operation.ts b/src/operation.ts index 247f2a90..0ee48c9d 100644 --- a/src/operation.ts +++ b/src/operation.ts @@ -340,8 +340,10 @@ export default class Operation { camelCase: boolean; }): string { function sanitize(id: string) { + // We aren't sanitizing underscores here by default in order to preserve operation IDs that + // were already generated with this method in the past. return id - .replace(/[^a-zA-Z0-9_]/g, '-') // Remove weird characters + .replace(opts?.camelCase ? /[^a-zA-Z0-9_]/g : /[^a-zA-Z0-9]/g, '-') // Remove weird characters .replace(/--+/g, '-') // Remove double --'s .replace(/^-|-$/g, ''); // Don't start or end with - }