Skip to content

Commit

Permalink
fix: default style and explode for params
Browse files Browse the repository at this point in the history
fixes #1016
  • Loading branch information
RomanHotsiy committed Jun 27, 2020
1 parent 418b748 commit 633d712
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/services/__tests__/fixtures/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
51 changes: 51 additions & 0 deletions src/services/__tests__/models/FieldModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 27 additions & 14 deletions src/services/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 633d712

Please sign in to comment.