Skip to content

Commit c509d98

Browse files
janbucharmacjohnny
authored andcommitted
[typescript-fetch] Fix uploading files (#2900)
* [typescript-fetch] Fix uploading files * Check for Blob instead of File * Update samples * Update samples * Update samples * Update samples * Regenerate samples * Bug * Manually fix samples * Implement support for Buffer and Blob in a backwards-compatible way * Rework how blob and buffer instance checking works * Check for Blob/Buffer existence properly * Avoid using Buffer and Blob in type declarations * Remove Buffer support * Update samples/client/petstore/typescript-fetch/tests/default/test/PetApi.ts Co-Authored-By: Esteban Marin <[email protected]> * Update samples/client/petstore/typescript-fetch/tests/default/test/PetApi.ts Co-Authored-By: Esteban Marin <[email protected]>
1 parent ddf21f0 commit c509d98

File tree

13 files changed

+441
-5
lines changed

13 files changed

+441
-5
lines changed

modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
export const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, "");
55

6+
const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;
7+
68
/**
79
* This is the base class for all generated API classes.
810
*/
@@ -47,7 +49,9 @@ export class BaseAPI {
4749
// do not handle correctly sometimes.
4850
url += '?' + querystring(context.query);
4951
}
50-
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
52+
const body = (context.body instanceof FormData || isBlob(context.body))
53+
? context.body
54+
: JSON.stringify(context.body);
5155
const init = {
5256
method: context.method,
5357
headers: context.headers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject
19+
*/
20+
export interface InlineObject {
21+
/**
22+
* Updated name of the pet
23+
* @type {string}
24+
* @memberof InlineObject
25+
*/
26+
name?: string;
27+
/**
28+
* Updated status of the pet
29+
* @type {string}
30+
* @memberof InlineObject
31+
*/
32+
status?: string;
33+
}
34+
35+
export function InlineObjectFromJSON(json: any): InlineObject {
36+
return {
37+
'name': !exists(json, 'name') ? undefined : json['name'],
38+
'status': !exists(json, 'status') ? undefined : json['status'],
39+
};
40+
}
41+
42+
export function InlineObjectToJSON(value?: InlineObject): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'name': value.name,
48+
'status': value.status,
49+
};
50+
}
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject1
19+
*/
20+
export interface InlineObject1 {
21+
/**
22+
* Additional data to pass to server
23+
* @type {string}
24+
* @memberof InlineObject1
25+
*/
26+
additionalMetadata?: string;
27+
/**
28+
* file to upload
29+
* @type {Blob}
30+
* @memberof InlineObject1
31+
*/
32+
file?: Blob;
33+
}
34+
35+
export function InlineObject1FromJSON(json: any): InlineObject1 {
36+
return {
37+
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
38+
'file': !exists(json, 'file') ? undefined : json['file'],
39+
};
40+
}
41+
42+
export function InlineObject1ToJSON(value?: InlineObject1): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'additionalMetadata': value.additionalMetadata,
48+
'file': value.file,
49+
};
50+
}
51+
52+

samples/client/petstore/typescript-fetch/builds/default/runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");
1616

17+
const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;
18+
1719
/**
1820
* This is the base class for all generated API classes.
1921
*/
@@ -58,7 +60,9 @@ export class BaseAPI {
5860
// do not handle correctly sometimes.
5961
url += '?' + querystring(context.query);
6062
}
61-
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
63+
const body = (context.body instanceof FormData || isBlob(context.body))
64+
? context.body
65+
: JSON.stringify(context.body);
6266
const init = {
6367
method: context.method,
6468
headers: context.headers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject
19+
*/
20+
export interface InlineObject {
21+
/**
22+
* Updated name of the pet
23+
* @type {string}
24+
* @memberof InlineObject
25+
*/
26+
name?: string;
27+
/**
28+
* Updated status of the pet
29+
* @type {string}
30+
* @memberof InlineObject
31+
*/
32+
status?: string;
33+
}
34+
35+
export function InlineObjectFromJSON(json: any): InlineObject {
36+
return {
37+
'name': !exists(json, 'name') ? undefined : json['name'],
38+
'status': !exists(json, 'status') ? undefined : json['status'],
39+
};
40+
}
41+
42+
export function InlineObjectToJSON(value?: InlineObject): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'name': value.name,
48+
'status': value.status,
49+
};
50+
}
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject1
19+
*/
20+
export interface InlineObject1 {
21+
/**
22+
* Additional data to pass to server
23+
* @type {string}
24+
* @memberof InlineObject1
25+
*/
26+
additionalMetadata?: string;
27+
/**
28+
* file to upload
29+
* @type {Blob}
30+
* @memberof InlineObject1
31+
*/
32+
file?: Blob;
33+
}
34+
35+
export function InlineObject1FromJSON(json: any): InlineObject1 {
36+
return {
37+
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
38+
'file': !exists(json, 'file') ? undefined : json['file'],
39+
};
40+
}
41+
42+
export function InlineObject1ToJSON(value?: InlineObject1): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'additionalMetadata': value.additionalMetadata,
48+
'file': value.file,
49+
};
50+
}
51+
52+

