[typescript-fetch] Support deepObject serialization in query parameters (#2234)

* [typescript-fetch] Support deep objects in query parameters

Support objects in query parameters as specified by the deepObject serialization style of OpenAPI 3:
`Object id = {"role": "admin", "firstName": "Alex"}` => `/users?id[role]=admin&id[firstName]=Alex`

Not supported due to ambiguity are arrays of objects (e.g. `person[name]=Alice&person[name]=Bob` could be an array of person objects with one name each, or one person object with an array property `name`).

* [typescript-fetch] Update sample files

* [typescript-fetch] URL-encode query parameter array elements
This commit is contained in:
Lukas S 2019-03-12 15:20:22 +01:00 committed by William Cheng
parent c8ecc5bf45
commit 317168fe9d
6 changed files with 60 additions and 30 deletions

View File

@ -161,7 +161,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -183,15 +183,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}

View File

@ -172,7 +172,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -194,15 +194,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}

View File

@ -172,7 +172,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -194,15 +194,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}

View File

@ -172,7 +172,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -194,15 +194,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}

View File

@ -172,7 +172,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -194,15 +194,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}

View File

@ -172,7 +172,7 @@ export class Configuration {
export type Json = any;
export type HTTPMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
export type HTTPHeaders = { [key: string]: string };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> };
export type HTTPQuery = { [key: string]: string | number | null | boolean | Array<string | number | null | boolean> | HTTPQuery };
export type HTTPBody = Json | FormData;
export type ModelPropertyNaming = 'camelCase' | 'snake_case' | 'PascalCase' | 'original';
@ -194,15 +194,20 @@ export function exists(json: any, key: string) {
return value !== null && value !== undefined;
}
export function querystring(params: HTTPQuery) {
export function querystring(params: HTTPQuery, prefix: string = '') {
return Object.keys(params)
.map((key) => {
const fullKey = prefix + (prefix.length ? `[${key}]` : key);
const value = params[key];
if (value instanceof Array) {
const multiValue = value.join(`&${encodeURIComponent(key)}=`);
return `${encodeURIComponent(key)}=${multiValue}`;
const multiValue = value.map(singleValue => encodeURIComponent(singleValue))
.join(`&${encodeURIComponent(fullKey)}=`);
return `${encodeURIComponent(fullKey)}=${multiValue}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`
if (value instanceof Object) {
return querystring(value, fullKey);
}
return `${encodeURIComponent(fullKey)}=${encodeURIComponent(String(value))}`;
})
.join('&');
}