From 07f6df4de5a79aaeec2274b2a98609ccc9e4807c Mon Sep 17 00:00:00 2001 From: enzonotario Date: Thu, 9 Jan 2025 19:48:47 -0300 Subject: [PATCH 1/5] feat: schema examples --- docs/public/openapi.json | 38 ++ src/components/Path/OAPath.vue | 8 +- .../Playground/OAPlaygroundParameters.vue | 4 +- src/components/Request/OARequestBody.vue | 2 +- src/components/Response/OAResponse.vue | 4 +- src/components/Schema/OASchemaTabs.vue | 101 ++-- src/components/Try/OATryWithVariables.vue | 12 +- src/composables/useTheme.ts | 10 +- src/lib/examples/getSchemaExample.ts | 34 ++ src/lib/{ => examples}/getSchemaUiJson.ts | 11 +- src/lib/{ => examples}/getSchemaUiXml.ts | 2 +- src/lib/getSchemaUiContentType.ts | 11 - src/lib/processOpenAPI.ts | 12 +- src/types.ts | 5 +- .../__snapshots__/processOpenAPI.test.ts.snap | 479 ++++++++++++------ test/lib/getSchemaUi.test.ts | 6 +- 16 files changed, 481 insertions(+), 258 deletions(-) create mode 100644 src/lib/examples/getSchemaExample.ts rename src/lib/{ => examples}/getSchemaUiJson.ts (92%) rename src/lib/{ => examples}/getSchemaUiXml.ts (85%) delete mode 100644 src/lib/getSchemaUiContentType.ts diff --git a/docs/public/openapi.json b/docs/public/openapi.json index 2ef48242..01a74f32 100644 --- a/docs/public/openapi.json +++ b/docs/public/openapi.json @@ -103,6 +103,24 @@ "application/json": { "schema": { "$ref": "#/components/schemas/Artist" + }, + "examples": { + "Charly": { + "value": { + "name": "Charly García", + "description": "One of the most influential rock musicians in Argentine history.", + "image": "https://cdn.rock-legends.com/photos/charly.jpg", + "band": "Sui Generis" + } + }, + "Maria": { + "value": { + "name": "Maria Gabriela Epumer", + "description": "A very important rock musician in Argentine history.", + "image": "https://cdn.rock-legends.com/photos/maria.jpg", + "band": "Viudas e Hijas de Roque Enroll" + } + } } } } @@ -114,6 +132,26 @@ "application/json": { "schema": { "$ref": "#/components/schemas/Artist" + }, + "examples": { + "Charly": { + "value": { + "id": 1, + "name": "Charly García", + "description": "One of the most influential rock musicians in Argentine history.", + "image": "https://cdn.rock-legends.com/photos/charly.jpg", + "band": "Sui Generis" + } + }, + "Maria": { + "value": { + "id": 2, + "name": "Maria Gabriela Epumer", + "description": "A very important rock musician in Argentine history.", + "image": "https://cdn.rock-legends.com/photos/maria.jpg", + "band": "Viudas e Hijas de Roque Enroll" + } + } } } } diff --git a/src/components/Path/OAPath.vue b/src/components/Path/OAPath.vue index 175069a2..a797dba0 100644 --- a/src/components/Path/OAPath.vue +++ b/src/components/Path/OAPath.vue @@ -23,6 +23,10 @@ const openapi = props.openapi ?? getOpenApiInstance() const operation = openapi.getOperation(props.id) +const operationData = initOperationData(operation) + +provide('operationData', operationData) + const operationPath = openapi.getOperationPath(props.id) const operationMethod = openapi.getOperationMethod(props.id)?.toUpperCase() @@ -85,7 +89,7 @@ const request = ref( baseUrl: baseUrl.value, parameters: operationParameters ?? [], authorizations: securityUi.length ? Object.entries(securityUi)[0]?.schemes : {}, - body: operationRequestBody?.content?.[bodyRequestContentType]?.uiContentType, + body: (operationRequestBody?.content?.[bodyRequestContentType]?.examples ?? { value: '' })[0]?.value, variables: {}, }) : {}, @@ -98,8 +102,6 @@ function updateRequest(newRequest) { function updateSelectedServer(server) { selectedServer.value = server } - -provide('operationData', initOperationData(operation))