Skip to content

Commit e78eb19

Browse files
rachmarirsese
andauthored
account for non-object example values in rest docs (#33411)
Co-authored-by: Robert Sese <734194+rsese@users.noreply.github.com>
1 parent 24abb19 commit e78eb19

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

components/lib/get-rest-code-samples.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useMainContext } from 'components/context/MainContext'
88
type CodeExamples = Record<string, any>
99
/*
1010
Generates a curl example
11-
1211
For example:
1312
curl \
1413
-X POST \
@@ -24,6 +23,11 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
2423
? codeSample.response.contentType
2524
: 'application/vnd.github+json'
2625

26+
const contentTypeHeader =
27+
codeSample?.request?.contentType === 'application/octet-stream'
28+
? '-H "Content-Type: application/octet-stream"'
29+
: ''
30+
2731
let requestPath = codeSample?.request?.parameters
2832
? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
2933
: operation.requestPath
@@ -46,14 +50,22 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
4650
const CURL_CONTENT_TYPE_MAPPING: { [key: string]: string } = {
4751
'application/x-www-form-urlencoded': '--data-urlencode',
4852
'multipart/form-data': '--form',
53+
'application/octet-stream': '--data-binary',
4954
}
5055
const contentType = codeSample.request.contentType
5156
if (codeSample.request.contentType in CURL_CONTENT_TYPE_MAPPING) {
5257
requestBodyParams = ''
53-
const paramNames = Object.keys(codeSample.request.bodyParameters)
54-
paramNames.forEach((elem) => {
55-
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
56-
})
58+
// Most of the time the example body parameters have a name and value
59+
// and are included in an object. But, some cases are a single value
60+
// and the type is a string.
61+
if (typeof codeSample.request.bodyParameters === 'object') {
62+
const paramNames = Object.keys(codeSample.request.bodyParameters)
63+
paramNames.forEach((elem) => {
64+
requestBodyParams = `${requestBodyParams} ${CURL_CONTENT_TYPE_MAPPING[contentType]} "${elem}=${codeSample.request.bodyParameters[elem]}"`
65+
})
66+
} else {
67+
requestBodyParams = `${CURL_CONTENT_TYPE_MAPPING[contentType]} "${codeSample.request.bodyParameters}"`
68+
}
5769
}
5870
}
5971

@@ -71,6 +83,7 @@ export function getShellExample(operation: Operation, codeSample: CodeSample) {
7183
const args = [
7284
operation.verb !== 'get' && `-X ${operation.verb.toUpperCase()}`,
7385
`-H "Accept: ${defaultAcceptHeader}" \\\n ${authHeader}${apiVersionHeader}`,
86+
contentTypeHeader,
7487
`${operation.serverUrl}${requestPath}`,
7588
requestBodyParams,
7689
].filter(Boolean)
@@ -101,7 +114,10 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
101114
requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''
102115

103116
let requestBodyParams = ''
104-
if (codeSample?.request?.bodyParameters) {
117+
// Most of the time the example body parameters have a name and value
118+
// and are included in an object. But, some cases are a single value
119+
// and the type is a string.
120+
if (typeof codeSample?.request?.bodyParameters === 'object') {
105121
const bodyParamValues = Object.values(codeSample.request.bodyParameters)
106122
// GitHub CLI does not support sending Objects and arrays using the -F or
107123
// -f flags. That support may be added in the future. It is possible to
@@ -120,6 +136,8 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
120136
}
121137
})
122138
.join('\\\n ')
139+
} else {
140+
requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
123141
}
124142
const args = [
125143
operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`,
@@ -147,9 +165,15 @@ export function getGHExample(operation: Operation, codeSample: CodeSample) {
147165
148166
*/
149167
export function getJSExample(operation: Operation, codeSample: CodeSample) {
150-
const parameters = codeSample.request
151-
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
152-
: {}
168+
const parameters =
169+
// Most of the time the example body parameters have a name and value
170+
// and are included in an object. But, some cases are a single value
171+
// and the type is a string.
172+
typeof codeSample.request.bodyParameters === 'object'
173+
? codeSample.request
174+
? { ...codeSample.request.parameters, ...codeSample.request.bodyParameters }
175+
: {}
176+
: { ...codeSample.request.parameters, data: codeSample.request.bodyParameters }
153177

154178
let queryParameters = ''
155179

0 commit comments

Comments
 (0)