Skip to content

Commit 43c1017

Browse files
authored
Specify a Content-Type header on stream type requests when th epayload type is known (#292)
* Specify a content type header when creating a stream content * Extra unit test for the request content type being specified
1 parent d9e71a0 commit 43c1017

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ type OperationCompiler(schema: OpenApiDocument, defCompiler: DefinitionCompiler,
358358
<@
359359
let stream: obj = %%streamObj
360360
let msg = %httpRequestMessage
361-
msg.Content <- RuntimeHelpers.toStreamContent(stream)
361+
msg.Content <- RuntimeHelpers.toStreamContent(stream, payloadMime)
362362
msg
363363
@>
364364
| Some(MultipartFormData, formData) ->

‎src/SwaggerProvider.Runtime/RuntimeHelpers.fs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Swagger.Internal
22

33
open System
44
open System.Net.Http
5+
open System.Net.Http.Headers
56
open System.Text.Json.Serialization
67
open System.Threading.Tasks
78

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

136-
let toStreamContent(boxedStream: obj) =
137+
let toStreamContent(boxedStream: obj, contentType: string) =
137138
match boxedStream with
138-
| :? IO.Stream as stream -> new StreamContent(stream)
139+
| :? IO.Stream as stream ->
140+
let content = new StreamContent(stream)
141+
142+
if (not <| String.IsNullOrEmpty(contentType)) then
143+
content.Headers.ContentType <- MediaTypeHeaderValue(contentType)
144+
145+
content
139146
| _ -> failwith $"Unexpected parameter type {boxedStream.GetType().Name} instead of IO.Stream"
140147

141148
let getPropertyValues(object: obj) =

‎tests/Swashbuckle.WebApi.Server/Controllers/FileController.fs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ type ReturnFileController() =
5252
[<HttpPost("stream"); BinaryContent>]
5353
member this.GetFileLength() =
5454
task {
55-
use reader = new StreamReader(this.Request.Body)
56-
let! content = reader.ReadToEndAsync()
57-
return content.Length
55+
56+
if this.Request.ContentType <> "application/octet-stream" then
57+
return ActionResult<int>(UnsupportedMediaTypeResult())
58+
else
59+
use reader = new StreamReader(this.Request.Body)
60+
let! content = reader.ReadToEndAsync()
61+
return ActionResult<int>(this.Ok(content.Length))
5862
}
5963

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

0 commit comments

Comments
 (0)