Skip to content

Fix nullable IParsable type recognition in source generator and analyzer#5354

Merged
thomhurst merged 3 commits intomainfrom
copilot/fix-nullable-iparsable-issue
Apr 3, 2026
Merged

Fix nullable IParsable type recognition in source generator and analyzer#5354
thomhurst merged 3 commits intomainfrom
copilot/fix-nullable-iparsable-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 3, 2026

Nullable<T> where T : IParsable<T> (e.g., DateTime?) was not recognized as parsable, causing TUnit0001 errors when passing string arguments to nullable parsable parameters.

// This failed with: TUnit0001: Attribute argument types 'string' don't match method parameter types 'System.DateTime?'
[Test]
[Arguments("2026/04/03 15:45")]
[Arguments(null)]
public void ParsesDateAndTime(DateTime? expectedDateTime) { }

Changes

  • ParsableTypeExtensions.IsParsableFromString — Unwrap Nullable<T> before checking for IParsable<T> interface. This file is shared (via linked compile) between the source generator and analyzer, fixing both.
  • TypedConstantFormatter.FormatPrimitiveForCode — Use the underlying type for Parse() code generation when target is nullable (generates DateTime.Parse(...) not DateTime?.Parse(...)).
  • StringToParsableArgumentsTests — Added test cases for DateTime?, TimeSpan?, Guid? with both string and null arguments.
  • DataDrivenTestArgumentsAnalyzerTests — Added analyzer test verifying nullable parsable types accept string arguments without diagnostics.

Note: The runtime CastHelper.Cast<T> already handled nullable unwrapping correctly via Nullable.GetUnderlyingType(), so no runtime changes were needed.

Copilot AI linked an issue Apr 3, 2026 that may be closed by this pull request
1 task
@codacy-production
Copy link
Copy Markdown

codacy-production bot commented Apr 3, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

Copilot AI and others added 2 commits April 3, 2026 11:00
- Unwrap nullable value types (e.g., DateTime? -> DateTime) in IsParsableFromString
- Use underlying type for Parse() call in TypedConstantFormatter for nullable targets
- Add test cases for nullable DateTime?, TimeSpan?, Guid? with string arguments
- Add source generator snapshot test for nullable parsable arguments

Agent-Logs-Url: https://github.com/thomhurst/TUnit/sessions/18bddde7-3da0-4a0c-8fd4-6221aaea00bc

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
@thomhurst thomhurst marked this pull request as ready for review April 3, 2026 11:11
Copilot AI temporarily deployed to Pull Requests April 3, 2026 11:11 Inactive
Copilot AI temporarily deployed to Pull Requests April 3, 2026 11:11 Inactive
Copilot AI temporarily deployed to Pull Requests April 3, 2026 11:11 Inactive
Copilot AI changed the title [WIP] Fix nullable IParsable issue with TUnit compilation Fix nullable IParsable type recognition in source generator and analyzer Apr 3, 2026
Copilot AI requested a review from thomhurst April 3, 2026 11:14
Copy link
Copy Markdown
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR correctly identifies and fixes a real bug: nullable parsable value types (e.g., DateTime?) were incorrectly flagged by the analyzer and not properly handled in source generation when a string argument is provided. The fix is logically sound and the tests confirm the correct behavior. A few observations follow.

Code Duplication: Existing Extension Methods Not Reused

ParsableTypeExtensions.cs (new code):

if (type is INamedTypeSymbol { IsGenericType: true, ConstructedFrom.SpecialType: SpecialType.System_Nullable_T } nullableType)
{
    type = nullableType.TypeArguments[0];
}

TypedConstantFormatter.cs (new code, FormatPrimitiveForCode):

if (targetType is INamedTypeSymbol { IsGenericType: true, ConstructedFrom.SpecialType: SpecialType.System_Nullable_T } nullableType)
{
    parseType = nullableType.TypeArguments[0];
}

Both of these inline the nullable unwrapping pattern manually. However, TypeSymbolExtensions (TUnit.Core.SourceGenerator.Extensions) already provides IsNullableValueType() and GetNullableUnderlyingType() for exactly this purpose, and TypedConstantFormatter.cs itself already uses these methods in the same file (lines 18–20, 248–253 for the enum handling). The new FormatPrimitiveForCode block should use these same extensions for consistency:

// Suggested: use existing extension methods
if (targetType.IsNullableValueType())
{
    parseType = targetType.GetNullableUnderlyingType()!;
}

The namespace difference (TUnit.Analyzers.Extensions vs TUnit.Core.SourceGenerator.Extensions) means ParsableTypeExtensions may not have access to TypeSymbolExtensions without a using directive, but TypedConstantFormatter.cs definitely can and should reuse them.

