Skip to content

Commit 9c46b0a

Browse files
Make null handling explicit in toQueryParams and fix misleading test comment (#307)
* Initial plan * Add explicit null guard in toQueryParams and fix test comment Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com> * Apply Fantomas formatting to RuntimeHelpers.fs Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sergey-tihon <1197905+sergey-tihon@users.noreply.github.com>
1 parent 5aecbda commit 9c46b0a

2 files changed

Lines changed: 38 additions & 34 deletions

File tree

src/SwaggerProvider.Runtime/RuntimeHelpers.fs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,38 +85,42 @@ module RuntimeHelpers =
8585
| _ -> obj.ToString()
8686

8787
let toQueryParams (name: string) (obj: obj) (client: Swagger.ProvidedApiClientBase) =
88-
match obj with
89-
| :? array<byte> as xs -> [ name, (client.Serialize xs).Trim('\"') ] // TODO: Need to verify how servers parse byte[] from query string
90-
| :? array<bool> as xs -> xs |> toStrArray name
91-
| :? array<int32> as xs -> xs |> toStrArray name
92-
| :? array<int64> as xs -> xs |> toStrArray name
93-
| :? array<float32> as xs -> xs |> toStrArray name
94-
| :? array<double> as xs -> xs |> toStrArray name
95-
| :? array<string> as xs -> xs |> toStrArray name
96-
| :? array<DateTime> as xs -> xs |> toStrArrayDateTime name
97-
| :? array<DateTimeOffset> as xs -> xs |> toStrArrayDateTimeOffset name
98-
| :? array<Guid> as xs -> xs |> toStrArray name
99-
| :? array<Option<bool>> as xs -> xs |> toStrArrayOpt name
100-
| :? array<Option<int32>> as xs -> xs |> toStrArrayOpt name
101-
| :? array<Option<int64>> as xs -> xs |> toStrArrayOpt name
102-
| :? array<Option<float32>> as xs -> xs |> toStrArrayOpt name
103-
| :? array<Option<double>> as xs -> xs |> toStrArrayOpt name
104-
| :? array<Option<string>> as xs -> xs |> toStrArrayOpt name
105-
| :? array<Option<DateTime>> as xs -> xs |> toStrArrayDateTimeOpt name
106-
| :? array<Option<DateTimeOffset>> as xs -> xs |> toStrArrayDateTimeOffsetOpt name
107-
| :? array<Option<Guid>> as xs -> xs |> toStrArray name
108-
| :? Option<bool> as x -> x |> toStrOpt name
109-
| :? Option<int32> as x -> x |> toStrOpt name
110-
| :? Option<int64> as x -> x |> toStrOpt name
111-
| :? Option<float32> as x -> x |> toStrOpt name
112-
| :? Option<double> as x -> x |> toStrOpt name
113-
| :? Option<string> as x -> x |> toStrOpt name
114-
| :? Option<DateTime> as x -> x |> toStrDateTimeOpt name
115-
| :? Option<DateTimeOffset> as x -> x |> toStrDateTimeOffsetOpt name
116-
| :? DateTime as x -> [ name, x.ToString("O") ]
117-
| :? DateTimeOffset as x -> [ name, x.ToString("O") ]
118-
| :? Option<Guid> as x -> x |> toStrOpt name
119-
| _ -> [ name, obj.ToString() ]
88+
if isNull obj then
89+
[]
90+
else
91+
92+
match obj with
93+
| :? array<byte> as xs -> [ name, (client.Serialize xs).Trim('\"') ] // TODO: Need to verify how servers parse byte[] from query string
94+
| :? array<bool> as xs -> xs |> toStrArray name
95+
| :? array<int32> as xs -> xs |> toStrArray name
96+
| :? array<int64> as xs -> xs |> toStrArray name
97+
| :? array<float32> as xs -> xs |> toStrArray name
98+
| :? array<double> as xs -> xs |> toStrArray name
99+
| :? array<string> as xs -> xs |> toStrArray name
100+
| :? array<DateTime> as xs -> xs |> toStrArrayDateTime name
101+
| :? array<DateTimeOffset> as xs -> xs |> toStrArrayDateTimeOffset name
102+
| :? array<Guid> as xs -> xs |> toStrArray name
103+
| :? array<Option<bool>> as xs -> xs |> toStrArrayOpt name
104+
| :? array<Option<int32>> as xs -> xs |> toStrArrayOpt name
105+
| :? array<Option<int64>> as xs -> xs |> toStrArrayOpt name
106+
| :? array<Option<float32>> as xs -> xs |> toStrArrayOpt name
107+
| :? array<Option<double>> as xs -> xs |> toStrArrayOpt name
108+
| :? array<Option<string>> as xs -> xs |> toStrArrayOpt name
109+
| :? array<Option<DateTime>> as xs -> xs |> toStrArrayDateTimeOpt name
110+
| :? array<Option<DateTimeOffset>> as xs -> xs |> toStrArrayDateTimeOffsetOpt name
111+
| :? array<Option<Guid>> as xs -> xs |> toStrArray name
112+
| :? Option<bool> as x -> x |> toStrOpt name
113+
| :? Option<int32> as x -> x |> toStrOpt name
114+
| :? Option<int64> as x -> x |> toStrOpt name
115+
| :? Option<float32> as x -> x |> toStrOpt name
116+
| :? Option<double> as x -> x |> toStrOpt name
117+
| :? Option<string> as x -> x |> toStrOpt name
118+
| :? Option<DateTime> as x -> x |> toStrDateTimeOpt name
119+
| :? Option<DateTimeOffset> as x -> x |> toStrDateTimeOffsetOpt name
120+
| :? DateTime as x -> [ name, x.ToString("O") ]
121+
| :? DateTimeOffset as x -> [ name, x.ToString("O") ]
122+
| :? Option<Guid> as x -> x |> toStrOpt name
123+
| _ -> [ name, obj.ToString() ]
120124

121125
let getPropertyNameAttribute name =
122126
{ new Reflection.CustomAttributeData() with

tests/SwaggerProvider.Tests/RuntimeHelpersTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ module ToQueryParamsTests =
152152

153153
[<Fact>]
154154
let ``toQueryParams returns empty list for null input (treated as Option None)``() =
155-
// In F#, None for reference option types is compiled as null at the .NET level,
156-
// so a null obj matches Option<string> None and returns an empty list.
155+
// In F#, None for any option type is compiled as null at the .NET level,
156+
// so a null obj is treated as Option None and returns an empty list.
157157
let result = toQueryParams "q" null stubClient
158158
result |> shouldEqual []
159159

0 commit comments

Comments
 (0)