Skip to content

fix(csharp): support nullable DateOnly and TimeSpan scalar mappings - #1549

Open
grounzero wants to merge 2 commits into
dotansimha:mainfrom
grounzero:fix/csharp-nullable-dateonly-timespan
Open

fix(csharp): support nullable DateOnly and TimeSpan scalar mappings#1549
grounzero wants to merge 2 commits into
dotansimha:mainfrom
grounzero:fix/csharp-nullable-dateonly-timespan

Conversation

@grounzero

@grounzero grounzero commented Jul 16, 2026

Copy link
Copy Markdown

Add DateOnly, TimeSpan, and DateTimeOffset to the C# value-type allow list, enabling nullable suffix generation for GraphQL scalar fields mapped to these .NET value types.

These are built-in .NET value types and should be treated consistently with DateTime for nullable field generation. This fix corrects broken code generation for schemas using these scalar mappings.

Description

This PR adds support for nullable C# value-type scalar mappings to DateOnly, TimeSpan, and DateTimeOffset in the C# code generator plugins.

Related #1548

Problem

When GraphQL nullable scalar fields are mapped to these .NET value types via scalar configuration, the code generator was failing to append the nullable suffix (?), producing invalid C# code. The nullable logic was already correctly implemented in the field type resolver—it just needed these types added to the value-type allow list.

Root Cause

The csharpValueTypes array in packages/plugins/c-sharp/c-sharp-common/src/scalars.ts was incomplete. The isValueType() helper function checks membership in this list. While DateTime was included, the other built-in .NET value types were missing.

Solution

Extended the csharpValueTypes allow list to include DateOnly, TimeSpan, and DateTimeOffset. This is a minimal, focused change (3 lines) that leverages existing nullable suffix logic.

Impact

  • Before: public DateOnly duration { get; set; } ❌ (invalid—should be nullable)
  • After: public DateOnly? duration { get; set; } ✅ (correct)

Backwards Compatibility

Fully backwards compatible—only extends the type list. No breaking changes. This fix corrects previously broken behavior for schemas using these scalar mappings.


Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Screenshots/Sandbox

N/A — Data structure modification with## Description


Screenshots/Sandbox

How Has This Been Tested?

This fix has been validated with comprehensive regression tests covering all four value-type scenarios:

C# Schema Plugin Test: "Should properly treat value-type scalar mappings nullability"

Tests Added

  1. C# Schema Plugin Test
  • Validates nullable scalar fields are generated with ? suffix in input types

File: c-sharp.spec.ts lines 723-765)

✅ DateTime nullable mapping → DateTime?
✅ DateOnly nullable mapping → DateOnly?
✅ TimeSpan nullable mapping → TimeSpan?
✅ DateTimeOffset nullable mapping → DateTimeOffset?

  1. C# Operations Plugin Test
    File: c-sharp-operations.spec.ts (lines 250-304)
  • Validates nullable scalar fields are generated with ? suffix in response classes

✅ Response class with optional DateTime fields → DateTime?
✅ Response class with optional DateOnly fields → DateOnly?
✅ Response class with optional TimeSpan fields → TimeSpan?
✅ Response class with optional DateTimeOffset fields → DateTimeOffset?

Test Environment:

  • OS: Windows 11 (tested on Windows; cross-platform compatible)
  • @graphql-codegen/c-sharp: 5.0.0
  • @graphql-codegen/c-sharp-operations: 5.0.0
  • @graphql-codegen/c-sharp-common: 5.0.0
  • NodeJS: 18.x (16.x LTS compatible)
  • Yarn: 1.22.22
  • GraphQL: 16.x
# 1. Install dependencies
corepack yarn install --frozen-lockfile

# 2. Run targeted C# plugin tests
corepack yarn test packages/plugins/c-sharp/c-sharp/test/c-sharp.spec.ts \
  packages/plugins/c-sharp/c-sharp-operations/test/c-sharp-operations.spec.ts

# Expected output:
# Test Files  2 passed (2)
#      Tests  103 passed (103)
#   Duration  3.70s

# 3. Run linting
corepack yarn lint

# 4. Run build
corepack yarn build

# 5. Run full test suite (optional)
corepack yarn test

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Add DateOnly, TimeSpan, and DateTimeOffset to the C# value-type allow
list, enabling nullable suffix generation for GraphQL scalar fields
mapped to these .NET value types.

These are built-in .NET value types and should be treated consistently
with DateTime for nullable field generation. This fix corrects broken
code generation for schemas using these scalar mappings.
@changeset-bot

changeset-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3584743

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@graphql-codegen/c-sharp-common Patch
@graphql-codegen/c-sharp Patch
@graphql-codegen/c-sharp-operations Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

1 participant