Skip to content

Commit

Permalink
Merge pull request #41 from CSBiology/nullFix
Browse files Browse the repository at this point in the history
Fix hash function failing for null property values
  • Loading branch information
kMutagene authored Oct 16, 2024
2 parents 99096ef + 32991d3 commit 5a03d10
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 4.0.3+36c543f (Released 2024-10-16)
- fix GetHashCode member failing for null property values

### 4.0.2 (Released 2024-10-15)

- Undo `DynObj.combine` working with `#DynamicObj` as input - this caused issues with ncombining nested DOs of types that inherited from DynamicObj. The type signature has been fixed to `DynamicObj` (without the flexible `#`)
Expand Down
5 changes: 4 additions & 1 deletion src/DynamicObj/HashCodes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ let hashDateTime (dt : System.DateTime) : int =


let hash obj =
obj.GetHashCode()
if obj = null then
0
else
obj.GetHashCode()

let boxHashOption (a: 'a option) : obj =
if a.IsSome then a.Value.GetHashCode() else (0).GetHashCode()
Expand Down
15 changes: 15 additions & 0 deletions tests/DynamicObject.Tests/DynamicObjs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,21 @@ let tests_GetHashCode = testList "GetHashCode" [
b2.SetProperty("c", 2)
a2.SetProperty("b", b2)
Expect.equal (a.GetHashCode()) (a2.GetHashCode()) "Values should be equal"

testCase "null Value same key" <| fun _ ->
let a = DynamicObj()
a.SetProperty("b", null)
let b = DynamicObj()
b.SetProperty("b", null)
Expect.equal (a.GetHashCode()) (b.GetHashCode()) "Values should be equal"

testCase "null Value different key" <| fun _ ->
let a = DynamicObj()
a.SetProperty("b", null)
let b = DynamicObj()
a.SetProperty("c", null)
Expect.notEqual (a.GetHashCode()) (b.GetHashCode()) "Values should not be equal"

]

let main = testList "DynamicObj (Class)" [
Expand Down
7 changes: 7 additions & 0 deletions tests/DynamicObject.Tests/Inheritance.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ let tests_remove = testList "Remove" [

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

testCase "Remove Static HashCodeDoesNotFail" <| fun _ ->
let p = Person("123","John")

p.RemoveProperty("FirstName") |> ignore

p.GetHashCode() |> ignore

testCase "Remove Static Immutable" <| fun _ ->
let p = Person("123","John")
let f = fun () -> p.RemoveProperty("Id") |> ignore
Expand Down

0 comments on commit 5a03d10

Please sign in to comment.