diff --git a/tests/SwaggerProvider.Tests/Schema.TestHelpers.fs b/tests/SwaggerProvider.Tests/Schema.TestHelpers.fs index 7bcf327..9c3cf0f 100644 --- a/tests/SwaggerProvider.Tests/Schema.TestHelpers.fs +++ b/tests/SwaggerProvider.Tests/Schema.TestHelpers.fs @@ -101,3 +101,7 @@ let compilePropertyTypeWith (provideNullable: bool) (propYaml: string) (required /// Compile a minimal v3 schema where date/time formats map to DateOnly/TimeOnly types. let compilePropertyTypeWithDateOnly (propYaml: string) (required: bool) : Type = compilePropertyTypeWithOptions false true propYaml required + +/// Compile a minimal v3 schema with both PreferNullable and useDateOnly options enabled. +let compilePropertyTypeWithNullableAndDateOnly (propYaml: string) (required: bool) : Type = + compilePropertyTypeWithOptions true true propYaml required diff --git a/tests/SwaggerProvider.Tests/Schema.TypeMappingTests.fs b/tests/SwaggerProvider.Tests/Schema.TypeMappingTests.fs index c979e19..e3c3948 100644 --- a/tests/SwaggerProvider.Tests/Schema.TypeMappingTests.fs +++ b/tests/SwaggerProvider.Tests/Schema.TypeMappingTests.fs @@ -54,11 +54,18 @@ let ``required string date-time format maps to DateTimeOffset``() = ty |> shouldEqual typeof [] -let ``required string date format maps to DateTimeOffset``() = +let ``required string date format maps to DateTimeOffset when useDateOnly is false``() = let ty = compilePropertyType " type: string\n format: date\n" true ty |> shouldEqual typeof +[] +let ``required string date format maps to DateOnly when useDateOnly is true``() = + let ty = + compilePropertyTypeWithDateOnly " type: string\n format: date\n" true + + ty |> shouldEqual typeof + [] let ``required string time format falls back to string when useDateOnly is false``() = // The test helper compiles with useDateOnly=false, so TimeOnly is not used @@ -136,6 +143,22 @@ let ``optional DateTimeOffset maps to Option``() = ty |> shouldEqual(typedefof>.MakeGenericType(typeof)) +[] +let ``optional DateOnly maps to Option when useDateOnly is true``() = + let ty = + compilePropertyTypeWithDateOnly " type: string\n format: date\n" false + + ty + |> shouldEqual(typedefof>.MakeGenericType(typeof)) + +[] +let ``optional TimeOnly maps to Option when useDateOnly is true``() = + let ty = + compilePropertyTypeWithDateOnly " type: string\n format: time\n" false + + ty + |> shouldEqual(typedefof>.MakeGenericType(typeof)) + [] let ``optional Guid maps to Option``() = let ty = @@ -444,3 +467,35 @@ let ``PreferNullable: optional binary (base64) stays as plain Stream``() = compilePropertyTypeWith true " type: string\n format: binary\n" false ty |> shouldEqual typeof + +// ── PreferNullable + useDateOnly: value-type date/time formats use Nullable ────────────── +// When both provideNullable=true and useDateOnly=true, optional DateOnly/TimeOnly properties +// should be wrapped in Nullable (not Option), consistent with other value types. + +[] +let ``PreferNullable: optional DateOnly maps to Nullable when useDateOnly is true``() = + let ty = + compilePropertyTypeWithNullableAndDateOnly " type: string\n format: date\n" false + + ty |> shouldEqual typeof> + +[] +let ``PreferNullable: required DateOnly is not wrapped when useDateOnly is true``() = + let ty = + compilePropertyTypeWithNullableAndDateOnly " type: string\n format: date\n" true + + ty |> shouldEqual typeof + +[] +let ``PreferNullable: optional TimeOnly maps to Nullable when useDateOnly is true``() = + let ty = + compilePropertyTypeWithNullableAndDateOnly " type: string\n format: time\n" false + + ty |> shouldEqual typeof> + +[] +let ``PreferNullable: required TimeOnly is not wrapped when useDateOnly is true``() = + let ty = + compilePropertyTypeWithNullableAndDateOnly " type: string\n format: time\n" true + + ty |> shouldEqual typeof