From 60d131b0a9dab4710e900323c9ba81160cecf7d8 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Thu, 29 Aug 2024 20:22:07 +0200 Subject: [PATCH] fix: show siblings schema with oneOf (#2576) --- src/services/OpenAPIParser.ts | 12 ++-- .../__snapshots__/OpenAPIParser.test.ts.snap | 65 +++++++++++++++++++ .../__tests__/fixtures/oneOfHoist.json | 17 ++++- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/src/services/OpenAPIParser.ts b/src/services/OpenAPIParser.ts index add65b5c..a0f3f9a9 100644 --- a/src/services/OpenAPIParser.ts +++ b/src/services/OpenAPIParser.ts @@ -364,14 +364,18 @@ export class OpenAPIParser { const allOf = schema.allOf; for (let i = 0; i < allOf.length; i++) { - const sub = allOf[i]; - if (Array.isArray(sub.oneOf)) { + const { oneOf, ...sub } = allOf[i]; + if (!oneOf) { + continue; + } + if (Array.isArray(oneOf)) { const beforeAllOf = allOf.slice(0, i); const afterAllOf = allOf.slice(i + 1); + const siblingValues = Object.keys(sub).length > 0 ? [sub] : []; return { - oneOf: sub.oneOf.map((part: OpenAPISchema) => { + oneOf: oneOf.map((part: OpenAPISchema) => { return { - allOf: [...beforeAllOf, part, ...afterAllOf], + allOf: [...beforeAllOf, ...siblingValues, part, ...afterAllOf], 'x-refsStack': refsStack, }; }), diff --git a/src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap b/src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap index 7fe7cac2..df03b5c4 100644 --- a/src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap +++ b/src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap @@ -21,6 +21,14 @@ exports[`Models Schema should hoist oneOfs when mergin allOf 1`] = ` "oneOf": [ { "allOf": [ + { + "properties": { + "id": { + "description": "The user's ID", + "type": "integer", + }, + }, + }, { "properties": { "username": { @@ -61,6 +69,14 @@ exports[`Models Schema should hoist oneOfs when mergin allOf 1`] = ` }, { "allOf": [ + { + "properties": { + "id": { + "description": "The user's ID", + "type": "integer", + }, + }, + }, { "properties": { "email": { @@ -99,6 +115,55 @@ exports[`Models Schema should hoist oneOfs when mergin allOf 1`] = ` ], "x-refsStack": undefined, }, + { + "allOf": [ + { + "properties": { + "id": { + "description": "The user's ID", + "type": "integer", + }, + }, + }, + { + "properties": { + "id": { + "description": "The user's ID", + "format": "uuid", + "type": "string", + }, + }, + }, + { + "properties": { + "extra": { + "type": "string", + }, + }, + }, + { + "oneOf": [ + { + "properties": { + "password": { + "description": "The user's password", + "type": "string", + }, + }, + }, + { + "properties": { + "mobile": { + "description": "The user's mobile", + "type": "string", + }, + }, + }, + ], + }, + ], + "x-refsStack": undefined, + }, ], } `; diff --git a/src/services/__tests__/fixtures/oneOfHoist.json b/src/services/__tests__/fixtures/oneOfHoist.json index 05568aaf..0bb9be6e 100644 --- a/src/services/__tests__/fixtures/oneOfHoist.json +++ b/src/services/__tests__/fixtures/oneOfHoist.json @@ -9,6 +9,12 @@ "test": { "allOf": [ { + "properties": { + "id": { + "description": "The user's ID", + "type": "integer" + } + }, "oneOf": [ { "properties": { @@ -25,6 +31,15 @@ "type": "string" } } + }, + { + "properties": { + "id": { + "description": "The user's ID", + "type": "string", + "format": "uuid" + } + } } ] }, @@ -59,4 +74,4 @@ } } } -} \ No newline at end of file +}