Skip to content

Commit

Permalink
fix(oas2): allow for unspecified type query params (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel A. White authored May 9, 2023
1 parent b8f6017 commit f5594fb
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"dependencies": {
"@stoplight/json": "^3.18.1",
"@stoplight/json-schema-generator": "1.0.2",
"@stoplight/types": "^13.14.0",
"@stoplight/types": "^13.15.0",
"@types/json-schema": "7.0.11",
"@types/swagger-schema-official": "~2.0.22",
"@types/type-is": "^1.6.3",
Expand Down
8 changes: 4 additions & 4 deletions src/oas/__tests__/__snapshots__/operation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Array [
"id": "schema-http_query-abc-skip-",
},
},
"style": "commaDelimited",
"style": "unspecified",
},
Object {
"id": "http_query-abc-limit",
Expand All @@ -244,7 +244,7 @@ Array [
"id": "schema-http_query-abc-limit-",
},
},
"style": "commaDelimited",
"style": "unspecified",
},
],
},
Expand Down Expand Up @@ -307,7 +307,7 @@ Array [
"id": "schema-http_query-abc-skip-",
},
},
"style": "commaDelimited",
"style": "unspecified",
},
Object {
"id": "http_query-abc-limit",
Expand All @@ -319,7 +319,7 @@ Array [
"id": "schema-http_query-abc-limit-",
},
},
"style": "commaDelimited",
"style": "unspecified",
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion src/oas2/__tests__/__fixtures__/id/bundled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default {
// hash('http_query-http_operation-service_abc-get-/users/{}-summaryOnly')
id: 'http_query-http_operation-service_abc-get-/users/{}-summaryOnly',
name: 'summaryOnly',
style: 'commaDelimited',
style: 'unspecified',
schema: {
$schema: 'http://json-schema.org/draft-07/schema#',
'x-stoplight': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Object {
Object {
"id": Any<String>,
"name": "param",
"style": "commaDelimited",
"style": "unspecified",
},
],
}
Expand Down Expand Up @@ -136,7 +136,7 @@ Object {
Object {
"id": Any<String>,
"name": "param",
"style": "commaDelimited",
"style": "unspecified",
},
],
}
Expand Down Expand Up @@ -175,7 +175,7 @@ Object {
Object {
"id": Any<String>,
"name": "param",
"style": "commaDelimited",
"style": "unspecified",
},
],
}
Expand Down
25 changes: 16 additions & 9 deletions src/oas2/transformers/__tests__/params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,23 @@ describe('params.translator', () => {
};

it.each([
{ oasStyle: 'pipes', expected: { style: HttpParamStyles.PipeDelimited } },
{ oasStyle: 'ssv', expected: { style: HttpParamStyles.SpaceDelimited } },
{ oasStyle: 'csv', expected: { style: HttpParamStyles.CommaDelimited } },
{ oasStyle: 'tsv', expected: { style: HttpParamStyles.TabDelimited } },
{ oasStyle: 'pipes', expected: { style: HttpParamStyles.PipeDelimited } },
{ oasStyle: 'multi', expected: { style: HttpParamStyles.Form, explode: true } },
{ oasStyle: 'invalidStyleValue', expected: { style: HttpParamStyles.CommaDelimited } },
])('translate style: %o', ({ oasStyle, expected }) => {
{ type: 'string', oasStyle: 'pipes', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'ssv', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'csv', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'tsv', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'pipes', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'multi', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'string', oasStyle: 'invalidStyleValue', expected: { style: HttpParamStyles.Unspecified } },
{ type: 'array', oasStyle: 'pipes', expected: { style: HttpParamStyles.PipeDelimited } },
{ type: 'array', oasStyle: 'ssv', expected: { style: HttpParamStyles.SpaceDelimited } },
{ type: 'array', oasStyle: 'csv', expected: { style: HttpParamStyles.CommaDelimited } },
{ type: 'array', oasStyle: 'tsv', expected: { style: HttpParamStyles.TabDelimited } },
{ type: 'array', oasStyle: 'pipes', expected: { style: HttpParamStyles.PipeDelimited } },
{ type: 'array', oasStyle: 'multi', expected: { style: HttpParamStyles.Form, explode: true } },
{ type: 'array', oasStyle: 'invalidStyleValue', expected: { style: HttpParamStyles.CommaDelimited } },
])('translate style: %o', ({ type, oasStyle, expected }) => {
expect(
translateToQueryParameter({}, { ...parameter, collectionFormat: oasStyle } as QueryParameter),
translateToQueryParameter({}, { ...parameter, type, collectionFormat: oasStyle } as QueryParameter),
).toMatchObject(expected);
});

Expand Down
5 changes: 5 additions & 0 deletions src/oas2/transformers/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { Oas2TranslateFunction } from '../types';

type QueryParameterStyle = {
style:
| HttpParamStyles.Unspecified
| HttpParamStyles.Form
| HttpParamStyles.CommaDelimited
| HttpParamStyles.SpaceDelimited
Expand All @@ -47,6 +48,10 @@ type QueryParameterStyle = {
};

function chooseQueryParameterStyle(parameter: DeepPartial<QueryParameter>): QueryParameterStyle {
if (parameter.type !== 'array') {
return { style: HttpParamStyles.Unspecified };
}

switch (parameter.collectionFormat) {
case 'csv':
return { style: HttpParamStyles.CommaDelimited };
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1491,10 +1491,10 @@
"@types/json-schema" "^7.0.4"
utility-types "^3.10.0"

"@stoplight/types@^13.14.0":
version "13.14.0"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-13.14.0.tgz#7842b116d28a6d1cb65227aad9a93f5f0484d2ed"
integrity sha512-RqF5Cyl5227fRPIaWa5ptMvAtNUIM4yCzSUoERUXAarxpTcOrjMJ52p9lV9XrQtpLhLNMrWn08+h0ap10R22ig==
"@stoplight/types@^13.15.0":
version "13.15.0"
resolved "https://registry.yarnpkg.com/@stoplight/types/-/types-13.15.0.tgz#d2db6820d92e5085193d03c3057d15c40a70e34f"
integrity sha512-pBLjVRrWGVd+KzTbL3qrmufSKIEp0UfziDBdt/nrTHPKrlrtVwaHdrrQMcpM23yJDU1Wcg4cHvhIuGtKCT5OmA==
dependencies:
"@types/json-schema" "^7.0.4"
utility-types "^3.10.0"
Expand Down

0 comments on commit f5594fb

Please sign in to comment.