Skip to content

Commit

Permalink
Fix for null refs in #126
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-tihon committed Oct 30, 2019
1 parent db98a8d commit f837170
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 41 deletions.
25 changes: 5 additions & 20 deletions src/SwaggerProvider.DesignTime/v2/OperationCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type OperationCompiler (schema:SwaggerObject, defCompiler:DefinitionCompiler, ig
// Makes argument a string // TODO: Make body an exception
let coerceString defType (format : CollectionFormat) exp =
let obj = Expr.Coerce(exp, typeof<obj>) |> Expr.Cast<obj>
<@ (%obj).ToString() @>
<@ let x = (%obj)
if isNull x then null else x.ToString() @>

let rec coerceQueryString name expr =
let obj = Expr.Coerce(expr, typeof<obj>)
Expand Down Expand Up @@ -155,31 +156,15 @@ type OperationCompiler (schema:SwaggerObject, defCompiler:DefinitionCompiler, ig

let innerReturnType = defaultArg retTy null
let httpRequestMessage =
<@
let requestUrl =
let fakeHost = "http://fake-host/"
let uri = RuntimeHelpers.combineUrl fakeHost %address
let uriB = UriBuilder uri
let newQueries =
%queries
|> Seq.map (fun (name, value) ->
String.Format("{0}={1}", Uri.EscapeDataString name, Uri.EscapeDataString value))
|> String.concat "&"
if String.IsNullOrEmpty uriB.Query
then uriB.Query <- newQueries
else uriB.Query <- String.Format("{0}&{1}", uriB.Query, newQueries)
uriB.Uri.ToString().Substring(fakeHost.Length)
let method = HttpMethod(httpMethod)
new HttpRequestMessage(method, Uri(requestUrl, UriKind.Relative))
@>
<@ RuntimeHelpers.createHttpRequest httpMethod %address %queries @>

let httpRequestMessageWithPayload =
match payload with
| None -> httpRequestMessage
| Some (FormData, b) ->
<@ let keyValues = (%%b: seq<string*string>) |> Seq.map KeyValuePair
<@ let content = RuntimeHelpers.toFormUrlEncodedContent (%%b: seq<string*string>)
let msg = %httpRequestMessage
msg.Content <- new FormUrlEncodedContent(keyValues)
msg.Content <- content
msg @>
| Some (Body, b) ->
<@ let valueStr = (%this).Serialize(%%b: obj)
Expand Down
23 changes: 5 additions & 18 deletions src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ type OperationCompiler (schema:OpenApiDocument, defCompiler:DefinitionCompiler,
// Makes argument a string // TODO: Make body an exception
let coerceString exp =
let obj = Expr.Coerce(exp, typeof<obj>) |> Expr.Cast<obj>
<@ (%obj).ToString() @>
<@ let x = (%obj)
if isNull x then null else x.ToString() @>

let rec coerceQueryString name expr =
let obj = Expr.Coerce(expr, typeof<obj>)
Expand Down Expand Up @@ -249,24 +250,10 @@ type OperationCompiler (schema:OpenApiDocument, defCompiler:DefinitionCompiler,


let httpRequestMessage =
<@
let requestUrl =
let fakeHost = "http://fake-host/"
let uri = RuntimeHelpers.combineUrl fakeHost %path
let uriB = UriBuilder uri
let newQueries =
%queryParams
|> Seq.map (fun (name, value) ->
String.Format("{0}={1}", Uri.EscapeDataString name, Uri.EscapeDataString value))
|> String.concat "&"
if String.IsNullOrEmpty uriB.Query
then uriB.Query <- newQueries
else uriB.Query <- String.Format("{0}&{1}", uriB.Query, newQueries)
uriB.Uri.ToString().Substring(fakeHost.Length)
let method = HttpMethod(httpMethod)
let msg = new HttpRequestMessage(method, Uri(requestUrl, UriKind.Relative))
<@
let msg = RuntimeHelpers.createHttpRequest httpMethod %path %queryParams
RuntimeHelpers.fillHeaders msg %headers
msg
msg
@>

let httpRequestMessageWithPayload =
Expand Down
25 changes: 23 additions & 2 deletions src/SwaggerProvider.Runtime/RuntimeHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ module RuntimeHelpers =
let toMultipartFormDataContent (keyValues:seq<string*string>) =
let cnt = new MultipartFormDataContent()
for (k,v) in keyValues do
cnt.Add(toStringContent v, k)
if not<| isNull v
then cnt.Add(toStringContent v, k)
cnt
let toFormUrlEncodedContent (keyValues:seq<string*string>) =
let keyValues = keyValues |> Seq.map Collections.Generic.KeyValuePair
let keyValues =
keyValues
|> Seq.filter (snd >> isNull >> not)
|> Seq.map Collections.Generic.KeyValuePair
new FormUrlEncodedContent(keyValues)

let getDefaultHttpClient host =
Expand All @@ -84,6 +88,23 @@ module RuntimeHelpers =
let combineUrl (urlA:string) (urlB:string) =
sprintf "%s/%s" (urlA.TrimEnd('/')) (urlB.TrimStart('/'))

let createHttpRequest httpMethod address queryParams =
let requestUrl =
let fakeHost = "http://fake-host/"
let uri = combineUrl fakeHost address
let uriB = UriBuilder uri
let newQueries =
queryParams
|> Seq.map (fun (name, value) ->
String.Format("{0}={1}", Uri.EscapeDataString name, Uri.EscapeDataString value))
|> String.concat "&"
if String.IsNullOrEmpty uriB.Query
then uriB.Query <- newQueries
else uriB.Query <- String.Format("{0}&{1}", uriB.Query, newQueries)
uriB.Uri.ToString().Substring(fakeHost.Length)
let method = HttpMethod(httpMethod)
new HttpRequestMessage(method, Uri(requestUrl, UriKind.Relative))

let fillHeaders (msg:HttpRequestMessage) (heads:(string*string) seq) =
for (name, value) in heads do
if not <| msg.Headers.TryAddWithoutValidation(name, value) then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let [<Literal>] Schema = __SOURCE_DIRECTORY__ + "/../Schemas/v2/slack.json"
let [<Literal>] Host = "https://slack.com"
type Slack = SwaggerClientProvider<Schema, PreferAsync = true>

//[<Tests>]
[<Tests>]
let slackTests =
let slack = Slack.Client()
slack.HttpClient.BaseAddress <- Uri Host
Expand Down

0 comments on commit f837170

Please sign in to comment.