From 74055f3d385698908db2555f6547354040a8331b Mon Sep 17 00:00:00 2001 From: Brian Postlethwaite Date: Tue, 14 Jan 2025 12:17:30 +1100 Subject: [PATCH] #3008 While validating an element, use it's FHIR attribute name not the class property name if it is available. --- .../Introspection/FhirElementAttribute.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Hl7.Fhir.Base/Introspection/FhirElementAttribute.cs b/src/Hl7.Fhir.Base/Introspection/FhirElementAttribute.cs index 17f9b38c55..65c4cd3167 100644 --- a/src/Hl7.Fhir.Base/Introspection/FhirElementAttribute.cs +++ b/src/Hl7.Fhir.Base/Introspection/FhirElementAttribute.cs @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; +using System.Reflection; #nullable enable @@ -125,7 +126,18 @@ public FhirElementAttribute(string name, ChoiceType choice, XmlRepresentation re private void validateElement(object value, ValidationContext validationContext, List result) { - DotNetAttributeValidation.TryValidate(value, validationContext.IntoPath(value, validationContext.MemberName ?? Name), result); + var useName = validationContext.MemberName; + if (!string.IsNullOrEmpty(useName)) + { + var pm = ReflectionHelper.FindProperty(validationContext.ObjectType, validationContext.MemberName); + if (pm != null) + { + var at = pm.GetCustomAttribute(); + if (at != null) + useName = at.Name; + } + } + DotNetAttributeValidation.TryValidate(value, validationContext.IntoPath(value, useName ?? Name), result); } } }