fix(openapi): flatten nullable unions to avoid nested anyOf/oneOf

This commit is contained in:
Hampus Kraft
2026-02-17 14:06:58 +00:00
parent 2db02ec255
commit b227bd0a85
8 changed files with 131 additions and 102 deletions

View File

@@ -1017,6 +1017,19 @@ function isOpenAPISchema(schema: OpenAPISchemaOrRef): schema is OpenAPISchema {
}
function makeNullableSchema(inner: OpenAPISchemaOrRef): OpenAPISchema {
if (isOpenAPISchema(inner)) {
const keys = Object.keys(inner).filter((k) => k !== 'description');
if (inner.oneOf && keys.length === 1) {
const result: OpenAPISchema = {oneOf: [...inner.oneOf, {type: 'null'}]};
if (inner.description) result.description = inner.description;
return result;
}
if (inner.anyOf && keys.length === 1) {
const result: OpenAPISchema = {anyOf: [...inner.anyOf, {type: 'null'}]};
if (inner.description) result.description = inner.description;
return result;
}
}
return {
anyOf: [inner, {type: 'null'}],
};