| 10 | // operations have a top-level oneOf. |
| 11 | |
| 12 | async function getTopLevelOneOfProperty(schema) { |
| 13 | if (!schema.oneOf) { |
| 14 | throw new Error('Schema does not have a requestBody oneOf property defined') |
| 15 | } |
| 16 | if (!(Array.isArray(schema.oneOf) && schema.oneOf.length > 0)) { |
| 17 | throw new Error('Schema requestBody oneOf property is not an array') |
| 18 | } |
| 19 | // When a oneOf exists but the `type` differs, the case has historically |
| 20 | // been that the alternate option is an array, where the first option |
| 21 | // is the array as a property of the object. We need to ensure that the |
| 22 | // first option listed is the most comprehensive and preferred option. |
| 23 | const firstOneOfObject = schema.oneOf[0] |
| 24 | const allOneOfAreObjects = schema.oneOf.every((elem) => elem.type === 'object') |
| 25 | let required = firstOneOfObject.required || [] |
| 26 | let properties = firstOneOfObject.properties || {} |
| 27 | |
| 28 | // When all of the oneOf objects have the `type: object` we |
| 29 | // need to display all of the parameters. |
| 30 | // This merges all of the properties and required values. |
| 31 | if (allOneOfAreObjects) { |
| 32 | for (const each of schema.oneOf.slice(1)) { |
| 33 | Object.assign(firstOneOfObject.properties, each.properties) |
| 34 | required = firstOneOfObject.required.concat(each.required) |
| 35 | } |
| 36 | properties = firstOneOfObject.properties |
| 37 | } |
| 38 | return { properties, required } |
| 39 | } |
| 40 | |
| 41 | // Gets the body parameters for a given schema recursively. |
| 42 | export async function getBodyParams(schema, topLevel = false) { |