-
-
Notifications
You must be signed in to change notification settings - Fork 542
Fix binary response type to process as a Blob rather than text/string #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Why not add support for |
I'm not actually sure of an easy way to determine between returning a The only way I can think of it is an update to the Service class functions to take an options object. This would also mean more complicated TypeScript to determine the response type. The reason I chose to use blobs was:
One place which I used as a reference for my decision is: https://github.com/noahunallar/arraybuffer-vs-blob |
In regards to axios - according to docs (https://github.com/axios/axios#request-config):
So if used in Node.js when you set responseType: "blob", "json" will actually be used, which I guess fallbacks to "text" and might result in corrupted data. |
Ah good spotting, I had tested axios but only on client-side. If running under Node.js I've noticed people opting for streams most of the time when dealing with files. Although I'm not sure how I would know in the case of using axios.
Happy to make changes to the PR, just need some input from the team as to what they would like. |
How will the responseType be determined from the OpenAPI specification? I was not yet able to get it to generate a |
Nvm, just figured it out. I'd really love to see this getting merged! Is there anything I could possibly help with? |
Apologies I didn't have much information in my original PR, I've updated it to better detail how it works. Nothing to do at the moment just waiting for @ferdikoomen to review it and let me know if there is any changes to be made. |
@loganbenjamin Thanks for the additional info, I will review this asap (bit busy right now, but will try to make some time!). The feature looks very nice from your description and a quick look at the changes. |
Just tried this branch with a newer ionic (6.x) / angular (13.2.x) app, and this works perfectly with the exception of an initial build error:
It appears that changing this line in request.ts:
To this:
alleviates the above error, which appears to be coming from an angular/common/http type def conflict. Once the above line was changed, Blob responses started coming back as expected with no changes to my openapi spec. Not sure whether there's a more elegant way to handle this with generics, but hitting that line with the "any" hammer seems to do the trick for now. |
@jospete thanks for checking it out and responding with detailed information, I've made a small change to fix this. |
@loganbenjamin I ran into this same issue, and while I troubleshooting and coming up with a similar solution, I also noticed that the [accept] header is always being set to 'application/json', even though the openapi spec for the file download path is "application/octet-stream". This isn't causing me problems, since my server seems to be ignoring the [accept] header in this case-- but this seems like this should be solved in this same PR. |
{{~#equals results.length 1~}} | ||
{{~#equals results.0.base 'binary'~}} | ||
responseType: 'blob', | ||
{{/equals}} | ||
{{~/equals~}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{{~#equals results.length 1~}} | |
{{~#equals results.0.base 'binary'~}} | |
responseType: 'blob', | |
{{/equals}} | |
{{~/equals~}} | |
{{~#equals results.0.base 'binary'~}} | |
responseType: 'blob', | |
{{/equals}} |
I'd like to suggest to not check on results.length 1 as there might be multiple valid responses, e.g. some 202 not ready response, which is not a blob and thus would be filtered out.
However only checking for the first element seems fine to be, as the 200 success response will be sorted and should always be the first element
In an Angular codegen variant, requests are successfully executed (200 OK) but they resolve failed in the underlying HttpClient code, due to their bodies being blobs. |
Hi @maciejgoscinski, thank you for pointing this out. we are facing the same problem using Angular |
Hi, what is status of this issue? I have the same problem with blob request from api |
Any news on this? |
@bertramr i have the same issue in angular. Do you need more context ? 🙏 |
Hello there, I wanted to share with you a pull request that I think you might find helpful: #1043 Please note that this pull request changes the original design and, as mentioned in the description, it's unlikely to be included in the main repository. However, if you're interested in using it, you can maintain your own version. I hope you find this helpful! |
Hey any updates on when this can be merged? |
Would love to see this too, would be quite nice to get this merged. We have a similar requirement where we get binary data but currently we get the |
Looking forward for this merge too. @ferdikoomen how else can we help to expedite this? |
@ferdikoomen LGTM |
We need it also, would be super helpful to have this merged :) |
Any updates on this ? Thanks! 🙏 |
Hello, any updates? |
@loganbenjamin can you fix the conflicts and can some from the authors review the change? |
It would be so nice to merge this PR. Any reason it is not by now? |
@benny-noumena check out our fork @hey-api/openapi-ts which has added more support for blob responses. It may fix this issue for you, and if not please open and issue there and I will work to fix it. |
Issue
When the OpenAPI response type is a
binary
type the generated TypeScript has a response type ofPromise<Blob>
but the response returned is astring
.E.g. for fetch the code is
return await response.text()
.This mismatches from the expected type defined in the TypeScript
Blob
vs actual typestring
, and can corrupt the data.Solution
Update the code to detect when response type is a
binary
type and return aBlob
type.E.g. for fetch
return await response.blob()
.Changes
ApiRequestOptions
type to include aresponseType
parameter.binary
type and set the requestresponseType = 'blob'
. This is the same way the TypeScript definition is generated currently.Notes/things to review
Blob
response type will be generated.Blob
response type.Stream
and fetch could be aBlob
. It is more difficult to give the user the option as to what response type they would like.