Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/SwaggerProvider.DesignTime/Provider.OpenApiClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type public OpenApiClientTypeProvider(cfg: TypeProviderConfig) as this =
|> Seq.map(fun e -> $"%s{e.Message} @ %s{e.Pointer}")
|> String.concat "\n")

let defCompiler = DefinitionCompiler(schema, preferNullable)
let useDateOnly = cfg.SystemRuntimeAssemblyVersion.Major >= 6
let defCompiler = DefinitionCompiler(schema, preferNullable, useDateOnly)

let opCompiler =
OperationCompiler(schema, defCompiler, ignoreControllerPrefix, ignoreOperationId, preferAsync)
Expand Down
3 changes: 2 additions & 1 deletion src/SwaggerProvider.DesignTime/Provider.SwaggerClient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type public SwaggerTypeProvider(cfg: TypeProviderConfig) as this =

let schema = SwaggerParser.parseSchema schemaData

let defCompiler = DefinitionCompiler(schema, preferNullable)
let useDateOnly = cfg.SystemRuntimeAssemblyVersion.Major >= 6
let defCompiler = DefinitionCompiler(schema, preferNullable, useDateOnly)

let opCompiler =
OperationCompiler(schema, defCompiler, ignoreControllerPrefix, ignoreOperationId, preferAsync)
Expand Down
16 changes: 10 additions & 6 deletions src/SwaggerProvider.DesignTime/v2/DefinitionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ and NamespaceAbstraction(name: string) =
Some ty)

/// Object for compiling definitions.
type DefinitionCompiler(schema: SwaggerObject, provideNullable) as this =
type DefinitionCompiler(schema: SwaggerObject, provideNullable, useDateOnly: bool) as this =
let definitionToSchemaObject = Map.ofSeq schema.Definitions
let definitionToType = Collections.Generic.Dictionary<_, _>()
let nsRoot = NamespaceAbstraction("Root")
Expand Down Expand Up @@ -353,11 +353,15 @@ type DefinitionCompiler(schema: SwaggerObject, provideNullable) as this =
| Double -> typeof<double>
| String -> typeof<string>
| Date ->
// Runtime detection: design-time assembly targets netstandard2.0 so
// compile-time NET6_0_OR_GREATER is not available; DateOnly exists on .NET 6+
System.Type.GetType("System.DateOnly")
|> Option.ofObj
|> Option.defaultValue typeof<DateTime>
// Use DateOnly only when the target runtime supports it (.NET 6+).
// We check useDateOnly (derived from cfg.SystemRuntimeAssemblyVersion) rather than
// probing the design-time host process, which may differ from the consumer's runtime.
if useDateOnly then
System.Type.GetType("System.DateOnly")
|> Option.ofObj
|> Option.defaultValue typeof<DateTime>
else
typeof<DateTime>
Comment on lines +356 to +364
| DateTime -> typeof<DateTime>
| File -> typeof<byte>.MakeArrayType 1
| Enum(_, "string") -> typeof<string>
Expand Down
16 changes: 10 additions & 6 deletions src/SwaggerProvider.DesignTime/v3/DefinitionCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ and NamespaceAbstraction(name: string) =
Some ty)

/// Object for compiling definitions.
type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: bool) as this =
let pathToSchema =
if isNull schema.Components then
Map.empty
Expand Down Expand Up @@ -497,11 +497,15 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable) as this =
// for `multipart/form-data` : https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#considerations-for-file-uploads
typeof<IO.Stream>
| HasFlag JsonSchemaType.String, "date" ->
// Runtime detection: design-time assembly targets netstandard2.0 so
// compile-time NET6_0_OR_GREATER is not available; DateOnly exists on .NET 6+
System.Type.GetType("System.DateOnly")
|> Option.ofObj
|> Option.defaultValue typeof<DateTimeOffset>
// Use DateOnly only when the target runtime supports it (.NET 6+).
// We check useDateOnly (derived from cfg.SystemRuntimeAssemblyVersion) rather than
// probing the design-time host process, which may differ from the consumer's runtime.
if useDateOnly then
System.Type.GetType("System.DateOnly")
|> Option.ofObj
|> Option.defaultValue typeof<DateTimeOffset>
else
typeof<DateTimeOffset>
Comment on lines +500 to +508
| HasFlag JsonSchemaType.String, "date-time" -> typeof<DateTimeOffset>
| HasFlag JsonSchemaType.String, "uuid" -> typeof<Guid>
| HasFlag JsonSchemaType.String, _ -> typeof<string>
Expand Down
4 changes: 2 additions & 2 deletions tests/SwaggerProvider.Tests/Schema.Parser.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module V2 =
let testSchema schemaStr =
let schema = SwaggerParser.parseSchema schemaStr

let defCompiler = DefinitionCompiler(schema, false)
let defCompiler = DefinitionCompiler(schema, false, Environment.Version.Major >= 6)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
ignore <| defCompiler.Namespace.GetProvidedTypes()
Expand All @@ -35,7 +35,7 @@ module V3 =
|> Seq.map (fun e -> e.Message)
|> String.concat ";\n- ")*)
try
let defCompiler = DefinitionCompiler(schema, false)
let defCompiler = DefinitionCompiler(schema, false, Environment.Version.Major >= 6)
let opCompiler = OperationCompiler(schema, defCompiler, true, false, true)
opCompiler.CompileProvidedClients(defCompiler.Namespace)
defCompiler.Namespace.GetProvidedTypes()
Expand Down
Loading