From 0bb24cb7321782859ddf8200810cbc1a2af561a5 Mon Sep 17 00:00:00 2001 From: Grace Park Date: Thu, 5 Oct 2023 10:21:22 -0700 Subject: [PATCH] Allow for arrays in code samples for REST GHExample (#43820) --- src/rest/components/get-rest-code-samples.ts | 12 +++++++++--- src/rest/components/types.ts | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/rest/components/get-rest-code-samples.ts b/src/rest/components/get-rest-code-samples.ts index f722e6b91333..f72698faa715 100644 --- a/src/rest/components/get-rest-code-samples.ts +++ b/src/rest/components/get-rest-code-samples.ts @@ -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]} ` } diff --git a/src/rest/components/types.ts b/src/rest/components/types.ts index 01bd2a96d7a5..ca81040b008d 100644 --- a/src/rest/components/types.ts +++ b/src/rest/components/types.ts @@ -44,7 +44,7 @@ export interface CodeSample { request: { contentType: string acceptHeader: string - bodyParameters: Record + bodyParameters: Record> parameters: Record description: string }