Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
feat(converter): add support of style keyword for query parameters (#29)
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
derevnjuk authored Apr 16, 2020
1 parent d0ee788 commit d15ac55
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 51 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Automated deploy

on:
push:
tags:
- 'v*'
release:
types:
- created

jobs:
release:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion __tests__/oas2har.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('GitHub swagger v2 JSON to HAR', async () => {
});

test('Petstore OpenApi v3 YAML to JSON converts to HAR', async () => {
const [firstRequest] = await oasToHarList(process.cwd() + '/__tests__/fixtures/petstore_oas.yaml');
const [firstRequest, ...rest] = await oasToHarList(process.cwd() + '/__tests__/fixtures/petstore_oas.yaml');
const { har } = firstRequest;

expect(har.method).toEqual('PUT');
Expand Down
101 changes: 101 additions & 0 deletions __tests__/params-serialization.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { paramsSerialization } = require('../src/converter')
const { toFlattenArray } = require('../src/utils')

describe('Query Parameters', () => {
const array = [1, 2, 3, 4, 5]
const object = { role: 'admin', name: { first: 'Jon', last: 'Snow' }, filter: { gt: 1, lte: 10 } }
const primitive = 1
const name = 'id'

test('should serialize primitive with default method', () => {
const result = paramsSerialization(name, primitive)
expect(result.queryString).toEqual('id=1')
expect(result.values).toEqual([{ name, value: primitive + '' }])
})

test('should serialize array with default method', () => {
const result = paramsSerialization(name, array)
expect(result.queryString).toEqual(`${array.map((x) => `${name}=${x}`).join('&')}`)
expect(result.values).toEqual(array.map((x) => ({ name, value: x + '' })))
})

test('should serialize object with default method', () => {
const result = paramsSerialization(name, object)
expect(result.queryString).toEqual(
'role=admin&name[first]=Jon&name[last]=Snow&filter[gt]=1&filter[lte]=10'
)
expect(result.values).toEqual([
{ name: 'role', value: 'admin' },
{ name: 'name[first]', value: 'Jon' },
{ name: 'name[last]', value: 'Snow' },
{ name: 'filter[gt]', value: '1' },
{ name: 'filter[lte]', value: '10' },
])
})

test('should serialize primitive with style: form and explode: false', () => {
const result = paramsSerialization(name, primitive, { explode: false })
expect(result.queryString).toEqual('id=1')
expect(result.values).toEqual([{ name, value: primitive + '' }])
})

test('should serialize array with style: form and explode: false', () => {
const result = paramsSerialization(name, array, { explode: false })
expect(result.queryString).toEqual(`${name}=${array.join(',')}`)
expect(result.values).toEqual([{ name, value: array.join(',') }])
})

test('should serialize object with style: form and explode: false', () => {
const result = paramsSerialization(name, object, { explode: false })
expect(result.queryString).toEqual(`${name}=${toFlattenArray(object).join(',')}`)
expect(result.values).toEqual([{ name, value: toFlattenArray(object).join(',') }])
})

test('should ignore an object with style: spaceDelimited', () => {
const result = paramsSerialization(name, object, { explode: true, style: 'pipeDelimited' })
expect(result.queryString).toEqual('')
})

test('should ignore an object with style: pipeDelimited', () => {
const result = paramsSerialization(name, object, { explode: true, style: 'pipeDelimited' })
expect(result.queryString).toEqual('')
})

test('should serialize array with style: spaceDelimited and explode: true', () => {
const result = paramsSerialization(name, array, { explode: true, style: 'spaceDelimited' })
expect(result.queryString).toEqual(`${array.map((x) => `${name}=${x}`).join('&')}`)
expect(result.values).toEqual(array.map((x) => ({ name, value: x + '' })))
})

test('should serialize array with style: spaceDelimited and explode: false', () => {
const result = paramsSerialization(name, array, { explode: false, style: 'spaceDelimited' })
expect(result.queryString).toEqual(`${name}=${array.join('%20')}`)
expect(result.values).toEqual([{ name, value: array.join('%20') }])
})

test('should serialize array with style: pipeDelimited and explode: true', () => {
const result = paramsSerialization(name, array, { explode: true, style: 'pipeDelimited' })
expect(result.queryString).toEqual(`${array.map((x) => `${name}=${x}`).join('&')}`)
expect(result.values).toEqual(array.map((x) => ({name, value: x + ''})))
})

test('should serialize array with style: pipeDelimited and explode: false', () => {
const result = paramsSerialization(name, array, { explode: false, style: 'pipeDelimited' })
expect(result.queryString).toEqual(`${name}=${array.join('|')}`)
expect(result.values).toEqual([{ name, value: array.join('|') }])
})

test('should serialize object with style: deepObject', () => {
const result = paramsSerialization(name, object, { style: 'deepObject' })
expect(result.queryString).toEqual(
'role=admin&name[first]=Jon&name[last]=Snow&filter[gt]=1&filter[lte]=10'
)
expect(result.values).toEqual([
{ name: 'role', value: 'admin' },
{ name: 'name[first]', value: 'Jon' },
{ name: 'name[last]', value: 'Snow' },
{ name: 'filter[gt]', value: '1' },
{ name: 'filter[lte]', value: '10' },
])
})
})
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"@types/har-format": "^1.2.4",
"@types/swagger-schema-official": "^2.0.20",
"js-yaml": "^3.13.1",
"url-template": "^2.0.8",
"jstoxml": "^1.6.5"
"jstoxml": "^1.6.5",
"qs": "^6.9.3",
"url-template": "^2.0.8"
},
"typings": "./src/index.d.ts",
"repository": {
Expand Down
Loading

0 comments on commit d15ac55

Please sign in to comment.