diff --git a/lib/bundle.js b/lib/bundle.js index 5f14b34..4f752a9 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -56,10 +56,7 @@ function removeLocalReferenceFromPath(refValue) { */ function calculatePath(parentFileName, referencePath) { let currentDirName = path.dirname(parentFileName), - fileDirName = path.dirname(referencePath), - refDirName = currentDirName === fileDirName ? - referencePath : - path.join(currentDirName, referencePath); + refDirName = path.join(currentDirName, referencePath); return refDirName; } @@ -191,10 +188,9 @@ function setValueInComponents(keyInComponents, components, value) { * Return a trace from the current node's root to the place where we find a $ref * @param {object} nodeContext - The current node we are processing * @param {object} property - The current property that contains the $ref - * @param {string} parentFilename - The parent's filename * @returns {array} The trace to the place where the $ref appears */ -function getTraceFromParentKeyInComponents(nodeContext, property, parentFilename) { +function getTraceFromParentKeyInComponents(nodeContext, property) { const parents = [...nodeContext.parents].reverse(), isArrayKeyRegexp = new RegExp('^\\d$', 'g'), key = nodeContext.key, @@ -206,11 +202,7 @@ function getTraceFromParentKeyInComponents(nodeContext, property, parentFilename parentKeys : [key, ...parentKeys], nodeTrace = getRootFileTrace(nodeParentsKey), - cleanFileName = (filename) => { - const [file, local] = filename.split('#'); - return [calculatePath(parentFilename, file), local]; - }, - [file, local] = cleanFileName(property), + [file, local] = property.split('#'), [keyTraceInComponents, inComponents] = getKeyInComponents(nodeTrace, file, local); return [keyTraceInComponents, inComponents]; } @@ -238,7 +230,7 @@ function getReferences (currentNode, refTypeResolver, pathSolver, parentFilename ); if (hasReferenceTypeKey) { const tempRef = calculatePath(parentFilename, property.$ref), - [nodeTrace, inComponents] = getTraceFromParentKeyInComponents(this, tempRef, parentFilename), + [nodeTrace, inComponents] = getTraceFromParentKeyInComponents(this, tempRef), referenceInDocument = getJsonPointerRelationToRoot( jsonPointerEncodeAndReplace, tempRef, diff --git a/test/data/petstore separate yaml/bundleExp.yaml b/test/data/petstore separate yaml/bundleExp.yaml new file mode 100644 index 0000000..e1d4781 --- /dev/null +++ b/test/data/petstore separate yaml/bundleExp.yaml @@ -0,0 +1,179 @@ +openapi: '3.0' +info: + version: 1.0.0 + title: Swagger Petstore + description: >- + A sample API that uses a petstore as an example to demonstrate features in + the swagger-2.0 specification + termsOfService: 'http://swagger.io/terms/' + contact: + name: Swagger API Team + email: apiteam@swagger.io + url: 'http://swagger.io' + license: + name: Apache 2.0 + url: 'https://www.apache.org/licenses/LICENSE-2.0.html' +host: petstore.swagger.io +basePath: /api +schemes: + - http +consumes: + - application/json +produces: + - application/json +paths: + /pets: + get: + description: > + Returns all pets from the system that the user has access to + + Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus + sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. + Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis + mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse + platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean + pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras + quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna + volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. + Praesent enim enim, gravida ac sodales sed, placerat id erat. + Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum + libero. Morbi euismod sagittis libero sed lacinia. + + + Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu + condimentum ligula luctus nec. Phasellus semper velit eget aliquet + faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum + lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius + odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. + Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada + accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit + amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, + accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed + vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, + euismod sapien. + operationId: findPets + parameters: + - $ref: '#/components/parameters/~1spec~1parameters.yaml%23~1tagsParam' + - $ref: '#/components/parameters/~1spec~1parameters.yaml%23~1limitsParam' + responses: + '200': + description: pet response + schema: + type: array + items: + $ref: '#/components/schemas/~1spec~1Pet.yaml' + default: + description: unexpected error + schema: + $ref: '#/components/schemas/~1common~1Error.yaml' + post: + description: Creates a new pet in the store. Duplicates are allowed + operationId: addPet + parameters: + - name: pet + in: body + description: Pet to add to the store + required: true + schema: + $ref: '#/components/schemas/~1spec~1NewPet.yaml' + responses: + '200': + description: pet response + schema: + $ref: '#/components/schemas/~1spec~1Pet.yaml' + default: + description: unexpected error + schema: + $ref: '#/components/schemas/~1common~1Error.yaml' + '/pets/{id}': + get: + description: >- + Returns a user based on a single ID, if the user does not have access to + the pet + operationId: find pet by id + parameters: + - name: id + in: path + description: ID of pet to fetch + required: true + type: integer + format: int64 + responses: + '200': + description: pet response + schema: + $ref: '#/components/schemas/~1spec~1Pet.yaml' + default: + description: unexpected error + schema: + $ref: '#/components/schemas/~1common~1Error.yaml' + delete: + description: deletes a single pet based on the ID supplied + operationId: deletePet + parameters: + - name: id + in: path + description: ID of pet to delete + required: true + type: integer + format: int64 + responses: + '204': + description: pet deleted + default: + description: unexpected error + schema: + $ref: '#/components/schemas/~1common~1Error.yaml' +components: + parameters: + /spec/parameters.yaml#/tagsParam: + name: tags + in: query + description: tags to filter by + required: false + type: array + collectionFormat: csv + items: + type: string + /spec/parameters.yaml#/limitsParam: + name: limit + in: query + description: maximum number of results to return + required: false + type: integer + format: int32 + schemas: + /spec/Pet.yaml: + type: object + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + /common/Error.yaml: + type: object + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string + /spec/NewPet.yaml: + type: object + allOf: + - $ref: '#/components/schemas/~1spec~1Pet.yaml' + - required: + - name + properties: + description: + type: integer + format: int64 diff --git a/test/unit/bundle.test.js b/test/unit/bundle.test.js index c5dbb46..3145e0d 100644 --- a/test/unit/bundle.test.js +++ b/test/unit/bundle.test.js @@ -217,7 +217,7 @@ describe('bundle files method - 3.0', function () { limitParameter = fs.readFileSync(petstoreFolder + '/parameters/query/limit.yaml', 'utf8'), petIdParameter = fs.readFileSync(petstoreFolder + '/parameters/path/petId.yaml', 'utf8'), errorCommon = fs.readFileSync(petstoreFolder + '/common/Error.yaml', 'utf8'), - expected = fs.readFileSync(petstoreFolder + '/bundleExpected.json', 'utf8'), + expected = fs.readFileSync(petstoreFolder + '/bundleExp.yaml', 'utf8'), input = { type: 'folder', specificationVersion: '3.0', @@ -228,7 +228,7 @@ describe('bundle files method - 3.0', function () { } ], options: {}, - bundleFormat: 'JSON', + bundleFormat: 'yaml', data: [ { path: '/spec/NewPet.yaml', @@ -299,7 +299,7 @@ describe('bundle files method - 3.0', function () { const res = await Converter.bundle(input); expect(res).to.not.be.empty; expect(res.result).to.be.true; - expect(JSON.stringify(res.output.data.bundledContent, null, 2)).to.be.equal(expected); + expect(res.output.data.bundledContent).to.be.equal(expected); }); it('Should return bundled file - with_parameters', async function () {