Skip to content

Commit

Permalink
Fix GH CLI examples that contain arrays of objects (#49180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori authored Feb 28, 2024
1 parent 8e09c3b commit 10e50e7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/rest/components/get-rest-code-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
const { bodyParameters } = codeSample.request
if (bodyParameters) {
if (typeof bodyParameters === 'object') {
const bodyParamValues = Object.values(codeSample.request.bodyParameters)
const bodyParamValues = Object.values(bodyParameters)
// GitHub CLI does not support sending Objects using the -F or
// -f flags. That support may be added in the future. It is possible to
// use gh api --input to take a JSON object from standard input
Expand All @@ -148,23 +148,31 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
if (bodyParamValues.some((elem) => typeof elem === 'object' && !Array.isArray(elem))) {
return undefined
}
requestBodyParams = Object.keys(codeSample.request.bodyParameters)
.map((key) => {
if (typeof codeSample.request.bodyParameters[key] === 'string') {
return `-f ${key}='${codeSample.request.bodyParameters[key]}' `
} else if (Array.isArray(codeSample.request.bodyParameters[key])) {
requestBodyParams = Object.entries(bodyParameters)
.map(([key, params]) => {
if (typeof params === 'string') {
return `-f ${key}='${params}' `
} else if (Array.isArray(params)) {
let cliLine = ''
for (const value of codeSample.request.bodyParameters[key]) {
cliLine += `${typeof value === 'string' ? '-f' : '-F'} "${key}[]=${value}" `
for (const param of params) {
if (typeof param === 'string') {
cliLine += `-f "${key}[]=${param}" `
} else {
// When an array of objects is sent, the CLI takes the key and value as two separate arguments
// E.g. -F "properties[][property_name]=repo" -F "properties[][value]=docs-internal"
for (const [k, v] of Object.entries(param)) {
cliLine += `-F "${key}[][${k}]=${v}" `
}
}
}
return cliLine
} else {
return `-F ${key}=${codeSample.request.bodyParameters[key]} `
return `-F ${key}=${params} `
}
})
.join('\\\n ')
} else {
requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
requestBodyParams = `-f '${bodyParameters}'`
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rest/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface CodeSample {
request: {
contentType: string
acceptHeader: string
bodyParameters: Record<string, string | Array<string>>
bodyParameters: Record<string, string | Array<string | { [key: string]: string }>>
parameters: Record<string, string>
description: string
}
Expand Down

0 comments on commit 10e50e7

Please sign in to comment.