mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Fixing issue #309
This commit is contained in:
@@ -512,6 +512,24 @@ module.exports = {
|
||||
return reqParam;
|
||||
},
|
||||
|
||||
dereferenceFromOutOfComponents: function (spec, reference) {
|
||||
let splitRef = reference.split('/'),
|
||||
resolvedContent;
|
||||
|
||||
splitRef = splitRef.slice(1).map((elem) => {
|
||||
// https://swagger.io/docs/specification/using-ref#escape
|
||||
// since / is the default delimiter, slashes are escaped with ~1
|
||||
return decodeURIComponent(
|
||||
elem
|
||||
.replace(/~1/g, '/')
|
||||
.replace(/~0/g, '~')
|
||||
);
|
||||
});
|
||||
|
||||
resolvedContent = deref._getEscaped(spec, splitRef);
|
||||
return resolvedContent;
|
||||
},
|
||||
|
||||
/**
|
||||
* Generates a Trie-like folder structure from the root path object of the OpenAPI specification.
|
||||
* @param {Object} spec - specification in json format
|
||||
@@ -585,6 +603,10 @@ module.exports = {
|
||||
|
||||
pathLength = currentPath.length;
|
||||
|
||||
if (currentPathObject.hasOwnProperty('$ref')) {
|
||||
currentPathObject = this.dereferenceFromOutOfComponents(spec, currentPathObject.$ref);
|
||||
}
|
||||
|
||||
// get method names available for this path
|
||||
pathMethods = getPathMethods(Object.keys(currentPathObject));
|
||||
|
||||
@@ -799,6 +821,10 @@ module.exports = {
|
||||
collectionVariables,
|
||||
pathLevelServers = '';
|
||||
|
||||
if (currentPathObject.hasOwnProperty('$ref')) {
|
||||
currentPathObject = this.dereferenceFromOutOfComponents(spec, currentPathObject.$ref);
|
||||
}
|
||||
|
||||
// discard the leading slash, if it exists
|
||||
if (path[0] === '/') {
|
||||
path = path.substring(1);
|
||||
|
||||
1216
test/data/valid_openapi/petstore-detailed-referenced-path.yaml
Normal file
1216
test/data/valid_openapi/petstore-detailed-referenced-path.yaml
Normal file
File diff suppressed because it is too large
Load Diff
114
test/data/valid_openapi/referencedPathFromOutOfComponents.yaml
Normal file
114
test/data/valid_openapi/referencedPathFromOutOfComponents.yaml
Normal file
@@ -0,0 +1,114 @@
|
||||
openapi: "3.0.0"
|
||||
info:
|
||||
version: 1.0.0
|
||||
title: Swagger Petstore
|
||||
license:
|
||||
name: MIT
|
||||
servers:
|
||||
- url: http://petstore.swagger.io/v1
|
||||
paths:
|
||||
/pets:
|
||||
get:
|
||||
summary: List all pets
|
||||
operationId: listPets
|
||||
tags:
|
||||
- pets
|
||||
parameters:
|
||||
- name: limit
|
||||
in: query
|
||||
description: How many items to return at one time (max 100)
|
||||
required: false
|
||||
schema:
|
||||
type: integer
|
||||
format: int32
|
||||
responses:
|
||||
'200':
|
||||
description: A paged array of pets
|
||||
headers:
|
||||
x-next:
|
||||
description: A link to the next page of responses
|
||||
schema:
|
||||
type: string
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Pets"
|
||||
default:
|
||||
description: unexpected error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
post:
|
||||
summary: Create a pet
|
||||
operationId: createPets
|
||||
tags:
|
||||
- pets
|
||||
responses:
|
||||
'201':
|
||||
description: Null response
|
||||
default:
|
||||
description: unexpected error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
/pets/{petId}:
|
||||
$ref: "#/x-operations/getAPet"
|
||||
components:
|
||||
schemas:
|
||||
Pet:
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
format: int64
|
||||
name:
|
||||
type: string
|
||||
tag:
|
||||
type: string
|
||||
Pets:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Pet"
|
||||
Error:
|
||||
type: object
|
||||
required:
|
||||
- code
|
||||
- message
|
||||
properties:
|
||||
code:
|
||||
type: integer
|
||||
format: int32
|
||||
message:
|
||||
type: string
|
||||
x-operations:
|
||||
getAPet:
|
||||
get:
|
||||
summary: Info for a specific pet
|
||||
operationId: showPetById
|
||||
tags:
|
||||
- pets
|
||||
parameters:
|
||||
- name: petId
|
||||
in: path
|
||||
required: true
|
||||
description: The id of the pet to retrieve
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Expected response to a valid request
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Pet"
|
||||
default:
|
||||
description: unexpected error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/Error"
|
||||
@@ -58,7 +58,11 @@ describe('CONVERT FUNCTION TESTS ', function() {
|
||||
allHTTPMethodsSpec = path.join(__dirname, VALID_OPENAPI_PATH, '/all-http-methods.yaml'),
|
||||
invalidNullInfo = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-null-info.json'),
|
||||
invalidNullInfoTitle = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-info-null-title.json'),
|
||||
invalidNullInfoVersion = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-info-null-version.json');
|
||||
invalidNullInfoVersion = path.join(__dirname, INVALID_OPENAPI_PATH, '/invalid-info-null-version.json'),
|
||||
referencedPathFromOutOfComponents =
|
||||
path.join(__dirname, VALID_OPENAPI_PATH + '/referencedPathFromOutOfComponents.yaml'),
|
||||
referencedPathFromOutOfComponentsTagsStrategy =
|
||||
path.join(__dirname, VALID_OPENAPI_PATH + '/petstore-detailed-referenced-path.yaml');
|
||||
|
||||
|
||||
it('Should add collection level auth with type as `bearer`' +
|
||||
@@ -1275,6 +1279,50 @@ describe('CONVERT FUNCTION TESTS ', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('[Github #309 - Should convert a path when is referenced ' +
|
||||
'from a different place than components]', function() {
|
||||
|
||||
it('Should convert and include the referenced paths' +
|
||||
referencedPathFromOutOfComponents, function(done) {
|
||||
Converter.convert({ type: 'file', data:
|
||||
referencedPathFromOutOfComponents }, {}, (err, conversionResult) => {
|
||||
expect(err).to.be.null;
|
||||
expect(conversionResult.result).to.equal(true);
|
||||
expect(conversionResult.output.length).to.equal(1);
|
||||
expect(conversionResult.output[0].type).to.equal('collection');
|
||||
expect(conversionResult.output[0].data).to.have.property('info');
|
||||
expect(conversionResult.output[0].data).to.have.property('item');
|
||||
expect(conversionResult.output[0].data.item[0].item.length)
|
||||
.to.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('Should convert and include the referenced paths using tags as folder strategy' +
|
||||
referencedPathFromOutOfComponentsTagsStrategy, function(done) {
|
||||
Converter.convert(
|
||||
{
|
||||
type: 'file',
|
||||
data: referencedPathFromOutOfComponentsTagsStrategy
|
||||
},
|
||||
{
|
||||
folderStrategy: 'Tags'
|
||||
},
|
||||
(err, conversionResult) => {
|
||||
expect(err).to.be.null;
|
||||
expect(conversionResult.result).to.equal(true);
|
||||
expect(conversionResult.output.length).to.equal(1);
|
||||
expect(conversionResult.output[0].type).to.equal('collection');
|
||||
expect(conversionResult.output[0].data).to.have.property('info');
|
||||
expect(conversionResult.output[0].data).to.have.property('item');
|
||||
expect(conversionResult.output[0].data.item[0].item.length)
|
||||
.to.equal(7);
|
||||
done();
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Converting swagger 2.0 files', function() {
|
||||
|
||||
Reference in New Issue
Block a user