Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show siblings schema with oneOf #2576

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}),
Expand Down
65 changes: 65 additions & 0 deletions src/services/__tests__/__snapshots__/OpenAPIParser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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,
},
],
}
`;
Expand Down
17 changes: 16 additions & 1 deletion src/services/__tests__/fixtures/oneOfHoist.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
"test": {
"allOf": [
{
"properties": {
"id": {
"description": "The user's ID",
"type": "integer"
}
},
"oneOf": [
{
"properties": {
Expand All @@ -25,6 +31,15 @@
"type": "string"
}
}
},
{
"properties": {
"id": {
"description": "The user's ID",
"type": "string",
"format": "uuid"
}
}
}
]
},
Expand Down Expand Up @@ -59,4 +74,4 @@
}
}
}
}
}
Loading