From 7208325618107d5a84e08a1a499197cf42f7c6d0 Mon Sep 17 00:00:00 2001 From: Marten Smits Date: Thu, 5 Aug 2021 13:18:28 +0200 Subject: [PATCH 1/2] fix: use element as primary data type for polymorphic elements --- .../Navigation/ProfileNavigationExtensions.cs | 13 +++++++++++-- .../Specification/Snapshot/SnapshotGenerator.cs | 8 +------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Hl7.Fhir.Specification/Specification/Navigation/ProfileNavigationExtensions.cs b/src/Hl7.Fhir.Specification/Specification/Navigation/ProfileNavigationExtensions.cs index 00be5f5300..9d2451f4bb 100644 --- a/src/Hl7.Fhir.Specification/Specification/Navigation/ProfileNavigationExtensions.cs +++ b/src/Hl7.Fhir.Specification/Specification/Navigation/ProfileNavigationExtensions.cs @@ -7,7 +7,7 @@ */ using Hl7.Fhir.Model; -using Hl7.Fhir.Specification.Navigation; +using Hl7.Fhir.Utility; using System; using System.Collections.Generic; using System.Diagnostics; @@ -104,7 +104,16 @@ public static bool InRange(this ElementDefinition defn, int count) /// A instance, or null. public static ElementDefinition.TypeRefComponent PrimaryType(this ElementDefinition defn) { - return defn.Type != null && defn.Type.Count > 0 ? defn.Type[0] : null; + if (defn.Type.IsNullOrEmpty()) + return null; + else if (defn.Type.Count == 1) + return defn.Type[0]; + else //if there are multiple types (value[x]), try to get a common type, otherwise, use Element as the common datatype + { + var distinctTypeCode = defn.CommonTypeCode() ?? FHIRAllTypes.Element.GetLiteral(); + return new ElementDefinition.TypeRefComponent() { Code = distinctTypeCode }; + } + } /// Returns the type profile reference of the primary element type, if it exists, or null diff --git a/src/Hl7.Fhir.Specification/Specification/Snapshot/SnapshotGenerator.cs b/src/Hl7.Fhir.Specification/Specification/Snapshot/SnapshotGenerator.cs index d5e212b1ea..ccb0e1def6 100644 --- a/src/Hl7.Fhir.Specification/Specification/Snapshot/SnapshotGenerator.cs +++ b/src/Hl7.Fhir.Specification/Specification/Snapshot/SnapshotGenerator.cs @@ -718,13 +718,7 @@ private async T.Task createNewElement(ElementDefinitionNavigator snap, ElementDe { var newElement = (ElementDefinition)baseElement.DeepCopy(); newElement.Path = ElementDefinitionNavigator.ReplacePathRoot(newElement.Path, diff.Path); - newElement.Base = null; - - //Remove type specific constraints on polymorph type elements. - if(diff.Current.Type.Count > 1) - { - removeNewTypeConstraint(newElement, typeStructure); - } + newElement.Base = null; // [WMR 20160915] NEW: Notify subscribers OnPrepareElement(newElement, typeStructure, baseElement); From 8bacde864efade175796b8eeac5d780d0b86f563 Mon Sep 17 00:00:00 2001 From: Marten Smits Date: Thu, 5 Aug 2021 13:19:37 +0200 Subject: [PATCH 2/2] added test --- .../Snapshot/SnapshotGeneratorTest.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Hl7.Fhir.Specification.Tests/Snapshot/SnapshotGeneratorTest.cs b/src/Hl7.Fhir.Specification.Tests/Snapshot/SnapshotGeneratorTest.cs index c26b2c75f4..2637c739c5 100644 --- a/src/Hl7.Fhir.Specification.Tests/Snapshot/SnapshotGeneratorTest.cs +++ b/src/Hl7.Fhir.Specification.Tests/Snapshot/SnapshotGeneratorTest.cs @@ -7431,6 +7431,36 @@ public async T.Task SnapshotSucceedsWithExtendedVariantElementDef() structureDef.Snapshot.Element.Where(element => element.Path == "Observation.value[x].extension").Should().HaveCount(2, "Elements are in the snapshot"); structureDef.Snapshot.Element.Where(element => element.Path == "Observation.extension").Should().HaveCount(1, "Only the root extension should be there"); - } + } + + [TestMethod] + public async T.Task TestExtensionValueXCommentShouldBeNull() + { + const string ElementId = "Extension.value[x]"; + + var zipSource = ZipSource.CreateValidationSource(); + var resolver = new MultiResolver(zipSource); + var sd = await resolver.FindStructureDefinitionForCoreTypeAsync(nameof(Extension)); + + var element = sd.Snapshot.Element.Single(x => x.ElementId == ElementId); + element.Comment.Should().BeNull(); + + var generator = new SnapshotGenerator(resolver, SnapshotGeneratorSettings.CreateDefault()); + + generator.PrepareElement += (sender, e) => + { + if (e.Element.Path == ElementId) + { + Debug.WriteLine($"Element:{ElementId} BaseElement:{e?.BaseElement?.ElementId}"); + } + }; + + // Act + await generator.UpdateAsync(sd); + + // Assert + element = sd.Snapshot.Element.Single(x => x.ElementId == ElementId); + element.Comment.Should().BeNull(); + } } }