From 34039e2d2714c30f0ffc6a97ca71002d8f45e730 Mon Sep 17 00:00:00 2001 From: thenaomiday Date: Fri, 11 Jun 2021 17:44:14 -0400 Subject: [PATCH] fix(code snippets): align curl params in code snippets correctly (#1320) * fix: align curl code snippets correctly * test: updating a test snapshot Co-authored-by: Jon Ursenbach --- .../__snapshots__/index.test.jsx.snap | 10 +++---- .../__snapshots__/index.test.js.snap | 24 ++++++++++----- .../oas-to-snippet/__tests__/index.test.js | 30 +++++++++++++++++++ .../oas-to-snippet/src/supportedLanguages.js | 1 + 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/packages/api-explorer/__tests__/__snapshots__/index.test.jsx.snap b/packages/api-explorer/__tests__/__snapshots__/index.test.jsx.snap index 22e837adc..7df5da842 100644 --- a/packages/api-explorer/__tests__/__snapshots__/index.test.jsx.snap +++ b/packages/api-explorer/__tests__/__snapshots__/index.test.jsx.snap @@ -2,9 +2,9 @@ exports[`DocAsync should do OAS dereferencing 1`] = ` "

oneOf request with a nested allOf

 
patchhttps://httpbin.org/pets
curl --request PATCH \\\\ - --url https://httpbin.org/pets \\\\ - --header 'Content-Type: application/json' \\\\ - --data '{\\"pet_type\\":\\"Cat\\"}'

Body Params

string
object
boolean
int32

Response

Updated

allOf with a readOnly prop

 
puthttps://httpbin.org/pets
curl --request PUT \\\\ - --url https://httpbin.org/pets \\\\ - --header 'Content-Type: application/json'

Body Params

object
_self
string
string
string
object
source
string
string
string
int32

Response

successful operation

" + --url https://httpbin.org/pets \\\\ + --header 'Content-Type: application/json' \\\\ + --data '{\\"pet_type\\":\\"Cat\\"}'

Body Params

string
object
boolean
int32

Response

Updated

allOf with a readOnly prop

 
puthttps://httpbin.org/pets
curl --request PUT \\\\ + --url https://httpbin.org/pets \\\\ + --header 'Content-Type: application/json'

Body Params

object
_self
string
string
string
object
source
string
string
string
int32

Response

successful operation

" `; diff --git a/packages/oas-to-snippet/__tests__/__snapshots__/index.test.js.snap b/packages/oas-to-snippet/__tests__/__snapshots__/index.test.js.snap index 5c2323cf4..f6860d157 100644 --- a/packages/oas-to-snippet/__tests__/__snapshots__/index.test.js.snap +++ b/packages/oas-to-snippet/__tests__/__snapshots__/index.test.js.snap @@ -3,11 +3,11 @@ exports[`multipart/form-data handlings should convert a multipart/form-data operation into a proper snippet that uses the original file 1`] = ` Object { "code": "curl --request POST \\\\ - --url https://example.com/multipart \\\\ - --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \\\\ - --form orderId=10 \\\\ - --form userId=3232 \\\\ - --form documentFile=@owlbert.png", + --url https://example.com/multipart \\\\ + --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \\\\ + --form orderId=10 \\\\ + --form userId=3232 \\\\ + --form documentFile=@owlbert.png", "highlightMode": "shell", } `; @@ -39,6 +39,14 @@ fetch(url, options) } `; +exports[`should have special indents for curl snippets 1`] = ` +"curl --request GET \\\\ + --url https://example.com/body \\\\ + --header 'Content-Type: application/x-www-form-urlencoded' \\\\ + --data a=test \\\\ + --data 'b=1,2,3'" +`; + exports[`supported languages c should generate code for the default target 1`] = ` Object { "code": "CURL *hnd = curl_easy_init(); @@ -159,7 +167,7 @@ IRestResponse response = client.Execute(request);", exports[`supported languages curl should generate code for the default target 1`] = ` Object { "code": "curl --request GET \\\\ - --url https://example.com/path/123", + --url https://example.com/path/123", "highlightMode": "shell", } `; @@ -167,8 +175,8 @@ Object { exports[`supported languages curl targets curl should support snippet generation 1`] = ` Object { "code": "curl --request GET \\\\ - --url 'http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark' \\\\ - --header 'Accept: application/xml'", + --url 'http://petstore.swagger.io/v2/user/login?username=woof&password=barkbarkbark' \\\\ + --header 'Accept: application/xml'", "highlightMode": "shell", } `; diff --git a/packages/oas-to-snippet/__tests__/index.test.js b/packages/oas-to-snippet/__tests__/index.test.js index 7cb48e086..c9ea8e62b 100644 --- a/packages/oas-to-snippet/__tests__/index.test.js +++ b/packages/oas-to-snippet/__tests__/index.test.js @@ -109,6 +109,36 @@ test('should pass through form encoded values to code snippet', () => { expect(code).toMatch('body: encodedParams'); }); +test('should have special indents for curl snippets', () => { + const { code } = generateCodeSnippet( + oas, + { + path: '/body', + method: 'get', + requestBody: { + content: { + 'application/x-www-form-urlencoded': { + schema: { + type: 'object', + required: ['a'], + properties: { + a: { + type: 'string', + }, + }, + }, + }, + }, + }, + }, + { formData: { a: 'test', b: [1, 2, 3] } }, + {}, + 'curl' + ); + + expect(code).toMatchSnapshot(); +}); + test('should not contain proxy url', () => { const { code } = generateCodeSnippet( new Oas({ [extensions.PROXY_ENABLED]: true }), diff --git a/packages/oas-to-snippet/src/supportedLanguages.js b/packages/oas-to-snippet/src/supportedLanguages.js index b8cf6e9e4..9a30da686 100644 --- a/packages/oas-to-snippet/src/supportedLanguages.js +++ b/packages/oas-to-snippet/src/supportedLanguages.js @@ -53,6 +53,7 @@ module.exports = { name: 'cURL', opts: { escapeBrackets: true, + indent: ' ', }, }, },