Skip to content
Clone
194c44c
40 lines 1.4 KB
#!/usr/bin/env bun
// Flattens `allOf` composition in the OpenAPI document so the Alova
// generator (which cannot resolve composed schemas) can process it.
// Usage: bun scripts/flatten-openapi.mjs <input> <output>
import { readFileSync, writeFileSync } from 'node:fs'
function mergeInto(target, source) {
if (!source || typeof source !== 'object') return
for (const [key, value] of Object.entries(source)) {
if (key === 'allOf') continue
if (key === 'required' && Array.isArray(value)) {
target.required = [...new Set([...(target.required ?? []), ...value])]
continue
}
if (key === 'properties' && value && typeof value === 'object') {
target.properties = { ...(target.properties ?? {}), ...value }
continue
}
if (target[key] === undefined) target[key] = value
}
}
function flatten(node) {
if (Array.isArray(node)) return node.map(flatten)
if (!node || typeof node !== 'object') return node
const out = {}
for (const [key, value] of Object.entries(node)) {
if (key === 'allOf' && Array.isArray(value)) {
for (const part of value) mergeInto(out, flatten(part))
continue
}
out[key] = flatten(value)
}
return out
}
const [input, output] = process.argv.slice(2)
const doc = JSON.parse(readFileSync(input, 'utf8'))
writeFileSync(output, JSON.stringify(flatten(doc), null, 2))
console.log(`flattened ${input} -> ${output}`)