NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

FileParameter tries to do JsonConvert.SerializeObject on the Stream

Open erwinkramer opened this issue 3 years ago • 6 comments

When using a generated client for an API where the input type is a stream, this generates a FileParameter body input parameter in NSwag. This looks good.

When you look at the generated code by NSwag, it tries to put the stream into a Json Serialized Object, this shouldn't happen. It's not Json! It's a stream where the type is application/octet-stream, so there is no point in serializing. It should put the body.Data (the actual steam). directly on the http request stream instead, no reading at all.

var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(body, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/octet-stream");
                    request_.Content = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");
                    request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

The latest version where it did work seems to be NSwag.ApiDescription.Client 13.7.0, however, it used a regular IO Stream instead of a FileParameter.

erwinkramer avatar Mar 08 '21 17:03 erwinkramer

I'm also having this issue

smeegoan avatar Apr 14 '21 14:04 smeegoan

Same here. As result of Json serialization receive Timeouts are not supported on this stream exception.

BlackGad avatar Oct 04 '21 10:10 BlackGad

@RicoSuter Any chance that you will pay attention to this issue in near future?

BlackGad avatar Oct 04 '21 11:10 BlackGad

I'm still encountering this issue in version 14.0.3. @erwinkramer did you find a workaround for this problem?

rbnpst avatar Feb 07 '24 14:02 rbnpst

@rbnpst i'm not working with that tech right now, but my workaround at the time was going back to version 13.7. If you would ask me what i would do if i encounter it today, i'd try out other tooling, like Kiota from Microsoft. Let me know when u try it.

erwinkramer avatar Feb 07 '24 14:02 erwinkramer

13.7 works for me. Not sure if we want to use an older version for long, but it's good for now. I'll take a look at Kiota. Thanks!

rbnpst avatar Feb 08 '24 10:02 rbnpst