Skip to content

Commit cd0b398

Browse files
authored
One of objects openapi (#37826)
1 parent 708226c commit cd0b398

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/rest/scripts/utils/get-body-params.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,7 @@ export async function getBodyParams(schema, topLevel = false) {
106106
if (param.items.oneOf.every((object) => object.type === 'object')) {
107107
paramType.splice(paramType.indexOf('array'), 1, `array of objects`)
108108
param.oneOfObject = true
109-
110-
for (const oneOfParam of param.items.oneOf) {
111-
const objParam = {
112-
type: 'object',
113-
name: oneOfParam.title,
114-
description: await renderContent(oneOfParam.description),
115-
isRequired: oneOfParam.required,
116-
childParamsGroups: [],
117-
}
118-
objParam.childParamsGroups.push(...(await getBodyParams(oneOfParam, false)))
119-
childParamsGroups.push(objParam)
120-
}
109+
childParamsGroups.push(...(await getOneOfChildParams(param.items)))
121110
}
122111
} else {
123112
const arrayType = param.items.type
@@ -129,7 +118,14 @@ export async function getBodyParams(schema, topLevel = false) {
129118
}
130119
}
131120
} else if (paramType && paramType.includes('object')) {
132-
childParamsGroups.push(...(await getBodyParams(param, false)))
121+
if (param && param.oneOf) {
122+
if (param.oneOf.every((object) => object.type === 'object')) {
123+
param.oneOfObject = true
124+
childParamsGroups.push(...(await getOneOfChildParams(param)))
125+
}
126+
} else {
127+
childParamsGroups.push(...(await getBodyParams(param, false)))
128+
}
133129
} else if (param && param.oneOf) {
134130
// get concatenated description and type
135131
const descriptions = []
@@ -150,6 +146,8 @@ export async function getBodyParams(schema, topLevel = false) {
150146
descriptions.push({ type: childParam.type, description: childParam.description })
151147
}
152148
}
149+
} else {
150+
descriptions.push({ type: param.type, description: param.description })
153151
}
154152
}
155153
// Occasionally, there is no parent description and the description
@@ -205,7 +203,7 @@ async function getTransformedParam(param, paramType, props) {
205203
if (required && required.includes(paramKey)) {
206204
paramDecorated.isRequired = true
207205
}
208-
if (childParamsGroups && childParamsGroups.length > 0) {
206+
if (childParamsGroups && childParamsGroups.length > 0 && !param.oneOfObject) {
209207
// Since the allOf properties can have multiple duplicate properties we want to get rid of the duplicates with the same name, but keep the
210208
// the one that has isRequired set to true.
211209
const mergedChildParamsGroups = Array.from(
@@ -221,6 +219,8 @@ async function getTransformedParam(param, paramType, props) {
221219
)
222220

223221
paramDecorated.childParamsGroups = mergedChildParamsGroups
222+
} else if (childParamsGroups.length > 0) {
223+
paramDecorated.childParamsGroups = childParamsGroups
224224
}
225225
if (param.enum) {
226226
paramDecorated.enum = param.enum
@@ -236,3 +236,19 @@ async function getTransformedParam(param, paramType, props) {
236236
}
237237
return paramDecorated
238238
}
239+
240+
async function getOneOfChildParams(param) {
241+
const childParamsGroups = []
242+
for (const oneOfParam of param.oneOf) {
243+
const objParam = {
244+
type: 'object',
245+
name: oneOfParam.title,
246+
description: await renderContent(oneOfParam.description),
247+
isRequired: oneOfParam.required,
248+
childParamsGroups: [],
249+
}
250+
objParam.childParamsGroups.push(...(await getBodyParams(oneOfParam, false)))
251+
childParamsGroups.push(objParam)
252+
}
253+
return childParamsGroups
254+
}

0 commit comments

Comments
 (0)