@rebilly/swagger-combine
Advanced tools
Comparing version 1.0.3 to 1.1.0
68
index.js
@@ -9,2 +9,14 @@ #!/usr/bin/env node | ||
const OPENAPI3_COMPONENTS = [ | ||
'schemas', | ||
'responses', | ||
'parameters', | ||
'examples', | ||
'headers', | ||
'requestBodies', | ||
'links', | ||
'callbacks', | ||
'securitySchemes' | ||
]; | ||
async function loadSpecs(specs) { | ||
@@ -25,3 +37,3 @@ try { | ||
function mergeStrictEqual(dest, src, type, srcName) { | ||
for (let name of Object.keys(src)) { | ||
for (let name of Object.keys(src || {})) { | ||
if (dest[name]) { | ||
@@ -53,2 +65,3 @@ try { | ||
const res = specs[0]; | ||
const oasVersion = getSpecVersion(specs[0]); | ||
@@ -58,12 +71,41 @@ for (let i = 1; i < specs.length; i++) { | ||
const specUrl = urls[i]; | ||
if (getSpecVersion(specs[i]) !== oasVersion) { | ||
console.error( | ||
'Specs has different versions: ' + oasVersion + ' and ' + getSpecVersion(specs[i]) | ||
); | ||
} | ||
mergeStrictEqual(res.paths, spec.paths, 'Path', specUrl); | ||
mergeStrictEqual(res.definitions, spec.definitions, 'Definition', specUrl); | ||
mergeStrictEqual(res.responses, spec.responses, 'Response', specUrl); | ||
mergeStrictEqual(res.parameters, spec.parameters, 'Parameter', specUrl); | ||
mergeStrictEqual( | ||
res.securityDefinitions, | ||
spec.securityDefinitions, | ||
'Security Definition', | ||
specUrl | ||
); | ||
if (oasVersion.startsWith('3.')) { | ||
if (!spec.components) { | ||
continue; | ||
} | ||
if (!res.components) { | ||
res.components = {}; | ||
} | ||
for (let componentName of OPENAPI3_COMPONENTS) { | ||
if (!spec.components[componentName]) continue; | ||
if (!res.components[componentName]) { | ||
res.components[componentName] = {}; | ||
} | ||
mergeStrictEqual( | ||
res.components[componentName], | ||
spec.components[componentName], | ||
componentName, | ||
specUrl | ||
); | ||
} | ||
} else { | ||
mergeStrictEqual(res.definitions, spec.definitions, 'Definition', specUrl); | ||
mergeStrictEqual(res.responses, spec.responses, 'Response', specUrl); | ||
mergeStrictEqual(res.parameters, spec.parameters, 'Parameter', specUrl); | ||
mergeStrictEqual( | ||
res.securityDefinitions, | ||
spec.securityDefinitions, | ||
'Security Definition', | ||
specUrl | ||
); | ||
} | ||
} | ||
@@ -74,2 +116,6 @@ | ||
function getSpecVersion(spec) { | ||
return (spec.openapi || spec.swagger || '').toString() || undefined; | ||
} | ||
async function run(argv) { | ||
@@ -109,2 +155,2 @@ const urls = [argv.baseSpec, ...(argv.specs || [])]; | ||
module.combineSpecs = combineSpecs; | ||
module.combineSpecs = combineSpecs; |
{ | ||
"name": "@rebilly/swagger-combine", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "Opinionated tool to shallow-merge two or more swagger 2.0 specs into one", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
20508
279
5