MCPcopy Index your code
hub / github.com/github/docs / getBodyParams

Function getBodyParams

src/rest/scripts/utils/get-body-params.js:42–171  ·  view source on GitHub ↗
(schema, topLevel = false)

Source from the content-addressed store, hash-verified

40
41// Gets the body parameters for a given schema recursively.
42export async function getBodyParams(schema, topLevel = false) {
43 const bodyParametersParsed = []
44 const schemaObject = schema.oneOf && topLevel ? await getTopLevelOneOfProperty(schema) : schema
45 const properties = schemaObject.properties || {}
46 const required = schemaObject.required || []
47
48 // Most operation requestBody schemas are objects. When the type is an array,
49 // there will not be properties on the `schema` object.
50 if (topLevel && schema.type === 'array') {
51 const childParamsGroups = []
52 const arrayType = schema.items.type
53 const paramType = [schema.type]
54 if (arrayType === 'object') {
55 childParamsGroups.push(...(await getBodyParams(schema.items, false)))
56 } else {
57 paramType.splice(paramType.indexOf('array'), 1, `array of ${arrayType}s`)
58 }
59 const paramDecorated = await getTransformedParam(schema, paramType, {
60 required,
61 topLevel,
62 childParamsGroups,
63 })
64 return [paramDecorated]
65 }
66
67 for (const [paramKey, param] of Object.entries(properties)) {
68 // OpenAPI 3.0 only had a single value for `type`. OpenAPI 3.1
69 // will either be a single value or an array of values.
70 // This makes type an array regardless of how many values the array
71 // includes. This allows us to support 3.1 while remaining backwards
72 // compatible with 3.0.
73 const paramType = Array.isArray(param.type) ? param.type : [param.type]
74 const additionalPropertiesType = param.additionalProperties
75 ? Array.isArray(param.additionalProperties.type)
76 ? param.additionalProperties.type
77 : [param.additionalProperties.type]
78 : []
79 const childParamsGroups = []
80
81 // If the parameter is an array or object there may be child params
82 // If the parameter has oneOf or additionalProperties, they need to be
83 // recursively read too.
84
85 // There are a couple operations with additionalProperties, which allows
86 // the api to define input parameters with the type dictionary. These are the only
87 // two operations (at the time of adding this code) that use additionalProperties
88 // Create a snapshot of dependencies for a repository
89 // Update a gist
90 if (param.additionalProperties && additionalPropertiesType.includes('object')) {
91 const keyParam = {
92 type: 'object',
93 name: 'key',
94 description: await renderContent(
95 `A user-defined key to represent an item in \`${paramKey}\`.`
96 ),
97 isRequired: param.required,
98 enum: param.enum,
99 default: param.default,

Calls 3

getTopLevelOneOfPropertyFunction · 0.85
getTransformedParamFunction · 0.85
renderContentFunction · 0.85

Tested by

no test coverage detected