|
9 | 9 | using FluentAssertions;
|
10 | 10 | using Hl7.Fhir.Model;
|
11 | 11 | using Hl7.Fhir.Utility;
|
| 12 | +using Hl7.Fhir.Validation; |
12 | 13 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
| 14 | +using System.Collections.Generic; |
| 15 | +using System.ComponentModel.DataAnnotations; |
| 16 | +using System; |
13 | 17 |
|
14 | 18 | namespace Hl7.Fhir.Tests.Model
|
15 | 19 | {
|
@@ -48,12 +52,103 @@ public void TestCheckMinorVersionCompatibiliy()
|
48 | 52 | Assert.IsFalse(ModelInfo.CheckMinorVersionCompatibility("3"));
|
49 | 53 | }
|
50 | 54 |
|
51 |
| - //If failed: change the description of the "STN" in the Currency enum of Money.cs from "SC#o TomC) and PrC-ncipe dobra" to "São Tomé and Príncipe dobra". |
| 55 | + //If failed: change the description of the "STN" in the Currency enum of Money.cs from "SC#o TomC) and PrC-ncipe dobra" to "São Tomé and Príncipe dobra". |
52 | 56 | [TestMethod]
|
53 | 57 | public void TestCorrectCurrencyDescription()
|
54 | 58 | {
|
55 | 59 | var currency = Money.Currencies.STN;
|
56 |
| - currency.GetDocumentation().Should().Be("São Tomé and Príncipe dobra"); |
| 60 | + currency.GetDocumentation().Should().Be("São Tomé and Príncipe dobra"); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void ValidatePatientWithDataAbsentExtension() |
| 65 | + { |
| 66 | + // Test for issue #3171 - Patient.Validate(true) throws NullReferenceException |
| 67 | + // when BirthDate has data-absent-reason extension but no value |
| 68 | + // This reproduces the exact scenario from the original issue #3171 report |
| 69 | + |
| 70 | + var patient = new Patient() |
| 71 | + { |
| 72 | + BirthDateElement = new Date() |
| 73 | + { |
| 74 | + Extension = new List<Extension>() |
| 75 | + { |
| 76 | + new Extension |
| 77 | + { |
| 78 | + Url = "http://hl7.org/fhir/StructureDefinition/data-absent-reason", |
| 79 | + Value = new Code |
| 80 | + { |
| 81 | + Value = "unknown" |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + // This exact line was failing with "Object reference not set to an instance of an object" |
| 89 | + // in netstandard2.0 and earlier .NET versions, due to GetHashCode() being called |
| 90 | + // on primitive types with null values during validation |
| 91 | + patient.Validate(true); // Should not throw NullReferenceException anymore |
| 92 | + |
| 93 | + // Ensure patient.Validate(false) still works as it did before |
| 94 | + patient.Validate(false); // This was working before the fix |
| 95 | + |
| 96 | + // Also test with TryValidate to ensure both validation paths work |
| 97 | + ICollection<ValidationResult> results = new List<ValidationResult>(); |
| 98 | + bool isValid = DotNetAttributeValidation.TryValidate(patient, results, true); |
| 99 | + // The validation may or may not pass (depends on other validation rules), |
| 100 | + // but it should not throw an exception |
| 101 | + } |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void DateGetHashCodeWithNullValue() |
| 105 | + { |
| 106 | + // Direct test for Date.GetHashCode() with null value - reproduces issue #3171 |
| 107 | + var date = new Date(); |
| 108 | + // Verify that Value is null |
| 109 | + Assert.IsNull(date.Value); |
| 110 | + |
| 111 | + // This should not throw NullReferenceException |
| 112 | + try |
| 113 | + { |
| 114 | + int hashCode = date.GetHashCode(); |
| 115 | + Assert.IsTrue(true, "GetHashCode completed without throwing an exception"); |
| 116 | + } |
| 117 | + catch (NullReferenceException ex) |
| 118 | + { |
| 119 | + Assert.Fail($"GetHashCode threw NullReferenceException: {ex.Message}"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + [TestMethod] |
| 124 | + public void AllPrimitiveTypesGetHashCodeWithNullValue() |
| 125 | + { |
| 126 | + // Test all primitive types to ensure they handle null values correctly |
| 127 | + var date = new Date(); |
| 128 | + var dateTime = new FhirDateTime(); |
| 129 | + var instant = new Instant(); |
| 130 | + var time = new Time(); |
| 131 | + |
| 132 | + // All should have null values |
| 133 | + Assert.IsNull(date.Value); |
| 134 | + Assert.IsNull(dateTime.Value); |
| 135 | + Assert.IsNull(instant.Value); |
| 136 | + Assert.IsNull(time.Value); |
| 137 | + |
| 138 | + // None should throw exceptions when GetHashCode is called |
| 139 | + try |
| 140 | + { |
| 141 | + int hashCode1 = date.GetHashCode(); |
| 142 | + int hashCode2 = dateTime.GetHashCode(); |
| 143 | + int hashCode3 = instant.GetHashCode(); |
| 144 | + int hashCode4 = time.GetHashCode(); |
| 145 | + |
| 146 | + Assert.IsTrue(true, "All GetHashCode calls completed without throwing exceptions"); |
| 147 | + } |
| 148 | + catch (NullReferenceException ex) |
| 149 | + { |
| 150 | + Assert.Fail($"One of the GetHashCode calls threw NullReferenceException: {ex.Message}"); |
| 151 | + } |
57 | 152 | }
|
58 | 153 | }
|
59 | 154 | }
|
0 commit comments