fix: default style and explode for params

fixes #1016
This commit is contained in:
Roman Hotsiy 2020-06-27 19:22:49 +03:00
parent 418b74848d
commit 633d71293f
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
3 changed files with 93 additions and 14 deletions

View File

@ -17,6 +17,21 @@
"schema": { "type": "array" },
"style": "form",
"explode": true
},
"queryParamWithNoStyle": {
"in": "query",
"name": "serialization_test_name",
"schema": { "type": "array" }
},
"pathParamWithNoStyle": {
"in": "path",
"name": "serialization_test_name",
"schema": { "type": "array" }
},
"cookieParamWithNoStyle": {
"in": "cookie",
"name": "serialization_test_name",
"schema": { "type": "array" }
}
},
"headers": {

View File

@ -43,6 +43,57 @@ describe('Models', () => {
expect(field.explode).toEqual(true);
});
test('field details relevant for default path param serialization', () => {
const field = new FieldModel(
parser,
{
$ref: '#/components/parameters/pathParamWithNoStyle',
},
'#/components/parameters/pathParamWithNoStyle',
opts,
);
expect(field.name).toEqual('serialization_test_name');
expect(field.in).toEqual('path');
expect(field.schema.type).toEqual('array');
expect(field.style).toEqual('simple');
expect(field.explode).toEqual(false);
});
test('field details relevant for default query parameter serialization', () => {
const field = new FieldModel(
parser,
{
$ref: '#/components/parameters/queryParamWithNoStyle',
},
'#/components/parameters/queryParamWithNoStyle',
opts,
);
expect(field.name).toEqual('serialization_test_name');
expect(field.in).toEqual('query');
expect(field.schema.type).toEqual('array');
expect(field.style).toEqual('form');
expect(field.explode).toEqual(true);
});
test('field details relevant for default cookie parameter serialization', () => {
const field = new FieldModel(
parser,
{
$ref: '#/components/parameters/queryParamWithNoStyle',
},
'#/components/parameters/queryParamWithNoStyle',
opts,
);
expect(field.name).toEqual('serialization_test_name');
expect(field.in).toEqual('query');
expect(field.schema.type).toEqual('array');
expect(field.style).toEqual('form');
expect(field.explode).toEqual(true);
});
test('field name should populated from name even if $ref (headers)', () => {
const field = new FieldModel(
parser,

View File

@ -12,18 +12,27 @@ import { extractExtensions } from '../../utils/openapi';
import { OpenAPIParser } from '../OpenAPIParser';
import { SchemaModel } from './Schema';
function getDefaultStyleValue(parameterLocation: OpenAPIParameterLocation): OpenAPIParameterStyle {
switch (parameterLocation) {
case 'header':
return 'simple';
case 'query':
return 'form';
case 'path':
return 'simple';
default:
return 'form';
}
}
const DEFAULT_SERIALIZATION: Record<
OpenAPIParameterLocation,
{ explode: boolean; style: OpenAPIParameterStyle }
> = {
path: {
style: 'simple',
explode: false,
},
query: {
style: 'form',
explode: true,
},
header: {
style: 'simple',
explode: false,
},
cookie: {
style: 'form',
explode: true,
},
};
/**
* Field or Parameter model ready to be used by components
@ -75,10 +84,14 @@ export class FieldModel {
} else if (info.style) {
this.style = info.style;
} else if (this.in) {
this.style = getDefaultStyleValue(this.in);
this.style = DEFAULT_SERIALIZATION[this.in].style;
}
this.explode = !!info.explode;
if (info.explode === undefined && this.in) {
this.explode = DEFAULT_SERIALIZATION[this.in].explode;
} else {
this.explode = !!info.explode;
}
this.deprecated = info.deprecated === undefined ? !!this.schema.deprecated : info.deprecated;
parser.exitRef(infoOrRef);