Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ type OperationCompiler(schema: OpenApiDocument, defCompiler: DefinitionCompiler,
<@
let stream: obj = %%streamObj
let msg = %httpRequestMessage
msg.Content <- RuntimeHelpers.toStreamContent(stream)
msg.Content <- RuntimeHelpers.toStreamContent(stream, payloadMime)
msg
@>
| Some(MultipartFormData, formData) ->
Expand Down
11 changes: 9 additions & 2 deletions src/SwaggerProvider.Runtime/RuntimeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Swagger.Internal

open System
open System.Net.Http
open System.Net.Http.Headers
open System.Text.Json.Serialization
open System.Threading.Tasks

Expand Down Expand Up @@ -133,9 +134,15 @@ module RuntimeHelpers =
let toTextContent(valueStr: string) =
new StringContent(valueStr, Text.Encoding.UTF8, "text/plain")

let toStreamContent(boxedStream: obj) =
let toStreamContent(boxedStream: obj, contentType: string) =
match boxedStream with
| :? IO.Stream as stream -> new StreamContent(stream)
| :? IO.Stream as stream ->
let content = new StreamContent(stream)

if (not <| String.IsNullOrEmpty(contentType)) then
content.Headers.ContentType <- MediaTypeHeaderValue(contentType)

content
| _ -> failwith $"Unexpected parameter type {boxedStream.GetType().Name} instead of IO.Stream"

let getPropertyValues(object: obj) =
Expand Down
10 changes: 7 additions & 3 deletions tests/Swashbuckle.WebApi.Server/Controllers/FileController.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ type ReturnFileController() =
[<HttpPost("stream"); BinaryContent>]
member this.GetFileLength() =
task {
use reader = new StreamReader(this.Request.Body)
let! content = reader.ReadToEndAsync()
return content.Length

if this.Request.ContentType <> "application/octet-stream" then
return ActionResult<int>(UnsupportedMediaTypeResult())
else
use reader = new StreamReader(this.Request.Body)
let! content = reader.ReadToEndAsync()
return ActionResult<int>(this.Ok(content.Length))
}

[<HttpPost("single"); Produces(MediaTypes.ApplicationOctetStream, Type = typeof<FileResult>)>]
Expand Down
Loading