Skip to content

Commit

Permalink
[BUG][typescript-fetch] wrong response for simple types (#14659)
Browse files Browse the repository at this point in the history
See #9364
See #2870
  • Loading branch information
Serranya authored Mar 15, 2023
1 parent 5eb2819 commit 5c45292
Show file tree
Hide file tree
Showing 25 changed files with 317 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ export class {{classname}} extends runtime.BaseAPI {
return new runtime.JSONApiResponse<any>(response);
{{/isArray}}
{{#returnSimpleType}}
return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<{{returnType}}>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
{{/returnSimpleType}}
{{/returnTypeIsPrimitive}}
{{^returnTypeIsPrimitive}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {
private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];
constructor(protected configuration = DefaultConfig) {
Expand All @@ -102,6 +103,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ export class FakeApi extends runtime.BaseAPI {
body: requestParameters.body as any,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<boolean>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down Expand Up @@ -290,7 +294,11 @@ export class FakeApi extends runtime.BaseAPI {
body: requestParameters.body as any,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<number>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down Expand Up @@ -319,7 +327,11 @@ export class FakeApi extends runtime.BaseAPI {
body: requestParameters.body as any,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,11 @@ export class UserApi extends runtime.BaseAPI {
query: queryParameters,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ export class UserApi extends runtime.BaseAPI {
query: queryParameters,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions samples/client/petstore/typescript-fetch/builds/default/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
18 changes: 18 additions & 0 deletions samples/client/petstore/typescript-fetch/builds/enum/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ export class UserApi extends runtime.BaseAPI {
query: queryParameters,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ export class UserApi extends runtime.BaseAPI {
query: queryParameters,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const DefaultConfig = new Configuration();
*/
export class BaseAPI {

private static readonly jsonRegex = new RegExp('^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$', 'i');
private middleware: Middleware[];

constructor(protected configuration = DefaultConfig) {
Expand All @@ -113,6 +114,23 @@ export class BaseAPI {
return this.withMiddleware<T>(...middlewares);
}

/**
* Check if the given MIME is a JSON MIME.
* JSON MIME examples:
* application/json
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
protected isJsonMime(mime: string | null | undefined): boolean {
if (!mime) {
return false;
}
return BaseAPI.jsonRegex.test(mime);
}

protected async request(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction): Promise<Response> {
const { url, init } = await this.createFetchParams(context, initOverrides);
const response = await this.fetchApi(url, init);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ export class UserApi extends runtime.BaseAPI {
query: queryParameters,
}, initOverrides);

return new runtime.TextApiResponse(response) as any;
if (this.isJsonMime(response.headers.get('content-type'))) {
return new runtime.JSONApiResponse<string>(response);
} else {
return new runtime.TextApiResponse(response) as any;
}
}

/**
Expand Down
Loading

0 comments on commit 5c45292

Please sign in to comment.