mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
[Issue#18] - Consistent parsing of all template variables
This commit is contained in:
23
lib/util.js
23
lib/util.js
@@ -89,23 +89,14 @@ module.exports = {
|
||||
* @returns {string} string after replacing /{pet}/ with /:pet/
|
||||
*/
|
||||
fixPathVariablesInUrl: function (url) {
|
||||
/*
|
||||
The first .replace:
|
||||
Handling path templating in request url if any
|
||||
only replace /{pet}<end-of-string> or /{pet}/
|
||||
https://regex101.com/r/Yqm2As/2
|
||||
|
||||
The second .replace:
|
||||
we also need to replace the scheme variables
|
||||
{scheme}://api.com
|
||||
needs to become
|
||||
{{scheme}}://api.com
|
||||
and scheme is already set as a collection variable
|
||||
*/
|
||||
|
||||
// All complicated logic removed
|
||||
// This simply replaces all instances of {text} with {{text}}
|
||||
// text cannot have any of these 3 chars: /{}
|
||||
// and {text} cannot be followed by a }
|
||||
// {{text}} will not be converted
|
||||
// https://regex101.com/r/9N1520/1
|
||||
return url
|
||||
.replace(/\/\{([a-zA-Z0-9\-\_]+)\}(\/|$)/g, '/:$1$2')
|
||||
.replace(/^\{([a-zA-Z0-9\-\_]+)\}\:/, '{{$1}}:');
|
||||
.replace(/(\{[^\/\{\}]+\})(?!\})/g, '{$1}');
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -1558,7 +1558,16 @@ describe('UTILITY FUNCTION TESTS ', function () {
|
||||
describe('fixPathVariablesInUrl function', function() {
|
||||
it('should convert a url with scheme and path variables', function(done) {
|
||||
var convertedUrl = Utils.fixPathVariablesInUrl('{scheme}://developer.uspto.gov/{path0}/segment/{path1}');
|
||||
expect(convertedUrl).to.equal('{{scheme}}://developer.uspto.gov/:path0/segment/:path1');
|
||||
expect(convertedUrl).to.equal('{{scheme}}://developer.uspto.gov/{{path0}}/segment/{{path1}}');
|
||||
|
||||
expect(Utils.fixPathVariablesInUrl('{{a}}')).to.equal('{{a}}');
|
||||
|
||||
expect(Utils.fixPathVariablesInUrl('{{a}}://{b}.com/{pathvar}/{morevar}'))
|
||||
.to.equal('{{a}}://{{b}}.com/{{pathvar}}/{{morevar}}');
|
||||
|
||||
expect(Utils.fixPathVariablesInUrl('{protocol}://{host}:{port}/{contextpath}/{restapi}'))
|
||||
.to.equal('{{protocol}}://{{host}}:{{port}}/{{contextpath}}/{{restapi}}');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user