Skip to content

Commit b9823f2

Browse files
github-actions[bot]Copilotsergey-tihonCopilotCopilot
authored
[repo-assist] fix: formatObject correctly handles Option(T) elements in arrays (#475)
* fix: formatObject correctly handles Option<T> elements in arrays When a generated API type has a property of type `Option<DateOnly>[]`, `Option<TimeOnly>[]`, or `Option<string>[]`, the previous `appendFormattedArray` helper fell through to `obj.ToString()` for each element, producing locale-specific output like `Some(07/04/2025)` instead of ISO 8601. Fix: detect when the array element type is an F# option type (pre-computed `isOptionEl` flag) and unwrap each element using the cached tag reader and Value property (same caches used elsewhere in the file). The inner value is then formatted via `appendFormattedValue`, which correctly handles DateOnly/TimeOnly (ISO 8601), strings (quoted), and all other types. Add 6 new tests covering: - `array<Option<DateOnly>>` with Some and None elements - `array<Option<TimeOnly>>` with Some and None elements - `array<Option<string>>` with Some and None elements Total tests: 540 → 547. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix: make appendFormattedArray recursive for nested option arrays * Hoist option cache lookups out of element loop in appendFormattedArray --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Sergey Tihon <sergey.tihon@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 0f2807a commit b9823f2

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎src/SwaggerProvider.Runtime/RuntimeHelpers.fs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,32 @@ module RuntimeHelpers =
378378
let props = getProperties(obj.GetType())
379379
let sb = System.Text.StringBuilder()
380380

381-
let appendFormattedArray(v: obj) =
381+
let rec appendFormattedArray(v: obj) =
382382
let vTy = v.GetType()
383383
sb.Append('[') |> ignore
384384
let mutable firstEl = true
385385
let elTy = vTy.GetElementType()
386386
let isDateOnly = not(isNull elTy) && elTy.FullName = dateOnlyTypeName
387387
let isTimeOnly = not(isNull elTy) && elTy.FullName = timeOnlyTypeName
388+
// Detect array<Option<T>> so elements are correctly unwrapped and formatted.
389+
// Without this, Option<DateOnly>/Option<TimeOnly> elements fall through to
390+
// obj.ToString(), producing locale-specific "Some(07/04/2025)" instead of ISO 8601.
391+
let isOptionEl =
392+
not(isNull elTy)
393+
&& elTy.IsGenericType
394+
&& elTy.GetGenericTypeDefinition() = typedefof<option<_>>
395+
396+
let optionTagReader =
397+
if isOptionEl then
398+
Some(optionTagReaderCache.GetOrAdd(elTy, optionTagReaderFactory))
399+
else
400+
None
401+
402+
let optionValueProp =
403+
if isOptionEl then
404+
Some(optionValueCache.GetOrAdd(elTy, optionValueFactory))
405+
else
406+
None
388407

389408
for x in (v :?> Array) |> Seq.cast<obj> do
390409
if not firstEl then
@@ -398,6 +417,21 @@ module RuntimeHelpers =
398417
sb.Append(formatDateOrTimeValue "yyyy-MM-dd" elTy x) |> ignore
399418
elif isTimeOnly then
400419
sb.Append(formatDateOrTimeValue "HH:mm:ss.FFFFFFF" elTy x) |> ignore
420+
elif isOptionEl then
421+
if optionTagReader.Value x = 1 then // 1 = Some
422+
let inner = optionValueProp.Value.GetValue(x)
423+
424+
if isNull inner then
425+
sb.Append("null") |> ignore
426+
else
427+
let innerTy = inner.GetType()
428+
429+
if innerTy.IsArray then
430+
appendFormattedArray inner
431+
else
432+
appendFormattedValue sb inner innerTy
433+
else
434+
sb.Append("null") |> ignore
401435
else
402436
sb.Append(x.ToString()) |> ignore
403437

‎tests/SwaggerProvider.Tests/RuntimeHelpersTests.fs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,15 @@ type FmtOptionStringArray(tags: string[] option) =
11841184
type FmtOptionDateOnlyArray(dates: DateOnly[] option) =
11851185
member _.Dates = dates
11861186

1187+
type FmtOptionDateOnlyElements(dates: DateOnly option[]) =
1188+
member _.Dates = dates
1189+
1190+
type FmtOptionTimeOnlyElements(times: TimeOnly option[]) =
1191+
member _.Times = times
1192+
1193+
type FmtOptionStringElements(names: string option[]) =
1194+
member _.Names = names
1195+
11871196

11881197
module FormatObjectTests =
11891198

@@ -1310,6 +1319,34 @@ module FormatObjectTests =
13101319

13111320
formatObject obj |> shouldEqual "{Dates=[2025-01-01; 2025-12-31]}"
13121321

1322+
[<Fact>]
1323+
let ``formatObject formats array of Option DateOnly Some elements as ISO 8601``() =
1324+
let obj =
1325+
FmtOptionDateOnlyElements([| Some(DateOnly(2025, 1, 1)); Some(DateOnly(2025, 12, 31)) |])
1326+
1327+
formatObject obj |> shouldEqual "{Dates=[2025-01-01; 2025-12-31]}"
1328+
1329+
[<Fact>]
1330+
let ``formatObject formats array of Option DateOnly with None elements as null``() =
1331+
let obj = FmtOptionDateOnlyElements([| Some(DateOnly(2025, 7, 4)); None |])
1332+
formatObject obj |> shouldEqual "{Dates=[2025-07-04; null]}"
1333+
1334+
[<Fact>]
1335+
let ``formatObject formats array of Option TimeOnly Some elements using HH:mm:ss.FFFFFFF``() =
1336+
let obj =
1337+
FmtOptionTimeOnlyElements([| Some(TimeOnly(14, 30, 0)); Some(TimeOnly(9, 5, 3, 123)) |])
1338+
1339+
formatObject obj |> shouldEqual "{Times=[14:30:00; 09:05:03.123]}"
1340+
1341+
[<Fact>]
1342+
let ``formatObject formats array of Option TimeOnly with None elements as null``() =
1343+
let obj = FmtOptionTimeOnlyElements([| None; Some(TimeOnly(8, 0, 0)) |])
1344+
formatObject obj |> shouldEqual "{Times=[null; 08:00:00]}"
1345+
1346+
[<Fact>]
1347+
let ``formatObject formats array of Option string elements``() =
1348+
let obj = FmtOptionStringElements([| Some "hello"; None; Some "world" |])
1349+
formatObject obj |> shouldEqual "{Names=[\"hello\"; null; \"world\"]}"
13131350

13141351

13151352
[<Fact>]

0 commit comments

Comments
 (0)