Skip to content

Commit

Permalink
#3008 While validating an element, use it's FHIR attribute name not t…
Browse files Browse the repository at this point in the history
…he class property name if it is available. (and update unit tests)
  • Loading branch information
brianpos committed Jan 14, 2025
1 parent 74055f3 commit 5e95761
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
#pragma warning disable CS1580 // Invalid type for parameter in XML comment cref attribute
#pragma warning disable CS1584 // XML comment has syntactically incorrect cref attribute

Expand Down Expand Up @@ -88,7 +89,13 @@ public void ValidateInstance(object? instance, in InstanceDeserializationContext
{
// Add the name of the property to the path, so we can display the correct name of the element,
// even if it does not really contain any values.
var nestedContext = validationContext.IntoEmptyProperty(propMapping.Name);
var useName = propMapping.Name;
// Use the fhir property name, not the .NET property name.
var at = propMapping.NativeProperty.GetCustomAttribute<FhirElementAttribute>();
if (at != null)
useName = at.Name;

var nestedContext = validationContext.IntoEmptyProperty(useName);

errors = add(errors, runAttributeValidation(propValue, new[] { cardinality }, nestedContext));
}
Expand Down
18 changes: 17 additions & 1 deletion src/Hl7.Fhir.Base/Validation/CodedValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
* available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
*/

using Hl7.Fhir.Introspection;
using Hl7.Fhir.Model;
using Hl7.Fhir.Utility;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using COVE = Hl7.Fhir.Validation.CodedValidationException;
using OO_Sev = Hl7.Fhir.Model.OperationOutcome.IssueSeverity;
using OO_Typ = Hl7.Fhir.Model.OperationOutcome.IssueType;
Expand Down Expand Up @@ -93,7 +95,21 @@ internal static CodedValidationException Initialize(ValidationContext context, s
// will return the parent, so we need to add the MemberName.
if (context.MemberName is not null)
{
path = $"{path}.{context.MemberName}";
var useName = context.MemberName;
var pm = ReflectionHelper.FindProperty(context.ObjectType, context.MemberName);
if (pm != null)
{
var at = pm.GetCustomAttribute<FhirElementAttribute>();
if (at != null)
{
useName = at.Name;
if (at.IsPrimitiveValue)
useName = null;
}
}

if (!string.IsNullOrEmpty(useName))
path = $"{path}.{useName}";
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Hl7.Fhir.Shared.Tests/Validation/ValidatePatient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Xml;

namespace Hl7.Fhir.Tests.Validation
Expand All @@ -33,6 +35,7 @@ public void ValidateDemoPatient()

Assert.IsFalse(DotNetAttributeValidation.TryValidate(patient, results, true));
Assert.IsTrue(results.Count > 0);
Console.WriteLine(string.Join("\n", results.Select(r => r.ErrorMessage)));

results.Clear();
foreach (DomainResource contained in patient.Contained) contained.Text = null;
Expand All @@ -46,6 +49,7 @@ public void ValidateDemoPatient()

Assert.IsFalse(DotNetAttributeValidation.TryValidate(patient, results, true));
Assert.IsTrue(results.Count > 0);
Console.WriteLine(string.Join("\n", results.Select(r => r.ErrorMessage)));
}
}
}
11 changes: 7 additions & 4 deletions src/Hl7.Fhir.Shared.Tests/Validation/ValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void TestIdValidation()
validateErrorOrFail(id);

id = new Id("123456789012345678901234567890123456745290123456745290123456745290123456745290");
validateErrorOrFail(id);
validateErrorOrFail(id, errorMessage: "'123456789012345678901234567890123456745290123456745290123456745290123456745290' is not a correct literal for an id. At Id");
}

[TestMethod]
Expand All @@ -63,7 +63,7 @@ public void ValidatesResourceTag()
Assert.IsFalse(DotNetAttributeValidation.TryValidate(p, recurse: true));
}

private static void validateErrorOrFail(Base instance, bool recurse = false, string membername = null)
private static void validateErrorOrFail(Base instance, bool recurse = false, string membername = null, string errorMessage = null)
{
try
{
Expand All @@ -73,8 +73,11 @@ private static void validateErrorOrFail(Base instance, bool recurse = false, str
}
catch (ValidationException ve)
{
Console.WriteLine($"Error: {ve.ValidationResult?.ErrorMessage}");
if (membername != null)
Assert.IsTrue(ve.ValidationResult.MemberNames.Contains(membername));
if (errorMessage != null)
Assert.AreEqual(errorMessage, ve.ValidationResult.ErrorMessage);
}
}

Expand Down Expand Up @@ -213,7 +216,7 @@ public void ValidateResourceWithIncorrectChildElement()
DotNetAttributeValidation.Validate(obs);

// When we recurse, this should fail
validateErrorOrFail(obs, true, membername: "Value");
validateErrorOrFail(obs, true, membername: "Value", errorMessage: "'Ewout Kramer' is not a correct literal for a dateTime. At Observation.effective.start");
}

#if !NETSTANDARD1_6
Expand All @@ -230,7 +233,7 @@ public void TestXhtmlValidation()
validateErrorOrFail(p, true);

p.Text.Div = "<div xmlns='http://www.w3.org/1999/xhtml'><img onmouseover='bigImg(this)' src='smiley.gif' alt='Smiley' /></div>";
validateErrorOrFail(p, true);
validateErrorOrFail(p, true, null, "Value is not well-formed Xml adhering to the FHIR schema for Narrative: The 'onmouseover' attribute is not declared. At Patient.text.div");
}
#endif

Expand Down

0 comments on commit 5e95761

Please sign in to comment.