Missing Source Generator Snapshot Test

The PR adds integration tests in TUnit.TestProject/StringToParsableArgumentsTests.cs and an analyzer test in DataDrivenTestArgumentsAnalyzerTests.cs, but does not add a source generator snapshot test (.verified.txt) to validate the generated code for nullable parsable parameters.

Given the project's CLAUDE.md rules ("Changes to source generator output or public APIs require running snapshot tests"), a test case exercising DateTime? (and ideally TimeSpan?, Guid?) with [Arguments("...")] should be added to TUnit.Core.SourceGenerator.Tests/ alongside a .verified.txt snapshot. This would explicitly document and protect the generated global::System.DateTime.Parse(...) call.

Hardcoded Future Date in Analyzer Test

[Arguments("2026/04/03 15:45")]

This is today's date at time of authoring, but it will become a past date and the slash-separated format (2026/04/03) may not parse successfully with CultureInfo.InvariantCulture for DateTime.Parse. The existing test cases in StringToParsableArgumentsTests.cs use ISO-8601 format ("2022-05-31") which is safer and more standard. The analyzer test format should be consistent with the runtime test cases.

Correctness: Reflection Engine Already Handles This

Worth noting: the runtime CastHelper.Cast() in TUnit.Core/Helpers/CastHelper.cs (line 72) already unwraps nullable via Nullable.GetUnderlyingType(type) ?? type before calling TryParseFromString. So the reflection-mode engine was already working for this case at runtime — the bug was purely in the source generator mode (analyzer false positive + incorrect generated code). The PR description and test coverage appropriately focus there.

Summary

Item Status
Core bug fix logic Correct
Analyzer fix (IsParsableFromString) Correct, but could reuse existing extension
Source generator fix (TypedConstantFormatter) Correct, but duplicates pattern instead of reusing existing extensions
Integration tests (TestProject) Good coverage (DateTime?, TimeSpan?, Guid?)
Analyzer test Present, minor date format concern
Source generator snapshot tests Missing

The fix is functionally correct. The main actionable items are: (1) reuse IsNullableValueType()/GetNullableUnderlyingType() in TypedConstantFormatter.cs for consistency with the rest of that file, and (2) add a source generator snapshot test per the project's snapshot testing requirements.

