Skip to content

fix(source-generators): improve nested property fallback handling for CS8601 warnings#65

Merged
ncipollina merged 3 commits intomainfrom
fix/cs8601-required-nested-properties
Jan 23, 2026
Merged

fix(source-generators): improve nested property fallback handling for CS8601 warnings#65
ncipollina merged 3 commits intomainfrom
fix/cs8601-required-nested-properties

Conversation

@ncipollina
Copy link
Contributor

🚀 Pull Request

📋 Summary

This PR fixes CS8601 "Possible null reference assignment" warnings by implementing smart fallback logic for nested properties based on their characteristics:

Property Type Fallback
required + non-nullable throw new System.InvalidOperationException(...)
Nullable (?) null
Non-nullable, non-required collection [] (empty collection)
Non-nullable, non-required object null

Key Changes

  • PropertyMappingCodeRenderer.cs: Added GetNestedObjectFallback() and GetNestedCollectionFallback() helper methods that determine the appropriate fallback based on analysis.IsRequired and analysis.Nullability.IsNullableType
  • Render methods: Updated to accept a fallback string instead of a boolean, providing more flexibility
  • Tests: Added 5 new tests for nullable nested collections and objects (both mapper-based and inline)

Before (caused CS8601 warnings)

Items = item.TryGetValue("items", ...) ? ... : null,  // Warning on non-nullable List<T>

After (no warnings)

// For non-nullable, non-required collections - use empty collection
Items = item.TryGetValue("items", ...) ? ... : [],

// For nullable collections - use null
Items = item.TryGetValue("items", ...) ? ... : null,

// For required properties - throw
ShippingAddress = item.TryGetValue("shippingAddress", ...) ? ... 
    : throw new System.InvalidOperationException("Required attribute 'shippingAddress' not found."),

✅ Checklist

  • My changes build cleanly
  • I've added/updated relevant tests
  • I've added/updated documentation or README
  • I've followed the coding style for this project
  • I've tested the changes locally (if applicable)

🧪 Related Issues or PRs

N/A


💬 Notes for Reviewers

  • The fallback logic correctly distinguishes between the C# required keyword and simple non-nullability
  • Empty collection expressions ([]) are used for non-required, non-nullable collections which works in C# 12+
  • For non-nullable, non-required nested objects, we still use null as fallback - this is a design limitation where the model should either use required or be nullable

🤖 Generated with Claude Code

ncipollina and others added 3 commits January 22, 2026 08:34
- Update examples to use consistent formatting
- Clean up constructor selector logic
- Minor refactoring across generator and test projects
- Add tech-debt-analysis documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…warnings

- Use `required` keyword to determine when to throw InvalidOperationException
- Use empty collection `[]` as fallback for non-nullable, non-required collections
- Use `null` as fallback for nullable nested properties
- Add helper methods GetNestedObjectFallback and GetNestedCollectionFallback
- Add tests for nullable nested objects and collections (mapper-based and inline)

This eliminates CS8601 warnings by using appropriate fallbacks:
- required + non-nullable: throw InvalidOperationException
- nullable: null
- non-nullable + non-required collections: empty collection []
- non-nullable + non-required objects: null (design limitation)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7c5ad4f31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Collaborator

@j-d-ha j-d-ha left a comment

Choose a reason for hiding this comment

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

👍 LGTM

@ncipollina ncipollina merged commit df43150 into main Jan 23, 2026
3 checks passed
@ncipollina ncipollina deleted the fix/cs8601-required-nested-properties branch January 23, 2026 14:42
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.

2 participants