Skip to content

Commit

Permalink
fix: incorrect serialization of parameter sample with hypen
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Jul 29, 2019
1 parent 299a59f commit f7dd658
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,20 @@ 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) {
name = escapeURITemplateName(name);
const suffix = explode ? '*' : '';
const template = new URI.Template(`{?${name}${suffix}}`);

return template.expand({ [name]: value }).substring(1);
return unescapeURITemplateName(template.expand({ [name]: value }).substring(1));
}

/*
Expand All @@ -188,7 +197,6 @@ export function urlFormEncodePayload(
switch (style) {
case 'form':
return serializeFormValue(fieldName, explode, fieldVal);
break;
case 'spaceDelimited':
return delimitedEncodeField(fieldVal, fieldName, '%20');
case 'pipeDelimited':
Expand All @@ -211,6 +219,7 @@ function serializePathParameter(
explode: boolean,
value: any,
): string {
name = escapeURITemplateName(name);
const suffix = explode ? '*' : '';
let prefix = '';

Expand All @@ -222,7 +231,7 @@ function serializePathParameter(

const template = new URI.Template(`{${prefix}${name}${suffix}}`);

return template.expand({ [name]: value });
return unescapeURITemplateName(template.expand({ [name]: value }));
}

function serializeQueryParameter(
Expand Down Expand Up @@ -273,12 +282,13 @@ function serializeHeaderParameter(
explode: boolean,
value: any,
): string {
name = escapeURITemplateName(name);
switch (style) {
case 'simple':
const suffix = explode ? '*' : '';
const template = new URI.Template(`{${name}${suffix}}`);

return template.expand({ [name]: value });
return unescapeURITemplateName(template.expand({ [name]: value }));
default:
console.warn('Unexpected style for header: ' + style);
return '';
Expand Down

0 comments on commit f7dd658

Please sign in to comment.