From 57c3b6ef8d339efd59740f75edfc8807ac238de0 Mon Sep 17 00:00:00 2001 From: Moritz Date: Wed, 15 Jan 2025 11:08:26 +0100 Subject: [PATCH] feat(parser): update example handling to support examples array --- examples/with-openapi-json/openapi.json | 7 ++++++- packages/zudoku/src/lib/oas/graphql/index.ts | 2 +- packages/zudoku/src/lib/oas/parser/upgrade/index.ts | 6 ++---- packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/examples/with-openapi-json/openapi.json b/examples/with-openapi-json/openapi.json index 671c35f4..eea8f170 100644 --- a/examples/with-openapi-json/openapi.json +++ b/examples/with-openapi-json/openapi.json @@ -46,7 +46,12 @@ "description": "Update an existent pet in the store", "content": { "application/json": { - "schema": { "$ref": "#/components/schemas/Pet" } + "schema": { "$ref": "#/components/schemas/Pet" }, + "example": { + "id": "123", + "name": "Dog", + "status": "available" + } }, "application/xml": { "schema": { "$ref": "#/components/schemas/Pet" } diff --git a/packages/zudoku/src/lib/oas/graphql/index.ts b/packages/zudoku/src/lib/oas/graphql/index.ts index 67ab6c4c..a2c35da4 100644 --- a/packages/zudoku/src/lib/oas/graphql/index.ts +++ b/packages/zudoku/src/lib/oas/graphql/index.ts @@ -330,7 +330,7 @@ const OperationItem = builder mediaType, schema: content.schema, examples: Object.entries(content.examples ?? {}).map( - ([name, value]) => ({ name, ...value }), + ([name, value]) => ({ name, value }), ), encoding: Object.entries(content.encoding ?? {}).map( ([name, value]) => ({ name, ...value }), diff --git a/packages/zudoku/src/lib/oas/parser/upgrade/index.ts b/packages/zudoku/src/lib/oas/parser/upgrade/index.ts index a18fac27..25a853b7 100644 --- a/packages/zudoku/src/lib/oas/parser/upgrade/index.ts +++ b/packages/zudoku/src/lib/oas/parser/upgrade/index.ts @@ -41,10 +41,8 @@ export const upgradeSchema = (schema: RecordAny): OpenAPIDocument => { }); schema = traverse(schema, (sub) => { - if (sub.example !== undefined) { - sub.examples = { - default: sub.example, - }; + if (sub.example !== undefined && sub.examples === undefined) { + sub.examples = [sub.example]; delete sub.example; } diff --git a/packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx b/packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx index d910604e..43338142 100644 --- a/packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx +++ b/packages/zudoku/src/lib/plugins/openapi/Sidecar.tsx @@ -142,7 +142,8 @@ export const Sidecar = ({ const code = useMemo(() => { const example = requestBodyContent?.[0]?.schema - ? generateSchemaExample(requestBodyContent[0].schema as SchemaObject) + ? (requestBodyContent.at(0)?.examples?.at(0)?.value ?? + generateSchemaExample(requestBodyContent[0].schema as SchemaObject)) : undefined; const snippet = new HTTPSnippet({