Skip to content

Commit 4f37610

Browse files
authored
OpenAPI form data curl examples (github#19507)
1 parent 2ff0420 commit 4f37610

File tree

11 files changed

+13075
-14554
lines changed

11 files changed

+13075
-14554
lines changed

lib/rest/static/decorated/api.github.com.json

Lines changed: 2167 additions & 2392 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-2.18.json

Lines changed: 1134 additions & 1280 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-2.19.json

Lines changed: 1154 additions & 1300 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-2.20.json

Lines changed: 1320 additions & 1462 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-2.21.json

Lines changed: 1380 additions & 1525 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-2.22.json

Lines changed: 1411 additions & 1585 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.0.json

Lines changed: 1472 additions & 1645 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/ghes-3.1.json

Lines changed: 1501 additions & 1680 deletions
Large diffs are not rendered by default.

lib/rest/static/decorated/github.ae.json

Lines changed: 1511 additions & 1678 deletions
Large diffs are not rendered by default.

script/rest/utils/create-code-samples.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,23 @@ function toShellExample ({ route, serverUrl }) {
4141
? `application/vnd.github.${requiredPreview.name}-preview+json`
4242
: 'application/vnd.github.v3+json'
4343

44+
let requestBodyParams = `-d '${JSON.stringify(params)}'`
45+
// If the content type is application/x-www-form-urlencoded the format of
46+
// the shell example is --data-urlencode param1=value1 --data-urlencode param2=value2
47+
if (route.operation.contentType === 'application/x-www-form-urlencoded') {
48+
requestBodyParams = ''
49+
const paramNames = Object.keys(params)
50+
paramNames.forEach(elem => {
51+
requestBodyParams = `${requestBodyParams} --data-urlencode ${elem}=${params[elem]}`
52+
})
53+
requestBodyParams = requestBodyParams.trim()
54+
}
55+
4456
const args = [
4557
method !== 'GET' && `-X ${method}`,
4658
defaultAcceptHeader ? `-H "Accept: ${defaultAcceptHeader}"` : '',
4759
`${serverUrl}${path}`,
48-
Object.keys(params).length && `-d '${JSON.stringify(params)}'`
60+
Object.keys(params).length && requestBodyParams
4961
].filter(Boolean)
5062
return `curl \\\n ${args.join(' \\\n ')}`
5163
}
@@ -102,9 +114,10 @@ function getExamplePathParams ({ operation }) {
102114
}
103115

104116
function getExampleBodyParams ({ operation }) {
117+
const contentType = Object.keys(get(operation, 'requestBody.content', []))[0]
105118
let schema
106119
try {
107-
schema = operation.requestBody.content['application/json'].schema
120+
schema = operation.requestBody.content[contentType].schema
108121
if (!schema.properties) return {}
109122
} catch (noRequestBody) {
110123
return {}

0 commit comments

Comments
 (0)