samples/client/petstore/typescript-fetch/builds/es6-target/runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");
1616

17+
const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;
18+
1719
/**
1820
* This is the base class for all generated API classes.
1921
*/
@@ -58,7 +60,9 @@ export class BaseAPI {
5860
// do not handle correctly sometimes.
5961
url += '?' + querystring(context.query);
6062
}
61-
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
63+
const body = (context.body instanceof FormData || isBlob(context.body))
64+
? context.body
65+
: JSON.stringify(context.body);
6266
const init = {
6367
method: context.method,
6468
headers: context.headers,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject
19+
*/
20+
export interface InlineObject {
21+
/**
22+
* Updated name of the pet
23+
* @type {string}
24+
* @memberof InlineObject
25+
*/
26+
name?: string;
27+
/**
28+
* Updated status of the pet
29+
* @type {string}
30+
* @memberof InlineObject
31+
*/
32+
status?: string;
33+
}
34+
35+
export function InlineObjectFromJSON(json: any): InlineObject {
36+
return {
37+
'name': !exists(json, 'name') ? undefined : json['name'],
38+
'status': !exists(json, 'status') ? undefined : json['status'],
39+
};
40+
}
41+
42+
export function InlineObjectToJSON(value?: InlineObject): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'name': value.name,
48+
'status': value.status,
49+
};
50+
}
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// tslint:disable
2+
/**
3+
* OpenAPI Petstore
4+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
5+
*
6+
* The version of the OpenAPI document: 1.0.0
7+
*
8+
*
9+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
10+
* https://openapi-generator.tech
11+
* Do not edit the class manually.
12+
*/
13+
14+
import { exists, mapValues } from '../runtime';
15+
/**
16+
*
17+
* @export
18+
* @interface InlineObject1
19+
*/
20+
export interface InlineObject1 {
21+
/**
22+
* Additional data to pass to server
23+
* @type {string}
24+
* @memberof InlineObject1
25+
*/
26+
additionalMetadata?: string;
27+
/**
28+
* file to upload
29+
* @type {Blob}
30+
* @memberof InlineObject1
31+
*/
32+
file?: Blob;
33+
}
34+
35+
export function InlineObject1FromJSON(json: any): InlineObject1 {
36+
return {
37+
'additionalMetadata': !exists(json, 'additionalMetadata') ? undefined : json['additionalMetadata'],
38+
'file': !exists(json, 'file') ? undefined : json['file'],
39+
};
40+
}
41+
42+
export function InlineObject1ToJSON(value?: InlineObject1): any {
43+
if (value === undefined) {
44+
return undefined;
45+
}
46+
return {
47+
'additionalMetadata': value.additionalMetadata,
48+
'file': value.file,
49+
};
50+
}
51+
52+

samples/client/petstore/typescript-fetch/builds/with-interfaces/runtime.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
export const BASE_PATH = "http://petstore.swagger.io/v2".replace(/\/+$/, "");
1616

17+
const isBlob = (value: any) => typeof Blob !== 'undefined' && value instanceof Blob;
18+
1719
/**
1820
* This is the base class for all generated API classes.
1921
*/
@@ -58,7 +60,9 @@ export class BaseAPI {
5860
// do not handle correctly sometimes.
5961
url += '?' + querystring(context.query);
6062
}
61-
const body = context.body instanceof FormData ? context.body : JSON.stringify(context.body);
63+
const body = (context.body instanceof FormData || isBlob(context.body))
64+
? context.body
65+
: JSON.stringify(context.body);
6266
const init = {
6367
method: context.method,
6468
headers: context.headers,

0 commit comments

Comments
 (0)