Skip to content
This repository has been archived by the owner on Jul 2, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Encode dash in URI templates
  • Loading branch information
danielgtaylor committed Dec 2, 2015
1 parent ee9a835 commit a9c400c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
@@ -1,6 +1,7 @@
# Unreleased

- Implement support for parser annotations.
- Encode dashes (`-`) in URI template parameters.

# 0.3.2 - 2015-11-30

Expand Down
8 changes: 7 additions & 1 deletion src/uri-template.js
Expand Up @@ -16,7 +16,13 @@ export default function buildUriTemplate(basePath, href, pathObjectParameters =
const parameterNames = _.unique([].concat(pathObjectParameterNames, queryParameterNames));
const parameterNamesString = parameterNames.length ? `{?${parameterNames.join(',')}}` : '';

return `${basePath}${href}${parameterNamesString}`;
const full = `${basePath}${href}${parameterNamesString}`;

// Before returning, we replace instances of `-` with `%2d`, but only when
// they occur inside of a template variable.
return full.replace(/\{.*?\}/g, (match) => {
return match.replace('-', '%2d');
});
}

return basePath + href;
Expand Down
19 changes: 19 additions & 0 deletions test/uri-template.js
Expand Up @@ -94,6 +94,25 @@ describe('URI Template Handler', () => {
});
});

context('when there are parameters with dashes', () => {
const basePath = '/my-api';
const href = '/pet/{unique-id}';
const queryParameters = [
{
in: 'query',
description: 'Tags to filter by',
name: 'tag-names',
required: true,
type: 'string',
},
];

it('returns the correct URI', () => {
const hrefForResource = buildUriTemplate(basePath, href, [], queryParameters);
expect(hrefForResource).to.equal('/my-api/pet/{unique%2did}{?tag%2dnames}');
});
});

context('when there is a conflict in parameter names', () => {
const basePath = '/api';
const href = '/pet/findByTags';
Expand Down

0 comments on commit a9c400c

Please sign in to comment.