Skip to content

Commit

Permalink
Allow for arrays in code samples for REST GHExample (#43820)
Browse files Browse the repository at this point in the history
  • Loading branch information
gracepark authored Oct 5, 2023
1 parent 1148818 commit 0bb24cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/rest/components/get-rest-code-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,26 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
// and the type is a string.
const { bodyParameters } = codeSample.request
if (bodyParameters) {
if (typeof bodyParameters === 'object' && !Array.isArray(bodyParameters)) {
if (typeof bodyParameters === 'object') {
const bodyParamValues = Object.values(codeSample.request.bodyParameters)
// GitHub CLI does not support sending Objects and arrays using the -F or
// 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
// constructed by jq and piped to gh api. However, we'll hold off on adding
// that complexity for now.
if (bodyParamValues.some((elem) => typeof elem === 'object')) {
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])) {
let cliLine = ''
for (const value of codeSample.request.bodyParameters[key]) {
cliLine += `-f "${key}[]=${value}" `
}
return cliLine
} else {
return `-F ${key}=${codeSample.request.bodyParameters[key]} `
}
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 @@ -44,7 +44,7 @@ export interface CodeSample {
request: {
contentType: string
acceptHeader: string
bodyParameters: Record<string, string>
bodyParameters: Record<string, string | Array<string>>
parameters: Record<string, string>
description: string
}
Expand Down

0 comments on commit 0bb24cb

Please sign in to comment.