From 379a4a9307b342fc5c777c5261fa14bcad971499 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 10 Apr 2019 16:57:52 -0700 Subject: [PATCH] feat(rest): allow `-` to be used for path template variable names Some specs use `{account-id}` as part of the path. --- .../src/__tests__/unit/router/openapi-path.unit.ts | 11 ++++++++++- packages/rest/src/router/openapi-path.ts | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/rest/src/__tests__/unit/router/openapi-path.unit.ts b/packages/rest/src/__tests__/unit/router/openapi-path.unit.ts index dc377bd887ad..31d66fe01e99 100644 --- a/packages/rest/src/__tests__/unit/router/openapi-path.unit.ts +++ b/packages/rest/src/__tests__/unit/router/openapi-path.unit.ts @@ -4,7 +4,6 @@ // License text available at https://opensource.org/licenses/MIT import {expect} from '@loopback/testlab'; - import {validateApiPath} from '../../..'; describe('validateApiPath', () => { @@ -42,6 +41,16 @@ describe('validateApiPath', () => { expect(path).to.eql('/{_foo}/{bar}'); }); + it('allows /{foo-bar}', () => { + const path = validateApiPath('/{foo-bar}'); + expect(path).to.eql('/{foo-bar}'); + }); + + it('allows /{foo}-{bar}', () => { + const path = validateApiPath('/{foo}-{bar}'); + expect(path).to.eql('/{foo}-{bar}'); + }); + it('disallows /:foo/bar', () => { disallows('/:foo/bar'); }); diff --git a/packages/rest/src/router/openapi-path.ts b/packages/rest/src/router/openapi-path.ts index c88c839f79c4..f4f1d7c97e5f 100644 --- a/packages/rest/src/router/openapi-path.ts +++ b/packages/rest/src/router/openapi-path.ts @@ -13,7 +13,7 @@ import pathToRegExp = require('path-to-regexp'); * allows `[A-Za-z0-9_]` */ const POSSIBLE_VARNAME_PATTERN = /\{([^\}]+)\}/g; -const INVALID_VARNAME_PATTERN = /\{([^\}]*[^\w\}][^\}]*)\}/; +const INVALID_VARNAME_PATTERN = /\{([^\}]*[^\w\-\}][^\}]*)\}/; /** * Validate the path to be compatible with OpenAPI path template. No parameter