diff --git a/src/SwaggerProvider.DesignTime/DefinitionCompiler.fs b/src/SwaggerProvider.DesignTime/DefinitionCompiler.fs index dff29207..c504f490 100644 --- a/src/SwaggerProvider.DesignTime/DefinitionCompiler.fs +++ b/src/SwaggerProvider.DesignTime/DefinitionCompiler.fs @@ -185,6 +185,27 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b // when compiling large schemas with many object types. let objToStringMethod = typeof.GetMethod("ToString", [||]) + // Resolve DateOnly/TimeOnly lazily and cache the result per compiler instance. + // This preserves the "only pay when needed" behavior while still avoiding + // repeated reflection lookups for schemas that actually use date/time formats. + let dateOnlyTy = + lazy + (if useDateOnly then + System.Type.GetType("System.DateOnly") + |> Option.ofObj + |> Option.defaultValue typeof + else + typeof) + + let timeOnlyTy = + lazy + (if useDateOnly then + System.Type.GetType("System.TimeOnly") + |> Option.ofObj + |> Option.defaultValue typeof + else + typeof) + let generateProperty (scope: UniqueNameGenerator) propName ty = let propertyName = scope.MakeUnique <| nicePascalName propName @@ -627,26 +648,9 @@ type DefinitionCompiler(schema: OpenApiDocument, provideNullable, useDateOnly: b // for `application/octet-stream` request body // for `multipart/form-data` : https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#considerations-for-file-uploads typeof - | HasFlag JsonSchemaType.String, "date" -> - // 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 - else - typeof + | HasFlag JsonSchemaType.String, "date" -> dateOnlyTy.Value | HasFlag JsonSchemaType.String, "date-time" -> typeof - | HasFlag JsonSchemaType.String, "time" -> - // Use TimeOnly only when the target runtime supports it (.NET 6+). - // useDateOnly is true for net6+ targets — TimeOnly was added in the same release. - if useDateOnly then - System.Type.GetType("System.TimeOnly") - |> Option.ofObj - |> Option.defaultValue typeof - else - typeof + | HasFlag JsonSchemaType.String, "time" -> timeOnlyTy.Value | HasFlag JsonSchemaType.String, "uuid" -> typeof | HasFlag JsonSchemaType.String, _ -> typeof | HasFlag JsonSchemaType.Array, _ ->