Skip to content

Commit

Permalink
OpenAPI: Fix switcher default value being ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 4, 2025
1 parent 2140aea commit 698b385
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-bats-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-openapi': patch
---

Fix switcher default value being ignored
30 changes: 30 additions & 0 deletions examples/openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 17 additions & 7 deletions packages/openapi/src/ui/playground/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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 };
}

0 comments on commit 698b385

Please sign in to comment.