diff --git a/modules/openapi-generator/src/main/resources/typescript/http/jquery.mustache b/modules/openapi-generator/src/main/resources/typescript/http/jquery.mustache index d6a25d993ec..3c67c5bd6f1 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/jquery.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/jquery.mustache @@ -21,14 +21,12 @@ export class JQueryHttpLibrary implements HttpLibrary { data: body }; - /** - * Allow receiving binary data with jquery ajax - * - * Source: https://keyangxiang.com/2017/09/01/HTML5-XHR-download-binary-content-as-Blob/ - */ - requestOptions.beforeSend = (jqXHR: any, settings: any) => { - settings.xhr().responseType = "blob"; - }; + // If we want a blob, we have to set the xhrFields' responseType AND add a + // custom converter to overwrite the default deserialization of JQuery... + requestOptions["xhrFields"] = { responseType: 'blob' }; + requestOptions["converters"] = {} + requestOptions["converters"]["* blob"] = (result:any) => result; + requestOptions["dataType"] = "blob"; if (request.getHeaders()['Content-Type']) { @@ -51,6 +49,7 @@ export class JQueryHttpLibrary implements HttpLibrary { if (body && body.constructor.name == "FormData") { requestOptions.contentType = false; } + const sentRequest = $.ajax(requestOptions); const resultPromise = new Promise((resolve, reject) => { diff --git a/samples/openapi3/client/petstore/typescript/builds/jquery/http/jquery.ts b/samples/openapi3/client/petstore/typescript/builds/jquery/http/jquery.ts index 238eef3be08..a0cd53a1996 100644 --- a/samples/openapi3/client/petstore/typescript/builds/jquery/http/jquery.ts +++ b/samples/openapi3/client/petstore/typescript/builds/jquery/http/jquery.ts @@ -21,14 +21,12 @@ export class JQueryHttpLibrary implements HttpLibrary { data: body }; - /** - * Allow receiving binary data with jquery ajax - * - * Source: https://keyangxiang.com/2017/09/01/HTML5-XHR-download-binary-content-as-Blob/ - */ - requestOptions.beforeSend = (jqXHR: any, settings: any) => { - settings.xhr().responseType = "blob"; - }; + // If we want a blob, we have to set the xhrFields' responseType AND add a + // custom converter to overwrite the default deserialization of JQuery... + requestOptions["xhrFields"] = { responseType: 'blob' }; + requestOptions["converters"] = {} + requestOptions["converters"]["* blob"] = (result:any) => result; + requestOptions["dataType"] = "blob"; if (request.getHeaders()['Content-Type']) { @@ -51,6 +49,7 @@ export class JQueryHttpLibrary implements HttpLibrary { if (body && body.constructor.name == "FormData") { requestOptions.contentType = false; } + const sentRequest = $.ajax(requestOptions); const resultPromise = new Promise((resolve, reject) => { diff --git a/samples/openapi3/client/petstore/typescript/tests/jquery/test/http/jquery.test.ts b/samples/openapi3/client/petstore/typescript/tests/jquery/test/http/jquery.test.ts index 007ff42b824..0898ca7f155 100644 --- a/samples/openapi3/client/petstore/typescript/tests/jquery/test/http/jquery.test.ts +++ b/samples/openapi3/client/petstore/typescript/tests/jquery/test/http/jquery.test.ts @@ -17,7 +17,7 @@ for (let libName in libs) { return new Promise((resolve, reject) => { lib.send(requestContext).toPromise().then((resp: petstore.ResponseContext) => { assert.ok(resp.httpStatusCode, 200, "Expected status code to be 200"); - return resp.body.text() + return resp.body.text(); }).then((txtBody: string) => { let body = JSON.parse(txtBody); assert.ok(body["headers"]); @@ -46,7 +46,7 @@ for (let libName in libs) { lib.send(requestContext).toPromise().then( (resp: petstore.ResponseContext) => { assert.ok(resp.httpStatusCode, 200, "Expected status code to be 200"); - return resp.body.text() + return resp.body.text(); }).then((txtBody: any) => { let body = JSON.parse(txtBody); assert.ok(body["headers"]);