Show types for oneOf path and query parameters in REST docs (#64187) · github/docs@7b1cf85 · GitHub
Skip to content

Commit 7b1cf85

Browse files
heiskrCopilot
andauthored
Show types for oneOf path and query parameters in REST docs (#64187)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bf5940de-658e-4a1a-bffa-3018fb78b138
1 parent 0fe63e0 commit 7b1cf85

4 files changed

Lines changed: 106 additions & 6 deletions

File tree

‎src/article-api/liquid-renderers/rest-tags.ts‎

Lines changed: 3 additions & 1 deletion

‎src/rest/scripts/utils/operation.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import getCodeSamples from './create-rest-examples'
99
import operationSchema from './operation-schema'
1010
import { validateJson } from '@/tests/lib/validate-json-schema'
1111
import { getBodyParams, type TransformedParam } from './get-body-params'
12+
import { simplifyParameterSchema } from './parameter-schema'
1213
import type { OpenApiOperation, OpenApiParameter, OpenApiServer } from './openapi-types'
1314
import type { MergedExample } from './create-rest-examples'
1415

@@ -155,12 +156,8 @@ export default class Operation {
155156
delete param.example
156157
delete param.examples
157158
delete param['x-multi-segment']
158-
// Keep only parameter schema subfields that renderers consume: type, default, and enum.
159159
if (param.schema && typeof param.schema === 'object') {
160-
const { type, default: defaultVal, enum: enumVal } = param.schema
161-
param.schema = { type }
162-
if (defaultVal !== undefined) param.schema.default = defaultVal
163-
if (enumVal !== undefined) param.schema.enum = enumVal
160+
param.schema = simplifyParameterSchema(param.schema)
164161
}
165162
return param
166163
}),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { OpenApiParameterSchema } from './openapi-types'
2+
3+
interface SchemaVariant {
4+
type?: string | string[]
5+
enum?: unknown[]
6+
items?: { type?: unknown; enum?: unknown[] }
7+
}
8+
9+
// Keep only the schema fields that parameter renderers read: type, default, and enum.
10+
// Multi-type parameters use oneOf or anyOf with no top-level type, so their type
11+
// and allowed values come from the variants instead.
12+
export function simplifyParameterSchema(schema: OpenApiParameterSchema): OpenApiParameterSchema {
13+
const { type, default: defaultVal } = schema
14+
let enumVal = schema.enum
15+
const simplified: OpenApiParameterSchema = { type }
16+
17+
const variants = (schema.oneOf ?? schema.anyOf) as SchemaVariant[] | undefined
18+
if (type === undefined && Array.isArray(variants)) {
19+
const types = new Set<string>()
20+
const enums = new Set<unknown>()
21+
let everyVariantRestricted = variants.length > 0
22+
for (const variant of variants) {
23+
const variantTypes = Array.isArray(variant.type) ? variant.type : [variant.type]
24+
for (const variantType of variantTypes) {
25+
if (variantType === undefined) continue
26+
// Match the "array of <type>s" wording that body parameters use.
27+
types.add(
28+
variantType === 'array' && typeof variant.items?.type === 'string'
29+
? `array of ${variant.items.type}s`
30+
: variantType,
31+
)
32+
}
33+
const variantEnum = variant.enum ?? variant.items?.enum
34+
if (!variantEnum?.length) everyVariantRestricted = false
35+
for (const value of variantEnum ?? []) enums.add(value)
36+
}
37+
if (types.size > 0) simplified.type = types.size === 1 ? [...types][0] : [...types]
38+
// A variant without an enum accepts any value, so a combined list would wrongly narrow the parameter.
39+
if (enumVal === undefined && everyVariantRestricted) enumVal = [...enums]
40+
}
41+
42+
if (defaultVal !== undefined) simplified.default = defaultVal
43+
if (enumVal !== undefined) simplified.enum = enumVal
44+
return simplified
45+
}

‎src/rest/tests/parameter-schema.ts‎

Lines changed: 56 additions & 0 deletions

0 commit comments

Comments
 (0)