Skip to content

Commit cf3d13e

Browse files
authored
Fix DateOnly: check target runtime version instead of design-time host (#323)
1 parent 9f6f5cf commit cf3d13e

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ type public OpenApiClientTypeProvider(cfg: TypeProviderConfig) as this =
103103
|> Seq.map(fun e -> $"%s{e.Message} @ %s{e.Pointer}")
104104
|> String.concat "\n")
105105

106-
let defCompiler = DefinitionCompiler(schema, preferNullable)
106+
let useDateOnly = cfg.SystemRuntimeAssemblyVersion.Major >= 6
107+
let defCompiler = DefinitionCompiler(schema, preferNullable, useDateOnly)
107108

108109
let opCompiler =
109110
OperationCompiler(schema, defCompiler, ignoreControllerPrefix, ignoreOperationId, preferAsync)

src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ type public SwaggerTypeProvider(cfg: TypeProviderConfig) as this =
8686

8787
let schema = SwaggerParser.parseSchema schemaData
8888

89-
let defCompiler = DefinitionCompiler(schema, preferNullable)
89+
let useDateOnly = cfg.SystemRuntimeAssemblyVersion.Major >= 6
90+
let defCompiler = DefinitionCompiler(schema, preferNullable, useDateOnly)
9091

9192
let opCompiler =
9293
OperationCompiler(schema, defCompiler, ignoreControllerPrefix, ignoreOperationId, preferAsync)

src/SwaggerProvider.DesignTime/v2/DefinitionCompiler.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ and NamespaceAbstraction(name: string) =
150150
Some ty)
151151

152152
/// Object for compiling definitions.
153-
type DefinitionCompiler(schema: SwaggerObject, provideNullable) as this =
153+
type DefinitionCompiler(schema: SwaggerObject, provideNullable, useDateOnly: bool) as this =
154154
let definitionToSchemaObject = Map.ofSeq schema.Definitions
155155
let definitionToType = Collections.Generic.Dictionary<_, _>()
156156
let nsRoot = NamespaceAbstraction("Root")
@@ -353,11 +353,15 @@ type DefinitionCompiler(schema: SwaggerObject, provideNullable) as this =
353353
| Double -> typeof<double>
354354
| String -> typeof<string>
355355
| Date ->
356-
// Runtime detection: design-time assembly targets netstandard2.0 so
357-
// compile-time NET6_0_OR_GREATER is not available; DateOnly exists on .NET 6+
358-
System.Type.GetType("System.DateOnly")
359-
|> Option.ofObj
360-
|> Option.defaultValue typeof<DateTime>
356+
// Use DateOnly only when the target runtime supports it (.NET 6+).
357+
// We check useDateOnly (derived from cfg.SystemRuntimeAssemblyVersion) rather than
358+
// probing the design-time host process, which may differ from the consumer's runtime.
359+
if useDateOnly then
360+
System.Type.GetType("System.DateOnly")
361+
|> Option.ofObj
362+
|> Option.defaultValue typeof<DateTime>
363+
else
364+
typeof<DateTime>
361365
| DateTime -> typeof<DateTime>
362366
| File -> typeof<byte>.MakeArrayType 1
363367
| Enum(_, "string") -> typeof<string>

src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ and NamespaceAbstraction(name: string) =
165165
Some ty)
166166

167167
/// Object for compiling definitions.
168-
type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
168+
type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: bool) as this =
169169
let pathToSchema =
170170
if isNull schema.Components then
171171
Map.empty
@@ -497,11 +497,15 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
497497
// for `multipart/form-data` : https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#considerations-for-file-uploads
498498
typeof<IO.Stream>
499499
| HasFlag JsonSchemaType.String, "date" ->
500-
// Runtime detection: design-time assembly targets netstandard2.0 so
501-
// compile-time NET6_0_OR_GREATER is not available; DateOnly exists on .NET 6+
502-
System.Type.GetType("System.DateOnly")
503-
|> Option.ofObj
504-
|> Option.defaultValue typeof<DateTimeOffset>
500+
// Use DateOnly only when the target runtime supports it (.NET 6+).
501+
// We check useDateOnly (derived from cfg.SystemRuntimeAssemblyVersion) rather than
502+
// probing the design-time host process, which may differ from the consumer's runtime.
503+
if useDateOnly then
504+
System.Type.GetType("System.DateOnly")
505+
|> Option.ofObj
506+
|> Option.defaultValue typeof<DateTimeOffset>
507+
else
508+
typeof<DateTimeOffset>
505509
| HasFlag JsonSchemaType.String, "date-time" -> typeof<DateTimeOffset>
506510
| HasFlag JsonSchemaType.String, "uuid" -> typeof<Guid>
507511
| HasFlag JsonSchemaType.String, _ -> typeof<string>

tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module V2 =
1313
let testSchema schemaStr =
1414
let schema = SwaggerParser.parseSchema schemaStr
1515

16-
let defCompiler = DefinitionCompiler(schema, false)
16+
let defCompiler = DefinitionCompiler(schema, false, Environment.Version.Major >= 6)
1717
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
1818
opCompiler.CompileProvidedClients(defCompiler.Namespace)
1919
ignore <| defCompiler.Namespace.GetProvidedTypes()
@@ -35,7 +35,7 @@ module V3 =
3535
|> Seq.map (fun e -> e.Message)
3636
|> String.concat ";\n- ")*)
3737
try
38-
let defCompiler = DefinitionCompiler(schema, false)
38+
let defCompiler = DefinitionCompiler(schema, false, Environment.Version.Major >= 6)
3939
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
4040
opCompiler.CompileProvidedClients(defCompiler.Namespace)
4141
defCompiler.Namespace.GetProvidedTypes()

0 commit comments

Comments
 (0)