Skip to content

Write null and real parameters - #890

Merged
SimonCropp merged 1 commit into
mainfrom
null-parameters
Nov 30, 2026
Merged

SimonCropp merged 1 commit into
mainfrom
null-parameters

Conversation

@SimonCropp

Copy link
Copy Markdown
Member

Verifying a null parameter threw for most types, including when a command that has one is recorded:

JsonSerializationException: Error getting value from 'Value' on 'System.Data.SqlTypes.SqlInt32'.
 ---> SqlNullValueException: Data is Null. This method or property cannot be called on Null values.

Cause

There were two bugs, the second hidden behind the first.

  • The SqlValue of a null parameter is a null of a SqlTypes type, such as SqlInt32.Null, whose members throw when they are read. This was the case for
    • DBNull of most types: bigint, binary, bit, datetime, decimal, float, int, money, real, smallint, tinyint, uniqueidentifier, varbinary, xml, and so on
    • a SqlTypes null of any type passed as the value, such as SqlString.Null
    • an output parameter the server set to null
  • ToSqlDbType had no entry for DbType.Single, so a real parameter threw when it was null, or when its value was not already a float.

Fix

A null is written as Value: null with its type, as it already was for the types that did not throw. A SqlTypes null is written the same way as a DBNull of that type.

param: {
  Value: null,
  DbType: Int32
}

Tests

The new tests fail without the fix: every SqlDbType as null, every SqlTypes null, real, and recording a null input and output parameter.

Across 140 cases, 70 threw before and none do now. The other 70 are written as they were, so existing snapshots do not change.

The change to the readme is the snippet line numbers. Version bumped to 12.6.0.

The SqlValue of a null parameter is a null of a SqlTypes type, such as
SqlInt32.Null, whose members throw when they are read, so serializing the
parameter threw a SqlNullValueException. This was the case for DBNull of most
types, for a SqlTypes null passed as the value, and for an output parameter
the server set to null. A null is now written as `Value: null` with its type,
as it already was for the types that did not throw.

ToSqlDbType had no entry for DbType.Single, so a real parameter threw when it
was null, or its value was not already a float.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant