Repository navigation
Fix type mapping regression for sql_variant and array interface targets (#2234, #2231) - #2267
Open
lqkhanh295 wants to merge 1 commit into
Open
lqkhanh295 wants to merge 1 commit into
lqkhanh295 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Summary
Fixes two type mapping regressions introduced in commit
6d48ef6:sql_variant(reported by ADO.NET withcolType = typeof(object)) into aboolor primitive member threwInvalidCastException: Specified cast is not valid(or unable to cast Int32 to Boolean).SMALLINT[](reported by Npgsql asshort[]) into an interface property such asIReadOnlyList<short>threwInvalidCastException: Object must implement IConvertibleatConvert.ChangeType.Root Cause
In commit
6d48ef6, the condition inLoadReaderValueOrBranchToDBNullLabelwas modified:colType == typeof(object)was added to the direct unbox branch under the assumption thatsql_variantcan directly unbox to the target type. However, ifsql_variantcontains anInt32and the target member isbool, emittingOpCodes.Unbox_Any, typeof(bool)violates CLR unbox rules and throwsInvalidCastException.dataTypeCode == unboxTypeCodewas constrained to!= TypeCode.Object. For arrays implementing interfaces (e.g.short[]implementingIReadOnlyList<short>), both haveTypeCode.Object. ExcludingTypeCode.Objectbypassed the direct castclass/unbox path and routed the array toFlexibleConvertBoxedFromHeadOfStack, which calledConvert.ChangeTypeon a non-IConvertiblearray.Changes
colType == typeof(object)withunboxType.IsAssignableFrom(colType).unboxType.IsAssignableFrom(colType)is true (e.g.short[]toIReadOnlyList<short>),OpCodes.Unbox_Anycorrectly performs acastclasson the reference/interface type.typeof(object)(such assql_variant) now correctly route toFlexibleConvertBoxedFromHeadOfStack, whereConvert.ChangeTypecoerces the boxed runtime value (e.g.int1 tobooltrue).Testing
TypeMappingRegressionTests.cscovering:sql_variant(objectcolumn type containingint) mapped tobool,bool?, andlong.short[],int[]) mapped toIReadOnlyList<short>andIEnumerable<int>.Dapper.Testssuite passes (800 tests executed, 0 failures).