[angular-typescript] fix using form data (#6574)

* #6457: fix sending form params

* #6457: fix sending form params using files

* #6457: fix sending form params with files for angular 4.3

* #6457: generate samples

* #6457: [typescript-angular] fix form data submission in IE/Edge

* re-apply [typescript-angular] add customized encoder to use '+' char in query parameter #6306 (#6334)

* adapt for HttpClient: [typescript-angular] add customized encoder to use '+' char in query parameter #6306 (#6334)

* generate samples

* #6457: fix url parameter encoder imports for angular <4.3

* #6457: generate samples
This commit is contained in:
Esteban Marin
2017-10-10 16:55:50 +02:00
committed by wing328
parent 02d25f5a14
commit dc88ed99ae
24 changed files with 351 additions and 201 deletions

0
bin/typescript-angular-v4.3-petstore-with-npm.sh Normal file → Executable file
View File

View File

@@ -0,0 +1,10 @@
set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
If Not Exist %executable% (
mvn clean package
)
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.yaml -c bin\typescript-petstore-npm.json -l typescript-angular -o samples\client\petstore\typescript-angular-v4.3\npm --additional-properties ngVersion=4.3
java %JAVA_OPTS% -jar %executable% %ags%

View File

@@ -20,7 +20,12 @@ import { {{classname}} } from '../{{filename}}';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
{{#useHttpClient}}
import { CustomHttpUrlEncodingCodec } from '../encoder';
{{/useHttpClient}}
{{^useHttpClient}}
import { CustomQueryEncoderHelper } from '../encoder';
{{/useHttpClient}}
{{#withInterfaces}}
import { {{classname}}Interface } from './{{classname}}Interface';
{{/withInterfaces}}
@@ -69,11 +74,6 @@ export class {{classname}} {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
{{^useHttpClient}}
{{! not sure why we used to generate a second method here rather than inlining those few lines of code,
but let's keep it for now for the sake of backwards compatiblity. }}
@@ -121,7 +121,12 @@ export class {{classname}} {
{{/allParams}}
{{#hasQueryParams}}
let queryParameters = new {{#useHttpClient}}HttpParams{{/useHttpClient}}{{^useHttpClient}}URLSearchParams{{/useHttpClient}}();
{{#useHttpClient}}
let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
{{/useHttpClient}}
{{^useHttpClient}}
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
{{/useHttpClient}}
{{#queryParams}}
{{#isListContainer}}
if ({{paramName}}) {
@@ -207,39 +212,59 @@ export class {{classname}} {
'{{{mediaType}}}'{{#hasMore}},{{/hasMore}}
{{/consumes}}
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
{{#formParams}}
{{#isFile}}
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
{{/isFile}}
{{/formParams}}
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (useForm) {
formParams = new FormData();
} else {
{{#useHttpClient}}
formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
{{/useHttpClient}}
{{^useHttpClient}}
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
{{/useHttpClient}}
}
{{#formParams}}
{{#isListContainer}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
{{paramName}}.forEach((element) => {
formParams.append('{{baseName}}', <any>element);
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', <any>element){{#useHttpClient}} || formParams{{/useHttpClient}};
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
formParams.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])){{#useHttpClient}} || formParams{{/useHttpClient}};
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if ({{paramName}} !== undefined) {
formParams.set('{{baseName}}', <any>{{paramName}});
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', <any>{{paramName}}){{#useHttpClient}} || formParams{{/useHttpClient}};
}
{{/isListContainer}}
{{/formParams}}
{{/hasFormParams}}
{{#useHttpClient}}
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<any>{{/isResponseFile}}(`${this.basePath}{{{path}}}`, {{#bodyParam}}{{paramName}}, {{/bodyParam}}{{#hasFormParams}}formParams, {{/hasFormParams}}{
return this.httpClient.{{httpMethod}}{{^isResponseFile}}<any>{{/isResponseFile}}(`${this.basePath}{{{path}}}`, {{#bodyParam}}{{paramName}}, {{/bodyParam}}
{{#hasFormParams}}convertFormParamsToString ? formParams.toString() : formParams, {{/hasFormParams}}{
{{#hasQueryParams}}
params: queryParameters,
{{/hasQueryParams}}
@@ -258,7 +283,7 @@ export class {{classname}} {
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
{{/hasFormParams}}
{{#isResponseFile}}
responseType: ResponseContentType.Blob,

View File

@@ -1,5 +1,28 @@
import { QueryEncoder } from "@angular/http";
{{#useHttpClient}}
import { HttpUrlEncodingCodec } from '@angular/common/http';
{{/useHttpClient}}
{{^useHttpClient}}
import { QueryEncoder } from '@angular/http';
{{/useHttpClient}}
{{#useHttpClient}}
/**
* CustomHttpUrlEncodingCodec
* Fix plus sign (+) not encoding, so sent as blank space
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
*/
export class CustomHttpUrlEncodingCodec extends HttpUrlEncodingCodec {
encodeKey(k: string): string {
k = super.encodeKey(k);
return k.replace(/\+/gi, '%2B');
}
encodeValue(v: string): string {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
{{/useHttpClient}}
{{^useHttpClient}}
/**
* CustomQueryEncoderHelper
* Fix plus sign (+) not encoding, so sent as blank space
@@ -14,4 +37,6 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}
{{/useHttpClient}}

View File

@@ -60,11 +60,6 @@ export class PetService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
*
* @summary Add a new pet to the store
@@ -282,7 +277,7 @@ export class PetService {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (status) {
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
}
@@ -321,7 +316,7 @@ export class PetService {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (tags) {
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
}
@@ -442,22 +437,34 @@ export class PetService {
let consumes: string[] = [
'application/x-www-form-urlencoded'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (name !== undefined) {
formParams.set('name', <any>name);
formParams.append('name', <any>name);
}
if (status !== undefined) {
formParams.set('status', <any>status);
formParams.append('status', <any>status);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
@@ -494,23 +501,37 @@ export class PetService {
let consumes: string[] = [
'multipart/form-data'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (additionalMetadata !== undefined) {
formParams.set('additionalMetadata', <any>additionalMetadata);
formParams.append('additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.set('file', <any>file);
formParams.append('file', <any>file);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037

View File

@@ -59,11 +59,6 @@ export class StoreService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
* @summary Delete purchase order by ID

View File

@@ -59,11 +59,6 @@ export class UserService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* This can only be done by the logged in user.
* @summary Create user
@@ -342,7 +337,7 @@ export class UserService {
throw new Error('Required parameter password was null or undefined when calling loginUser.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (username !== undefined) {
queryParameters.set('username', <any>username);
}

View File

@@ -1,4 +1,4 @@
import { QueryEncoder } from "@angular/http";
import { QueryEncoder } from '@angular/http';
/**
* CustomQueryEncoderHelper
@@ -14,4 +14,5 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}

View File

@@ -60,11 +60,6 @@ export class PetService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
*
* @summary Add a new pet to the store
@@ -282,7 +277,7 @@ export class PetService {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (status) {
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
}
@@ -321,7 +316,7 @@ export class PetService {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (tags) {
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
}
@@ -442,22 +437,34 @@ export class PetService {
let consumes: string[] = [
'application/x-www-form-urlencoded'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (name !== undefined) {
formParams.set('name', <any>name);
formParams.append('name', <any>name);
}
if (status !== undefined) {
formParams.set('status', <any>status);
formParams.append('status', <any>status);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
@@ -494,23 +501,37 @@ export class PetService {
let consumes: string[] = [
'multipart/form-data'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (additionalMetadata !== undefined) {
formParams.set('additionalMetadata', <any>additionalMetadata);
formParams.append('additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.set('file', <any>file);
formParams.append('file', <any>file);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037

View File

@@ -59,11 +59,6 @@ export class StoreService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
* @summary Delete purchase order by ID

View File

@@ -59,11 +59,6 @@ export class UserService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* This can only be done by the logged in user.
* @summary Create user
@@ -342,7 +337,7 @@ export class UserService {
throw new Error('Required parameter password was null or undefined when calling loginUser.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (username !== undefined) {
queryParameters.set('username', <any>username);
}

View File

@@ -1,4 +1,4 @@
import { QueryEncoder } from "@angular/http";
import { QueryEncoder } from '@angular/http';
/**
* CustomQueryEncoderHelper
@@ -14,4 +14,5 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}

View File

@@ -61,11 +61,6 @@ export class PetService implements PetServiceInterface {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
*
* @summary Add a new pet to the store
@@ -283,7 +278,7 @@ export class PetService implements PetServiceInterface {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (status) {
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
}
@@ -322,7 +317,7 @@ export class PetService implements PetServiceInterface {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (tags) {
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
}
@@ -443,22 +438,34 @@ export class PetService implements PetServiceInterface {
let consumes: string[] = [
'application/x-www-form-urlencoded'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (name !== undefined) {
formParams.set('name', <any>name);
formParams.append('name', <any>name);
}
if (status !== undefined) {
formParams.set('status', <any>status);
formParams.append('status', <any>status);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
@@ -495,23 +502,37 @@ export class PetService implements PetServiceInterface {
let consumes: string[] = [
'multipart/form-data'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (additionalMetadata !== undefined) {
formParams.set('additionalMetadata', <any>additionalMetadata);
formParams.append('additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.set('file', <any>file);
formParams.append('file', <any>file);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037

View File

@@ -60,11 +60,6 @@ export class StoreService implements StoreServiceInterface {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
* @summary Delete purchase order by ID

View File

@@ -60,11 +60,6 @@ export class UserService implements UserServiceInterface {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* This can only be done by the logged in user.
* @summary Create user
@@ -343,7 +338,7 @@ export class UserService implements UserServiceInterface {
throw new Error('Required parameter password was null or undefined when calling loginUser.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (username !== undefined) {
queryParameters.set('username', <any>username);
}

View File

@@ -1,4 +1,4 @@
import { QueryEncoder } from "@angular/http";
import { QueryEncoder } from '@angular/http';
/**
* CustomQueryEncoderHelper
@@ -14,4 +14,5 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}

View File

@@ -23,7 +23,7 @@ import { Pet } from '../model/pet';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';
import { CustomHttpUrlEncodingCodec } from '../encoder';
@Injectable()
@@ -58,6 +58,7 @@ export class PetService {
}
/**
* Add a new pet to the store
*
@@ -78,7 +79,8 @@ export class PetService {
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}
return this.httpClient.post<any>(`${this.basePath}/pet`, body, {
return this.httpClient.post<any>(`${this.basePath}/pet`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -108,7 +110,8 @@ export class PetService {
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}
return this.httpClient.delete<any>(`${this.basePath}/pet/${encodeURIComponent(petId)}`, {
return this.httpClient.delete<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -124,7 +127,7 @@ export class PetService {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
let queryParameters = new HttpParams();
let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
if (status) {
queryParameters = queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
}
@@ -139,7 +142,8 @@ export class PetService {
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}
return this.httpClient.get<any>(`${this.basePath}/pet/findByStatus`, {
return this.httpClient.get<any>(`${this.basePath}/pet/findByStatus`,
{
params: queryParameters,
headers: headers,
withCredentials: this.configuration.withCredentials,
@@ -156,7 +160,7 @@ export class PetService {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
let queryParameters = new HttpParams();
let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
if (tags) {
queryParameters = queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
}
@@ -171,7 +175,8 @@ export class PetService {
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}
return this.httpClient.get<any>(`${this.basePath}/pet/findByTags`, {
return this.httpClient.get<any>(`${this.basePath}/pet/findByTags`,
{
params: queryParameters,
headers: headers,
withCredentials: this.configuration.withCredentials,
@@ -195,7 +200,8 @@ export class PetService {
headers = headers.set('api_key', this.configuration.apiKeys["api_key"]);
}
return this.httpClient.get<any>(`${this.basePath}/pet/${encodeURIComponent(petId)}`, {
return this.httpClient.get<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -221,7 +227,8 @@ export class PetService {
headers = headers.set('Authorization', 'Bearer ' + accessToken);
}
return this.httpClient.put<any>(`${this.basePath}/pet`, body, {
return this.httpClient.put<any>(`${this.basePath}/pet`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -253,19 +260,28 @@ export class PetService {
let consumes: string[] = [
'application/x-www-form-urlencoded'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (name !== undefined) {
formParams.set('name', <any>name);
}
if (status !== undefined) {
formParams.set('status', <any>status);
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
}
return this.httpClient.post<any>(`${this.basePath}/pet/${encodeURIComponent(petId)}`, formParams, {
if (name !== undefined) {
formParams = formParams.append('name', <any>name) || formParams;
}
if (status !== undefined) {
formParams = formParams.append('status', <any>status) || formParams;
}
return this.httpClient.post<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`,
convertFormParamsToString ? formParams.toString() : formParams, {
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -297,20 +313,31 @@ export class PetService {
let consumes: string[] = [
'multipart/form-data'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (additionalMetadata !== undefined) {
formParams.set('additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.set('file', <any>file);
if (useForm) {
formParams = new FormData();
} else {
formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
}
return this.httpClient.post<any>(`${this.basePath}/pet/${encodeURIComponent(petId)}/uploadImage`, formParams, {
if (additionalMetadata !== undefined) {
formParams = formParams.append('additionalMetadata', <any>additionalMetadata) || formParams;
}
if (file !== undefined) {
formParams = formParams.append('file', <any>file) || formParams;
}
return this.httpClient.post<any>(`${this.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage`,
convertFormParamsToString ? formParams.toString() : formParams, {
headers: headers,
withCredentials: this.configuration.withCredentials,
});

View File

@@ -22,7 +22,7 @@ import { Order } from '../model/order';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';
import { CustomHttpUrlEncodingCodec } from '../encoder';
@Injectable()
@@ -57,6 +57,7 @@ export class StoreService {
}
/**
* Delete purchase order by ID
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
@@ -69,7 +70,8 @@ export class StoreService {
let headers = this.defaultHeaders;
return this.httpClient.delete<any>(`${this.basePath}/store/order/${encodeURIComponent(orderId)}`, {
return this.httpClient.delete<any>(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -88,7 +90,8 @@ export class StoreService {
headers = headers.set('api_key', this.configuration.apiKeys["api_key"]);
}
return this.httpClient.get<any>(`${this.basePath}/store/inventory`, {
return this.httpClient.get<any>(`${this.basePath}/store/inventory`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -106,7 +109,8 @@ export class StoreService {
let headers = this.defaultHeaders;
return this.httpClient.get<any>(`${this.basePath}/store/order/${encodeURIComponent(orderId)}`, {
return this.httpClient.get<any>(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -124,7 +128,8 @@ export class StoreService {
let headers = this.defaultHeaders;
return this.httpClient.post<any>(`${this.basePath}/store/order`, body, {
return this.httpClient.post<any>(`${this.basePath}/store/order`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});

View File

@@ -22,7 +22,7 @@ import { User } from '../model/user';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
import { CustomQueryEncoderHelper } from '../encoder';
import { CustomHttpUrlEncodingCodec } from '../encoder';
@Injectable()
@@ -57,6 +57,7 @@ export class UserService {
}
/**
* Create user
* This can only be done by the logged in user.
@@ -69,7 +70,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.post<any>(`${this.basePath}/user`, body, {
return this.httpClient.post<any>(`${this.basePath}/user`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -87,7 +89,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.post<any>(`${this.basePath}/user/createWithArray`, body, {
return this.httpClient.post<any>(`${this.basePath}/user/createWithArray`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -105,7 +108,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.post<any>(`${this.basePath}/user/createWithList`, body, {
return this.httpClient.post<any>(`${this.basePath}/user/createWithList`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -123,7 +127,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.delete<any>(`${this.basePath}/user/${encodeURIComponent(username)}`, {
return this.httpClient.delete<any>(`${this.basePath}/user/${encodeURIComponent(String(username))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -141,7 +146,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.get<any>(`${this.basePath}/user/${encodeURIComponent(username)}`, {
return this.httpClient.get<any>(`${this.basePath}/user/${encodeURIComponent(String(username))}`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -161,7 +167,7 @@ export class UserService {
throw new Error('Required parameter password was null or undefined when calling loginUser.');
}
let queryParameters = new HttpParams();
let queryParameters = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
if (username !== undefined) {
queryParameters = queryParameters.set('username', <any>username);
}
@@ -171,7 +177,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.get<any>(`${this.basePath}/user/login`, {
return this.httpClient.get<any>(`${this.basePath}/user/login`,
{
params: queryParameters,
headers: headers,
withCredentials: this.configuration.withCredentials,
@@ -186,7 +193,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.get<any>(`${this.basePath}/user/logout`, {
return this.httpClient.get<any>(`${this.basePath}/user/logout`,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});
@@ -208,7 +216,8 @@ export class UserService {
let headers = this.defaultHeaders;
return this.httpClient.put<any>(`${this.basePath}/user/${encodeURIComponent(username)}`, body, {
return this.httpClient.put<any>(`${this.basePath}/user/${encodeURIComponent(String(username))}`, body,
{
headers: headers,
withCredentials: this.configuration.withCredentials,
});

View File

@@ -1,11 +1,11 @@
import { QueryEncoder } from "@angular/http";
import { HttpUrlEncodingCodec } from '@angular/common/http';
/**
* CustomQueryEncoderHelper
* CustomHttpUrlEncodingCodec
* Fix plus sign (+) not encoding, so sent as blank space
* See: https://github.com/angular/angular/issues/11058#issuecomment-247367318
*/
export class CustomQueryEncoderHelper extends QueryEncoder {
export class CustomHttpUrlEncodingCodec extends HttpUrlEncodingCodec {
encodeKey(k: string): string {
k = super.encodeKey(k);
return k.replace(/\+/gi, '%2B');
@@ -14,4 +14,5 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}

View File

@@ -60,11 +60,6 @@ export class PetService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
*
* @summary Add a new pet to the store
@@ -282,7 +277,7 @@ export class PetService {
throw new Error('Required parameter status was null or undefined when calling findPetsByStatus.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (status) {
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
}
@@ -321,7 +316,7 @@ export class PetService {
throw new Error('Required parameter tags was null or undefined when calling findPetsByTags.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (tags) {
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
}
@@ -442,22 +437,34 @@ export class PetService {
let consumes: string[] = [
'application/x-www-form-urlencoded'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (name !== undefined) {
formParams.set('name', <any>name);
formParams.append('name', <any>name);
}
if (status !== undefined) {
formParams.set('status', <any>status);
formParams.append('status', <any>status);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
@@ -494,23 +501,37 @@ export class PetService {
let consumes: string[] = [
'multipart/form-data'
];
let canConsumeForm = this.canConsumeForm(consumes);
const canConsumeForm = this.canConsumeForm(consumes);
let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
if (useForm) {
formParams = new FormData();
} else {
// TODO: this fails if a parameter is a file, the api can't consume "multipart/form-data" and a blob is passed.
convertFormParamsToString = true;
formParams = new URLSearchParams('', new CustomQueryEncoderHelper());
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}
if (additionalMetadata !== undefined) {
formParams.set('additionalMetadata', <any>additionalMetadata);
formParams.append('additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.set('file', <any>file);
formParams.append('file', <any>file);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Post,
headers: headers,
body: formParams.toString(),
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials:this.configuration.withCredentials
});
// https://github.com/swagger-api/swagger-codegen/issues/4037

View File

@@ -59,11 +59,6 @@ export class StoreService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
* @summary Delete purchase order by ID

View File

@@ -59,11 +59,6 @@ export class UserService {
}
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
return mime != null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
}
/**
* This can only be done by the logged in user.
* @summary Create user
@@ -342,7 +337,7 @@ export class UserService {
throw new Error('Required parameter password was null or undefined when calling loginUser.');
}
let queryParameters = new URLSearchParams();
let queryParameters = new URLSearchParams('', new CustomQueryEncoderHelper());
if (username !== undefined) {
queryParameters.set('username', <any>username);
}

View File

@@ -1,4 +1,4 @@
import { QueryEncoder } from "@angular/http";
import { QueryEncoder } from '@angular/http';
/**
* CustomQueryEncoderHelper
@@ -14,4 +14,5 @@ export class CustomQueryEncoderHelper extends QueryEncoder {
v = super.encodeValue(v);
return v.replace(/\+/gi, '%2B');
}
}
}