Skip to content

Commit 0fcf31c

Browse files
authored
Support OpenAPI 3.1 (github#25710)
1 parent a9b0936 commit 0fcf31c

File tree

9 files changed

+673
-663
lines changed

9 files changed

+673
-663
lines changed

lib/rest/static/decorated/api.github.com.json

Lines changed: 104 additions & 104 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.0.json

Lines changed: 84 additions & 84 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.1.json

Lines changed: 86 additions & 86 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.2.json

Lines changed: 92 additions & 92 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.3.json

Lines changed: 92 additions & 92 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.4.json

Lines changed: 98 additions & 98 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/github.ae.json

Lines changed: 90 additions & 90 deletions
Large diffs are not rendered by default.

script/rest/utils/create-code-samples.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,15 @@ function getExampleParamValue(name, schema) {
171171
if (schema.anyOf && schema.anyOf[0].type) return getExampleParamValue(name, schema.anyOf[0])
172172
if (!schema.type) return 'any'
173173

174-
switch (schema.type) {
175-
case 'string':
176-
return name
177-
case 'boolean':
178-
return true
179-
case 'integer':
180-
return 42
181-
case 'object':
182-
return mapValues(schema.properties, (propSchema, propName) =>
183-
getExampleParamValue(propName, propSchema)
184-
)
185-
case 'array':
186-
return [getExampleParamValue(name, schema.items)]
174+
if (schema.type.includes('string')) return name
175+
else if (schema.type.includes('boolean')) return true
176+
else if (schema.type.includes('integer')) return 42
177+
else if (schema.type.includes('object')) {
178+
return mapValues(schema.properties, (propSchema, propName) =>
179+
getExampleParamValue(propName, propSchema)
180+
)
181+
} else if (schema.type.includes('array')) {
182+
return [getExampleParamValue(name, schema.items)]
187183
}
188184

189185
throw new Error(`Unknown data type in schema:, ${JSON.stringify(schema, null, 2)}`)

script/rest/utils/operation.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ async function getBodyParams(paramsObject, requiredParams) {
293293
param.name = paramKey
294294
param.in = 'body'
295295
param.rawType = param.type
296+
// OpenAPI 3.0 only had a single value for `type`. OpenAPI 3.1
297+
// will either be a single value or an array of values.
298+
// This makes type an array regardless of how many values the array
299+
// includes. This allows us to support 3.1 while remaining backwards
300+
// compatible with 3.0.
301+
if (!Array.isArray(param.type)) param.type = [param.type]
296302
param.rawDescription = param.description
297303

298304
// Stores the types listed under the `Type` column in the `Parameters`
@@ -328,16 +334,24 @@ async function getBodyParams(paramsObject, requiredParams) {
328334
}
329335

330336
// Arrays require modifying the displayed type (e.g., array of strings)
331-
if (param.type === 'array') {
337+
if (param.type.includes('array')) {
332338
if (param.items.type) paramArray.push(`array of ${param.items.type}s`)
333339
if (param.items.oneOf) {
334340
paramArray.push(param.items.oneOf.map((elem) => `array of ${elem.type}s`))
335341
}
342+
// push the remaining types in the param.type array
343+
// that aren't type array
344+
const remainingItems = param.type
345+
const indexOfArrayType = remainingItems.indexOf('array')
346+
remainingItems.splice(indexOfArrayType, 1)
347+
paramArray.push(...remainingItems)
336348
} else if (param.type) {
337-
paramArray.push(param.type)
349+
paramArray.push(...param.type)
338350
}
339-
340-
if (param.nullable) paramArray.push('nullable')
351+
// Supports backwards compatibility for OpenAPI 3.0
352+
// In 3.1 a nullable type is part of the param.type array and
353+
// the property param.nullable does not exist.
354+
if (param.nullable) paramArray.push('null')
341355

342356
param.type = paramArray.flat().join(' or ')
343357
param.description = param.description || ''

0 commit comments

Comments
 (0)