Fix NullReferenceException when serializing null System.Type properties#1091
Merged
EdwardCooke merged 1 commit intoaaubry:masterfrom Apr 9, 2026
Merged
Conversation
SystemTypeConverter.WriteYaml() now handles null values by emitting an empty scalar instead of crashing with NullReferenceException. SystemTypeConverter.ReadYaml() now handles empty/null scalar values by returning null instead of throwing on Type.GetType(). Added tests: CanSerializeNullType, CanSerializeNonNullType, NullTypeRoundTrips, NonNullTypeRoundTrips. Fixes the issue reported in PR aaubry#1038.
fdcastel
added a commit
to fdcastel/YamlDotNet
that referenced
this pull request
Mar 26, 2026
All value-type converters (Guid, TimeSpan, DateTime, DateTimeOffset, DateOnly, TimeOnly) now accept their Nullable<T> counterpart. All converters (including reference-type Uri) now handle null values in ReadYaml (return null for empty/null scalars) and WriteYaml (emit an empty scalar instead of throwing NullReferenceException). SystemTypeConverter is intentionally excluded — it is already fixed by PR aaubry#1091.
fdcastel
added a commit
to fdcastel/YamlDotNet
that referenced
this pull request
Mar 26, 2026
All value-type converters (Guid, TimeSpan, DateTime, DateTimeOffset, DateOnly, TimeOnly) now accept their Nullable<T> counterpart. All converters (including reference-type Uri) now handle null values in ReadYaml (return null for empty/null scalars) and WriteYaml (emit an empty scalar instead of throwing NullReferenceException). SystemTypeConverter is intentionally excluded — it is already fixed by PR aaubry#1091.
fdcastel
added a commit
to fdcastel/YamlDotNet
that referenced
this pull request
Mar 28, 2026
All value-type converters (Guid, TimeSpan, DateTime, DateTimeOffset, DateOnly, TimeOnly) now accept their Nullable<T> counterpart. All converters (including reference-type Uri) now handle null values in ReadYaml (return null for empty/null scalars) and WriteYaml (emit an empty scalar instead of throwing NullReferenceException). SystemTypeConverter is intentionally excluded — it is already fixed by PR aaubry#1091.
This was referenced Apr 9, 2026
This was referenced Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Serializing an object with a
System.Typeproperty set tonullthrows aNullReferenceException. This was reported via a failing test in PR #1038.Reproduction:
Root Cause
SystemTypeConverter.WriteYaml()unconditionally castsvaluetoTypeand dereferences it without a null check:Since
System.Typeis a reference type (unlike value-type converters such asGuidConverter), it can legitimately be null. The null isn't caught by the normal pipeline becauseCustomSerializationObjectGraphVisitor.Enter()callsWriteYaml()immediately upon finding the converter, before the null-handling path inFullObjectGraphTraversalStrategygets a chance to intercept.Fix
WriteYaml: Added a null check — emits an empty scalar for null values instead of crashing.ReadYaml: Added handling for empty scalars — returnsnullinstead of passing an empty string toType.GetType()which would throw.Tests Added
CanSerializeNullTypeTypeproperty produces valid YAMLCanSerializeNonNullTypeTypeproperty still works correctlyNullTypeRoundTripsTypesurvives serialize → deserialize round-tripNonNullTypeRoundTripsTypesurvives serialize → deserialize round-tripAll 4 tests pass on
net47,net6.0, andnet8.0.Closes #1038