add inline objects to ref map

This commit is contained in:
Luis Tejeda
2022-07-04 13:34:12 -05:00
parent e78ce5d1df
commit 8e0ead9562
6 changed files with 132 additions and 7 deletions

View File

@@ -323,7 +323,9 @@ function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, ve
[, local] = tempRef.split(localPointer), [, local] = tempRef.split(localPointer),
nodeFromData, nodeFromData,
refHasContent = false, refHasContent = false,
parseResult; parseResult,
newRefInDoc,
inline;
newValue = Object.assign({}, this.node); newValue = Object.assign({}, this.node);
nodeFromData = findNodeFromPath(tempRef, allData); nodeFromData = findNodeFromPath(tempRef, allData);
@@ -337,15 +339,21 @@ function getReferences (currentNode, isOutOfRoot, pathSolver, parentFilename, ve
} }
this.update({ $ref: tempRef }); this.update({ $ref: tempRef });
if (nodeTrace.length === 0) {
inline = true;
newRefInDoc = localPointer + jsonPointerLevelSeparator + traceToParent.join(jsonPointerLevelSeparator);
}
nodeReferenceDirectory[tempRef] = { nodeReferenceDirectory[tempRef] = {
local, local,
keyInComponents: nodeTrace, keyInComponents: nodeTrace,
node: newValue, node: newValue,
reference: referenceInDocument, reference: inline ? newRefInDoc : referenceInDocument,
traceToParent, traceToParent,
parentNodeKey: parentFilename, parentNodeKey: parentFilename,
mainKeyInTrace: nodeTrace[nodeTrace.length - 1], mainKeyInTrace: nodeTrace[nodeTrace.length - 1],
refHasContent refHasContent,
inline
}; };
mainKeys[componentKey] = tempRef; mainKeys[componentKey] = tempRef;
@@ -469,7 +477,6 @@ function generateComponentsObject (documentContext, rootContent, refTypeResolver
} }
else { else {
refData.nodeContent = documentContext.nodeContents[nodeRef]; refData.nodeContent = documentContext.nodeContents[nodeRef];
refData.inline = refData.keyInComponents.length === 0;
} }
if (local) { if (local) {
let contentFromTrace = getContentFromTrace(refData.nodeContent, local); let contentFromTrace = getContentFromTrace(refData.nodeContent, local);
@@ -524,14 +531,17 @@ function generateComponentsWrapper(parsedOasObject) {
* Generates a map of generated refernce to the original reference * Generates a map of generated refernce to the original reference
* *
* @param {object} globalReferences - Global references present at each root file context * @param {object} globalReferences - Global references present at each root file context
* @returns {object} referncce map * @returns {object} reference map
*/ */
function getReferenceMap(globalReferences) { function getReferenceMap(globalReferences) {
const output = {}; const output = {};
_.forEach(globalReferences, (globalReference, refKey) => { _.forEach(globalReferences, (globalReference, refKey) => {
if (_.isString(refKey) && _.isString(globalReference.reference)) { if (_.isString(refKey) && _.isString(globalReference.reference)) {
output[globalReference.reference] = refKey; output[globalReference.reference] = {
path: refKey,
type: globalReference.inline ? 'inline' : 'component'
};
} }
}); });

View File

@@ -0,0 +1,12 @@
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
favFood:
type: string

View File

@@ -0,0 +1,9 @@
description: Returns all pets alesuada ac...
operationId: findPets
responses:
"200":
description: pet response
content:
application/json:
schema:
$ref: "./pet.yaml"

View File

@@ -0,0 +1,12 @@
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string

View File

@@ -0,0 +1,30 @@
openapi: "3.0.2"
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
paths:
/pets:
get:
$ref: "./path.yaml"
/cat:
get:
description: Returns one cat
operationId: findcat
responses:
"200":
description: cat response
content:
application/json:
schema:
$ref: "./cat.yaml"

View File

@@ -42,7 +42,8 @@ let expect = require('chai').expect,
schemaCollision = path.join(__dirname, BUNDLES_FOLDER + '/schema_collision_from_responses'), schemaCollision = path.join(__dirname, BUNDLES_FOLDER + '/schema_collision_from_responses'),
schemaCollisionWRootComponent = path.join(__dirname, BUNDLES_FOLDER + '/schema_collision_w_root_components'), schemaCollisionWRootComponent = path.join(__dirname, BUNDLES_FOLDER + '/schema_collision_w_root_components'),
nestedExamplesAsValue = path.join(__dirname, BUNDLES_FOLDER + '/nested_examples_as_value'), nestedExamplesAsValue = path.join(__dirname, BUNDLES_FOLDER + '/nested_examples_as_value'),
referencedProperties = path.join(__dirname, BUNDLES_FOLDER + '/referenced_properties'); referencedProperties = path.join(__dirname, BUNDLES_FOLDER + '/referenced_properties'),
referencedPath = path.join(__dirname, BUNDLES_FOLDER + '/referenced_path');
describe('bundle files method - 3.0', function () { describe('bundle files method - 3.0', function () {
it('Should return bundled file as json - schema_from_response', async function () { it('Should return bundled file as json - schema_from_response', async function () {
@@ -2359,6 +2360,57 @@ describe('bundle files method - 3.0', function () {
expect(res.result).to.be.true; expect(res.result).to.be.true;
expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected); expect(JSON.stringify(JSON.parse(res.output.data[0].bundledContent), null, 2)).to.be.equal(expected);
}); });
it('Should return correct map with inline and components resolving', async function () {
let contentRootFile = fs.readFileSync(referencedPath + '/root.yaml', 'utf8'),
path = fs.readFileSync(referencedPath + '/path.yaml', 'utf8'),
pet = fs.readFileSync(referencedPath + '/pet.yaml', 'utf8'),
cat = fs.readFileSync(referencedPath + '/cat.yaml', 'utf8'),
expected = {
'#/paths//pets/get': {
path: '/path.yaml',
type: 'inline'
},
'#/components/schemas/_cat.yaml': {
path: '/cat.yaml',
type: 'component'
}
},
input = {
type: 'multiFile',
specificationVersion: '3.0',
rootFiles: [
{
path: '/root.yaml'
}
],
data: [
{
path: '/root.yaml',
content: contentRootFile
},
{
path: '/pet.yaml',
content: pet
},
{
path: '/path.yaml',
content: path
},
{
path: '/cat.yaml',
content: cat
}
],
options: { includeReferenceMap: true },
bundleFormat: 'JSON'
};
const res = await Converter.bundle(input);
expect(res).to.not.be.empty;
expect(res.result).to.be.true;
expect(res.output.data[0].referenceMap).to.deep.equal(expected);
});
}); });
describe('getReferences method when node does not have any reference', function() { describe('getReferences method when node does not have any reference', function() {