Skip to content

Commit

Permalink
fix: flatten definitions on bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Dec 23, 2021
1 parent 2147374 commit 7ef5b16
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,14 @@ JSONSchemaSequelizer.bundle = (schemas, definitions, description) => {
}

if (!definitions[def].properties) {
Object.assign(dump.definitions[def], util.fixRefs(definitions[def].definitions));
Object.assign(dump.definitions[def], definitions[def].definitions);
} else {
Object.assign(dump.definitions, util.fixRefs(definitions[def].definitions));
Object.assign(dump.definitions[def], util.fixRefs(definitions[def]));
Object.assign(dump.definitions, definitions[def].definitions);
Object.assign(dump.definitions[def], definitions[def]);
}
});

schemas.forEach(x => {
dump.definitions[x.id] = util.fixRefs(x);
});
util.flattenRefs(definitions);

return dump;
};
Expand All @@ -390,13 +388,34 @@ JSONSchemaSequelizer.generate = (dump, models, squash, globalOptions) => {
});
}

// identical setup as json-schema-faker
const fixedRefs = {
order: 1,
canRead: true,
read: (file, callback) => {
const rel = file.url.split('/').pop();
const model = dump.definitions[rel]
? { $schema: dump.definitions[rel] }
: null;

if (!model) {
callback(new Error(`Missing '${rel}' definition (${file.url})`));
} else {
callback(null, model.$schema);
}
},
};

const fixedOpts = {
resolve: { fixedRefs },
dereference: {
circular: 'ignore',
},
};

return Promise.resolve()
.then(() => new $RefParser()
.dereference(dump || {}, {
dereference: {
circular: 'ignore',
},
}))
.dereference(dump || {}, fixedOpts))
.then(_bundle => {
const _defns = _bundle.definitions || {};
const _result = squash ? {} : [];
Expand Down
30 changes: 30 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,35 @@ function copy(obj, cb) {
return obj;
}

function flattenRefs(definition, seen = [], depth = 0) {
/* istanbul ignore else */
if (!definition || typeof definition !== 'object') return definition;

/* istanbul ignore else */
if (Array.isArray(definition)) return definition.map(x => flattenRefs(x, seen, depth));

/* istanbul ignore else */
if (typeof definition.id === 'string' && depth > 1) {
const ref = { $ref: definition.id };
const keys = ['belongsTo', 'belongsToMany', 'hasMany', 'hasOne', 'type'];

keys.reduce((memo, key) => {
/* istanbul ignore else */
if (definition[key]) memo[key] = definition[key];
return memo;
}, ref);

/* istanbul ignore else */
if (ref.type === 'object') delete ref.type;
return ref;
}

Object.keys(definition).forEach(key => {
definition[key] = flattenRefs(definition[key], seen, depth + 1);
});
return definition;
}

function reduceRefs(definition) {
/* istanbul ignore else */
if (Array.isArray(definition.allOf)) {
Expand Down Expand Up @@ -672,6 +701,7 @@ module.exports = {
getRefs,
makeRefs,
reduceRefs,
flattenRefs,
makeModel,
sortModels,
getDefinition,
Expand Down

0 comments on commit 7ef5b16

Please sign in to comment.