JsonNet: Throw if custom quantity instantiation fails, not null#1664
Conversation
|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
|
I can't remove the stale label, but this pr is still needed |
|
@claude review this |
|
Claude finished @angularsen's task in 1m 51s —— View job Review of PR #1664 — UnitsNetBaseJsonConverter compatibility patch
SummaryThe core bug fix here is correct and well tested. The original problem: return (IQuantity)Activator.CreateInstance(registeredQuantity, valueUnit.Value, unit)!;If Current state of the codeNote that
This is a solid improvement over the original patch — richer diagnostics and a single, specific, catchable exception type instead of a bare Minor observations (non-blocking)
Note on stale label@andrewmeservy is right that this is still needed — the underlying null-cast bug is real and the fix (as refined) looks correct and safe to merge. I don't have permission to remove labels, but the fix itself looks ready from a code standpoint. I wasn't able to run |
85d32bd to
3e7dcc7
Compare
Why
UnitsNetBaseJsonConverterinstantiates registered custom quantity types withActivator.CreateInstance(). If that reflection call fails, or if it returnsnull, the converter should not returnnullfrom its non-nullIQuantityconversion path or surface an unclear reflection exception.What
UnitsNetExceptionwhen a registered custom quantity cannot be instantiated.Exception.Data.UnitsNetException.ErrorCodeDataKeyas the sharedException.Datakey for error codes.Validation
dotnet test UnitsNet.Serialization.JsonNet.Tests -f net8.0 --filter "FullyQualifiedName~UnitsNetBaseJsonConverterTest"dotnet test UnitsNet.Serialization.JsonNet.Tests -f net9.0 --filter "FullyQualifiedName~UnitsNetBaseJsonConverterTest"dotnet test UnitsNet.Serialization.JsonNet.Tests -f net10.0 --filter "FullyQualifiedName~UnitsNetBaseJsonConverterTest"