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/
|
* @returns {string} string after replacing /{pet}/ with /:pet/
|
||||||
*/
|
*/
|
||||||
fixPathVariablesInUrl: function (url) {
|
fixPathVariablesInUrl: function (url) {
|
||||||
/*
|
// All complicated logic removed
|
||||||
The first .replace:
|
// This simply replaces all instances of {text} with {{text}}
|
||||||
Handling path templating in request url if any
|
// text cannot have any of these 3 chars: /{}
|
||||||
only replace /{pet}<end-of-string> or /{pet}/
|
// and {text} cannot be followed by a }
|
||||||
https://regex101.com/r/Yqm2As/2
|
// {{text}} will not be converted
|
||||||
|
// https://regex101.com/r/9N1520/1
|
||||||
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
|
|
||||||
*/
|
|
||||||
|
|
||||||
return url
|
return url
|
||||||
.replace(/\/\{([a-zA-Z0-9\-\_]+)\}(\/|$)/g, '/:$1$2')
|
.replace(/(\{[^\/\{\}]+\})(?!\})/g, '{$1}');
|
||||||
.replace(/^\{([a-zA-Z0-9\-\_]+)\}\:/, '{{$1}}:');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1558,7 +1558,16 @@ describe('UTILITY FUNCTION TESTS ', function () {
|
|||||||
describe('fixPathVariablesInUrl function', function() {
|
describe('fixPathVariablesInUrl function', function() {
|
||||||
it('should convert a url with scheme and path variables', function(done) {
|
it('should convert a url with scheme and path variables', function(done) {
|
||||||
var convertedUrl = Utils.fixPathVariablesInUrl('{scheme}://developer.uspto.gov/{path0}/segment/{path1}');
|
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();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user