type Schema = { description?: string; allOf?: Array>; [key: string]: unknown } type Parameter = { description?: string; schema?: Schema; [key: string]: unknown } type Document = { paths?: Record }>> } // Effect writes a parameter's description into its schema: `.annotate` sets `schema.description`, and `.annotateKey` // (used where annotating would rename a shared component) adds a `{ description }` member to `schema.allOf`. OpenAPI // documents parameters by `parameters[].description`, so move it there. export function liftParameterDescriptions(document: T): T { for (const operations of Object.values((document as Document).paths ?? {})) for (const operation of Object.values(operations)) operation.parameters = operation.parameters?.map((parameter) => { if (parameter.schema === undefined || parameter.description !== undefined) return parameter const description = parameter.schema.allOf?.find(isDescription)?.description ?? parameter.schema.description if (description === undefined) return parameter return { ...parameter, description, schema: withoutDescription(parameter.schema) } }) return document } function isDescription(member: Record): member is { description: string } { return Object.keys(member).length === 1 && typeof member.description === "string" } function withoutDescription(schema: Schema): Schema { const rest = Object.fromEntries(Object.entries(schema).filter(([key]) => key !== "description" && key !== "allOf")) const allOf = schema.allOf?.filter((member) => !isDescription(member)) ?? [] if (allOf.length === 0) return rest if (allOf.length === 1 && Object.keys(rest).length === 0) return allOf[0] return { ...rest, allOf } }