Skip to content

Commit 36c543f

Browse files
committed
fix hash function failing for null property values
1 parent 99096ef commit 36c543f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/DynamicObj/HashCodes.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ let hashDateTime (dt : System.DateTime) : int =
1515

1616

1717
let hash obj =
18-
obj.GetHashCode()
18+
if obj = null then
19+
0
20+
else
21+
obj.GetHashCode()
1922

2023
let boxHashOption (a: 'a option) : obj =
2124
if a.IsSome then a.Value.GetHashCode() else (0).GetHashCode()

tests/DynamicObject.Tests/DynamicObjs.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,21 @@ let tests_GetHashCode = testList "GetHashCode" [
535535
b2.SetProperty("c", 2)
536536
a2.SetProperty("b", b2)
537537
Expect.equal (a.GetHashCode()) (a2.GetHashCode()) "Values should be equal"
538+
539+
testCase "null Value same key" <| fun _ ->
540+
let a = DynamicObj()
541+
a.SetProperty("b", null)
542+
let b = DynamicObj()
543+
b.SetProperty("b", null)
544+
Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Values should be equal"
545+
546+
testCase "null Value different key" <| fun _ ->
547+
let a = DynamicObj()
548+
a.SetProperty("b", null)
549+
let b = DynamicObj()
550+
a.SetProperty("c", null)
551+
Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Values should not be equal"
552+
538553
]
539554

540555
let main = testList "DynamicObj (Class)" [

tests/DynamicObject.Tests/Inheritance.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ let tests_remove = testList "Remove" [
6868

6969
Expect.equal p.FirstName null "Static property should "
7070

71+
testCase "Remove Static HashCodeDoesNotFail" <| fun _ ->
72+
let p = Person("123","John")
73+
74+
p.RemoveProperty("FirstName") |> ignore
75+
76+
p.GetHashCode() |> ignore
77+
7178
testCase "Remove Static Immutable" <| fun _ ->
7279
let p = Person("123","John")
7380
let f = fun () -> p.RemoveProperty("Id") |> ignore

0 commit comments

Comments
 (0)