github-actions bot pushed a commit to IntelliTect/CodingGuidelines that referenced this pull request Apr 6, 2026
Updated [TUnit.Core](https://github.com/thomhurst/TUnit) from 1.23.7 to
1.28.7.

<details>
<summary>Release notes</summary>

_Sourced from [TUnit.Core's
releases](https://github.com/thomhurst/TUnit/releases)._

## 1.28.7

<!-- Release notes generated using configuration in .github/release.yml
at v1.28.7 -->

## What's Changed
### Other Changes
* fix: prevent StringBuilder race in console interceptor during parallel
tests by @​thomhurst in thomhurst/TUnit#5414
### Dependencies
* chore(deps): update tunit to 1.28.5 by @​thomhurst in
thomhurst/TUnit#5415


**Full Changelog**:
thomhurst/TUnit@v1.28.5...v1.28.7

## 1.28.5

<!-- Release notes generated using configuration in .github/release.yml
at v1.28.5 -->

## What's Changed
### Other Changes
* perf: eliminate redundant builds in CI pipeline by @​thomhurst in
thomhurst/TUnit#5405
* perf: eliminate store.ToArray() allocation on mock behavior execution
hot path by @​thomhurst in thomhurst/TUnit#5409
* fix: omit non-class/struct constraints on explicit interface mock
implementations by @​thomhurst in
thomhurst/TUnit#5413
### Dependencies
* chore(deps): update tunit to 1.28.0 by @​thomhurst in
thomhurst/TUnit#5406


**Full Changelog**:
thomhurst/TUnit@v1.28.0...v1.28.5

## 1.28.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.28.0 -->

## What's Changed
### Other Changes
* fix: resolve build warnings in solution by @​thomhurst in
thomhurst/TUnit#5386
* Perf: Optimize MockEngine hot paths (~30-42% faster) by @​thomhurst in
thomhurst/TUnit#5391
* Move Playwright install into pipeline module by @​thomhurst in
thomhurst/TUnit#5390
* perf: optimize solution build performance by @​thomhurst in
thomhurst/TUnit#5393
* perf: defer per-class JIT via lazy test registration + parallel
resolution by @​thomhurst in
thomhurst/TUnit#5395
* Perf: Generate typed HandleCall<T1,...> overloads to eliminate
argument boxing by @​thomhurst in
thomhurst/TUnit#5399
* perf: filter generated attributes to TUnit-related types only by
@​thomhurst in thomhurst/TUnit#5402
* fix: generate valid mock class names for generic interfaces with
non-built-in type args by @​thomhurst in
thomhurst/TUnit#5404
### Dependencies
* chore(deps): update tunit to 1.27.0 by @​thomhurst in
thomhurst/TUnit#5392
* chore(deps): update dependency path-to-regexp to v8 by @​thomhurst in
thomhurst/TUnit#5378


**Full Changelog**:
thomhurst/TUnit@v1.27.0...v1.28.0

## 1.27.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.27.0 -->

## What's Changed
### Other Changes
* Fix Dependabot security vulnerabilities in docs site by @​thomhurst in
thomhurst/TUnit#5372
* fix: use 0.0.0-scrubbed sentinel version in snapshot scrubber to avoid
false Dependabot alerts by @​thomhurst in
thomhurst/TUnit#5374
* Speed up Engine.Tests by removing ProcessorCount parallelism cap by
@​thomhurst in thomhurst/TUnit#5379
* ci: add concurrency groups to cancel redundant workflow runs by
@​thomhurst in thomhurst/TUnit#5373
* Add scope-aware initialization and disposal OpenTelemetry spans to
trace timeline and HTML report by @​Copilot in
thomhurst/TUnit#5339
* Add WithInnerExceptions() for fluent AggregateException assertion
chaining by @​thomhurst in thomhurst/TUnit#5380
* Drop net6.0 and net7.0 TFMs, keep net8.0+ and netstandard2.x by
@​thomhurst in thomhurst/TUnit#5387
* Remove all [Obsolete] members and migrate callers by @​thomhurst in
thomhurst/TUnit#5384
* Add AssertionResult.Failed overload that accepts an Exception by
@​thomhurst in thomhurst/TUnit#5388
### Dependencies
* chore(deps): update dependency mockolate to 2.3.0 by @​thomhurst in
thomhurst/TUnit#5370
* chore(deps): update tunit to 1.25.0 by @​thomhurst in
thomhurst/TUnit#5371
* chore(deps): update dependency minimatch to v9.0.9 by @​thomhurst in
thomhurst/TUnit#5375
* chore(deps): update dependency path-to-regexp to v0.2.5 by @​thomhurst
in thomhurst/TUnit#5376
* chore(deps): update dependency minimatch to v10 by @​thomhurst in
thomhurst/TUnit#5377
* chore(deps): update dependency picomatch to v4 by @​thomhurst in
thomhurst/TUnit#5382
* chore(deps): update dependency svgo to v4 by @​thomhurst in
thomhurst/TUnit#5383
* chore(deps): update dependency path-to-regexp to v1 [security] by
@​thomhurst in thomhurst/TUnit#5385


**Full Changelog**:
thomhurst/TUnit@v1.25.0...v1.27.0

## 1.25.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.25.0 -->

## What's Changed
### Other Changes
* Fix missing `default` constraint on explicit interface implementations
with unconstrained generics by @​thomhurst in
thomhurst/TUnit#5363
* feat(mocks): add ReturnsAsync typed factory overload with method
parameters by @​thomhurst in
thomhurst/TUnit#5367
* Fix Arg.IsNull<T> and Arg.IsNotNull<T> to support nullable value types
by @​thomhurst in thomhurst/TUnit#5366
* refactor(mocks): use file-scoped types for generated implementation
details by @​thomhurst in thomhurst/TUnit#5369
* Compress HTML report JSON data and minify CSS by @​thomhurst in
thomhurst/TUnit#5368
### Dependencies
* chore(deps): update tunit to 1.24.31 by @​thomhurst in
thomhurst/TUnit#5356
* chore(deps): update dependency mockolate to 2.2.0 by @​thomhurst in
thomhurst/TUnit#5357
* chore(deps): update dependency polyfill to 9.24.1 by @​thomhurst in
thomhurst/TUnit#5365
* chore(deps): update dependency polyfill to 9.24.1 by @​thomhurst in
thomhurst/TUnit#5364


**Full Changelog**:
thomhurst/TUnit@v1.24.31...v1.25.0

## 1.24.31

<!-- Release notes generated using configuration in .github/release.yml
at v1.24.31 -->

## What's Changed
### Other Changes
* Fix Aspire 13.2.0+ timeout caused by ProjectRebuilderResource being
awaited by @​Copilot in thomhurst/TUnit#5335
* chore(deps): update dependency polyfill to 9.24.0 by @​thomhurst in
thomhurst/TUnit#5349
* Fix nullable IParsable type recognition in source generator and
analyzer by @​Copilot in thomhurst/TUnit#5354
* fix: resolve race condition in HookExecutionOrderTests by @​thomhurst
in thomhurst/TUnit#5355
* Fix MaxExternalSpansPerTest cap bypass when Activity.Parent chain is
broken by @​Copilot in thomhurst/TUnit#5352
### Dependencies
* chore(deps): update tunit to 1.24.18 by @​thomhurst in
thomhurst/TUnit#5340
* chore(deps): update dependency stackexchange.redis to 2.12.14 by
@​thomhurst in thomhurst/TUnit#5343
* chore(deps): update verify to 31.15.0 by @​thomhurst in
thomhurst/TUnit#5346
* chore(deps): update dependency polyfill to 9.24.0 by @​thomhurst in
thomhurst/TUnit#5348


**Full Changelog**:
thomhurst/TUnit@v1.24.18...v1.24.31

## 1.24.18

<!-- Release notes generated using configuration in .github/release.yml
at v1.24.18 -->

## What's Changed
### Other Changes
* feat(mocks): shorter, more readable generated mock type names by
@​thomhurst in thomhurst/TUnit#5334
* Fix DisposeAsync() ordering for nested property injection by @​Copilot
in thomhurst/TUnit#5337
### Dependencies
* chore(deps): update tunit to 1.24.13 by @​thomhurst in
thomhurst/TUnit#5331


**Full Changelog**:
thomhurst/TUnit@v1.24.13...v1.24.18

## 1.24.13

<!-- Release notes generated using configuration in .github/release.yml
at v1.24.13 -->

## What's Changed
### Other Changes
* perf(mocks): optimize MockEngine for lower allocation and faster
verification by @​thomhurst in
thomhurst/TUnit#5319
* Remove defunct `UseTestingPlatformProtocol` reference for vscode by
@​erwinkramer in thomhurst/TUnit#5328
* perf(aspnetcore): prevent thread pool starvation during parallel
WebApplicationTest server init by @​thomhurst in
thomhurst/TUnit#5329
* fix TUnit0073 for when type from from another assembly by @​SimonCropp
in thomhurst/TUnit#5322
* Fix implicit conversion operators bypassed in property injection casts
by @​Copilot in thomhurst/TUnit#5317
* fix(mocks): skip non-virtual 'new' methods when discovering mockable
members by @​thomhurst in thomhurst/TUnit#5330
* feat(mocks): IFoo.Mock() discovery with generic fallback and ORP
resolution by @​thomhurst in
thomhurst/TUnit#5327
### Dependencies
* chore(deps): update tunit to 1.24.0 by @​thomhurst in
thomhurst/TUnit#5315
* chore(deps): update aspire to 13.2.1 by @​thomhurst in
thomhurst/TUnit#5323
* chore(deps): update verify to 31.14.0 by @​thomhurst in
thomhurst/TUnit#5325

## New Contributors
* @​erwinkramer made their first contribution in
thomhurst/TUnit#5328

**Full Changelog**:
thomhurst/TUnit@v1.24.0...v1.24.13

## 1.24.0

<!-- Release notes generated using configuration in .github/release.yml
at v1.24.0 -->

## What's Changed
### Other Changes
* perf: optimize TUnit.Mocks hot paths by @​thomhurst in
thomhurst/TUnit#5304
* fix: resolve System.Memory version conflict on .NET Framework (net462)
by @​thomhurst in thomhurst/TUnit#5303
* fix: resolve CS0460/CS0122/CS0115 when mocking concrete classes from
external assemblies by @​thomhurst in
thomhurst/TUnit#5310
* feat(mocks): parameterless Returns() and ReturnsAsync() for async
methods by @​thomhurst in thomhurst/TUnit#5309
* Fix typo in NUnit manual migration guide by @​aa-ko in
thomhurst/TUnit#5312
* refactor(mocks): unify Mock.Of<T>() and Mock.OfPartial<T>() into
single API by @​thomhurst in
thomhurst/TUnit#5311
* refactor(mocks): clean up Mock API surface by @​thomhurst in
thomhurst/TUnit#5314
* refactor(mocks): remove generic/untyped overloads from public API by
@​thomhurst in thomhurst/TUnit#5313
### Dependencies
* chore(deps): update tunit to 1.23.7 by @​thomhurst in
thomhurst/TUnit#5305
* chore(deps): update dependency mockolate to 2.1.1 by @​thomhurst in
thomhurst/TUnit#5307

## New Contributors
* @​aa-ko made their first contribution in
thomhurst/TUnit#5312

**Full Changelog**:
thomhurst/TUnit@v1.23.7...v1.24.0

Commits viewable in [compare
view](thomhurst/TUnit@v1.23.7...v1.28.7).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=TUnit.Core&package-manager=nuget&previous-version=1.23.7&new-version=1.28.7)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Nullable IParsable is not recognized

2 participants