fix: incorrect serialization of some parameter samples

fixes #992
This commit is contained in:
Roman Hotsiy 2019-08-01 10:15:43 +03:00
parent 36ebbb1c89
commit aba45dbbe6
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
2 changed files with 20 additions and 24 deletions

View File

@ -513,7 +513,7 @@ describe('Utils', () => {
}, },
{ {
location: 'header', location: 'header',
name: 'id', name: 'x-id',
description: 'header parameters', description: 'header parameters',
cases: [ cases: [
{ {
@ -549,9 +549,7 @@ describe('Utils', () => {
locationTestGroup.cases.forEach(valueTypeTestGroup => { locationTestGroup.cases.forEach(valueTypeTestGroup => {
describe(valueTypeTestGroup.description, () => { describe(valueTypeTestGroup.description, () => {
valueTypeTestGroup.cases.forEach(testCase => { valueTypeTestGroup.cases.forEach(testCase => {
it(`should serialize correctly when style is ${testCase.style} and explode is ${ it(`should serialize correctly when style is ${testCase.style} and explode is ${testCase.explode}`, () => {
testCase.explode
}`, () => {
const parameter: OpenAPIParameter = { const parameter: OpenAPIParameter = {
name: locationTestGroup.name, name: locationTestGroup.name,
in: locationTestGroup.location, in: locationTestGroup.location,

View File

@ -163,20 +163,16 @@ function deepObjectEncodeField(fieldVal: any, fieldName: string): string {
} }
} }
// URI.Template doesn't support names with hypen, while OpenAPI allow
function escapeURITemplateName(template: string): string {
return template.replace(/-/g, '%2D');
}
function unescapeURITemplateName(template: string): string {
return template.replace(/%2D/g, '-');
}
function serializeFormValue(name: string, explode: boolean, value: any) { function serializeFormValue(name: string, explode: boolean, value: any) {
name = escapeURITemplateName(name); // Use RFC6570 safe name ([a-zA-Z0-9_]) and replace with our name later
// e.g. URI.template doesn't parse names with hypen (-) which are valid query param names
const safeName = '__redoc_param_name__';
const suffix = explode ? '*' : ''; const suffix = explode ? '*' : '';
const template = new URI.Template(`{?${name}${suffix}}`); const template = new URI.Template(`{?${safeName}${suffix}}`);
return unescapeURITemplateName(template.expand({ [name]: value }).substring(1)); return template
.expand({ [safeName]: value })
.substring(1)
.replace(/__redoc_param_name__/g, name);
} }
/* /*
@ -219,7 +215,6 @@ function serializePathParameter(
explode: boolean, explode: boolean,
value: any, value: any,
): string { ): string {
name = escapeURITemplateName(name);
const suffix = explode ? '*' : ''; const suffix = explode ? '*' : '';
let prefix = ''; let prefix = '';
@ -229,9 +224,12 @@ function serializePathParameter(
prefix = ';'; prefix = ';';
} }
const template = new URI.Template(`{${prefix}${name}${suffix}}`); // Use RFC6570 safe name ([a-zA-Z0-9_]) and replace with our name later
// e.g. URI.template doesn't parse names with hypen (-) which are valid query param names
const safeName = '__redoc_param_name__';
const template = new URI.Template(`{${prefix}${safeName}${suffix}}`);
return unescapeURITemplateName(template.expand({ [name]: value })); return template.expand({ [safeName]: value }).replace(/__redoc_param_name__/g, name);
} }
function serializeQueryParameter( function serializeQueryParameter(
@ -277,18 +275,18 @@ function serializeQueryParameter(
} }
function serializeHeaderParameter( function serializeHeaderParameter(
name: string,
style: OpenAPIParameterStyle, style: OpenAPIParameterStyle,
explode: boolean, explode: boolean,
value: any, value: any,
): string { ): string {
name = escapeURITemplateName(name);
switch (style) { switch (style) {
case 'simple': case 'simple':
const suffix = explode ? '*' : ''; const suffix = explode ? '*' : '';
const template = new URI.Template(`{${name}${suffix}}`);
return unescapeURITemplateName(template.expand({ [name]: value })); // name is not important here, so use RFC6570 safe name ([a-zA-Z0-9_])
const name = '__redoc_param_name__';
const template = new URI.Template(`{${name}${suffix}}`);
return decodeURIComponent(template.expand({ [name]: value }));
default: default:
console.warn('Unexpected style for header: ' + style); console.warn('Unexpected style for header: ' + style);
return ''; return '';
@ -324,7 +322,7 @@ export function serializeParameterValue(parameter: OpenAPIParameter, value: any)
case 'query': case 'query':
return serializeQueryParameter(name, style, explode, value); return serializeQueryParameter(name, style, explode, value);
case 'header': case 'header':
return serializeHeaderParameter(name, style, explode, value); return serializeHeaderParameter(style, explode, value);
case 'cookie': case 'cookie':
return serializeCookieParameter(name, style, explode, value); return serializeCookieParameter(name, style, explode, value);
default: default: