OpenAPI: Fix switcher default value being ignored

This commit is contained in:
Fuma Nama
2025-01-04 10:32:54 +08:00
parent 2140aea90f
commit 698b385780
3 changed files with 52 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---
Fix switcher default value being ignored
+30
View File
@@ -1235,6 +1235,36 @@
"trialEnds": "2023-06-16T17:16:37.161Z"
}
},
"parameters": {
"type": "object",
"required": ["media_input"],
"properties": {
"media_input": {
"oneOf": [
{
"title": "Image Media",
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "image"
},
"url": {
"type": "string"
}
},
"required": ["type", "url"],
"examples": [
{
"type": "image",
"url": "https://example.com/image.png"
}
]
}
]
}
}
},
"roles": {
"type": "array",
"items": {
+17 -7
View File
@@ -171,12 +171,20 @@ function convertValue(
}
if (schema.type === 'switcher') {
const schema = resolve(
getDynamicFieldSchema(fieldName, dynamicFields),
return convertValue(
fieldName,
value,
resolve(
getDynamicFieldSchema(
fieldName,
dynamicFields,
Object.values(schema.items).at(0),
),
references,
),
references,
dynamicFields,
);
return convertValue(fieldName, value, schema, references, dynamicFields);
}
if (typeof value === 'object' && schema.type === 'object') {
@@ -231,10 +239,12 @@ function convertValue(
function getDynamicFieldSchema(
name: string,
dynamicFields: Map<string, DynamicField>,
defaultValue?: RequestSchema | ReferenceSchema,
): RequestSchema | ReferenceSchema {
const field = dynamicFields.get(name);
return field?.type === 'field'
? field.schema
: { type: 'null', isRequired: false };
if (field?.type === 'field') return field.schema;
if (defaultValue) return defaultValue;
return { type: 'null', isRequired: false };
}