From 0437deacab07552c436dbaf27caa483fecce557c Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Fri, 19 Jan 2024 17:12:02 +0100 Subject: [PATCH 1/2] Ensuring we no longer call ModelInspector.Base - which is almost always a problem --- ...ir.Validation.Compilation.Shared.projitems | 4 +-- .../SchemaBuilders/FixedBuilder.cs | 6 ++++- .../SchemaBuilders/MaxValueBuilder.cs | 6 ++++- .../SchemaBuilders/MinValueBuilder.cs | 7 ++++-- .../SchemaBuilders/PatternBuilder.cs | 6 ++++- .../Validator.cs | 6 ++--- .../Impl/TestSerialization.cs | 25 +++++++++---------- 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/Firely.Fhir.Validation.Compilation.Shared.projitems b/src/Firely.Fhir.Validation.Compilation.Shared/Firely.Fhir.Validation.Compilation.Shared.projitems index a44bdfe1..1260b371 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/Firely.Fhir.Validation.Compilation.Shared.projitems +++ b/src/Firely.Fhir.Validation.Compilation.Shared/Firely.Fhir.Validation.Compilation.Shared.projitems @@ -16,14 +16,14 @@ + + - - diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/FixedBuilder.cs b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/FixedBuilder.cs index c570fbd0..1b2f2c65 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/FixedBuilder.cs +++ b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/FixedBuilder.cs @@ -18,6 +18,7 @@ namespace Firely.Fhir.Validation.Compilation /// internal class FixedBuilder : ISchemaBuilder { + /// public IEnumerable Build(ElementDefinitionNavigator nav, ElementConversionMode? conversionMode = ElementConversionMode.Full) { @@ -27,7 +28,10 @@ public IEnumerable Build(ElementDefinitionNavigator nav, ElementConv var def = nav.Current; if (def.Fixed is not null) - yield return new FixedValidator(def.Fixed.ToTypedElement(ModelInspector.Base)); + { + var inspector = ModelInspector.ForType(def.Fixed.GetType()); + yield return new FixedValidator(def.Fixed.ToTypedElement(inspector)); + } } } } diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MaxValueBuilder.cs b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MaxValueBuilder.cs index cbed9ab9..10871836 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MaxValueBuilder.cs +++ b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MaxValueBuilder.cs @@ -25,7 +25,11 @@ public IEnumerable Build(ElementDefinitionNavigator nav, ElementConv var def = nav.Current; if (def.MaxValue is not null) - yield return new MinMaxValueValidator(def.MaxValue.ToTypedElement(ModelInspector.Base), MinMaxValueValidator.ValidationMode.MaxValue); + { + var inspector = ModelInspector.ForType(def.MaxValue.GetType()); + yield return new MinMaxValueValidator(def.MaxValue.ToTypedElement(inspector), + MinMaxValueValidator.ValidationMode.MaxValue); + } } } } diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MinValueBuilder.cs b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MinValueBuilder.cs index f90f6576..59bb8119 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MinValueBuilder.cs +++ b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/MinValueBuilder.cs @@ -25,8 +25,11 @@ public IEnumerable Build(ElementDefinitionNavigator nav, ElementConv var def = nav.Current; if (def.MinValue is not null) - yield return new MinMaxValueValidator(def.MinValue.ToTypedElement(ModelInspector.Base), MinMaxValueValidator.ValidationMode.MinValue); - + { + var inspector = ModelInspector.ForType(def.MinValue.GetType()); + yield return new MinMaxValueValidator(def.MinValue.ToTypedElement(inspector), + MinMaxValueValidator.ValidationMode.MinValue); + } } } } diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs index 9eaf1e43..1c7a730b 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs +++ b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs @@ -20,6 +20,7 @@ namespace Firely.Fhir.Validation.Compilation /// This constraint is not part of an element refering to a backbone type (see eld-5). internal class PatternBuilder : ISchemaBuilder { + /// public IEnumerable Build(ElementDefinitionNavigator nav, ElementConversionMode? conversionMode = ElementConversionMode.Full) { @@ -28,7 +29,10 @@ public IEnumerable Build(ElementDefinitionNavigator nav, ElementConv var def = nav.Current; if (def.Pattern is not null) - yield return new PatternValidator(def.Pattern.ToTypedElement(ModelInspector.Base)); + { + var inspector = ModelInspector.ForType(def.Pattern.GetType()); + yield return new FixedValidator(def.Pattern.ToTypedElement(inspector)); + } } } } diff --git a/src/Firely.Fhir.Validation.Shared/Validator.cs b/src/Firely.Fhir.Validation.Shared/Validator.cs index 544da9f4..466b5005 100644 --- a/src/Firely.Fhir.Validation.Shared/Validator.cs +++ b/src/Firely.Fhir.Validation.Shared/Validator.cs @@ -49,16 +49,16 @@ public Validator( return toTypedElement(r); } } - + private static ITypedElement? toTypedElement(object? o) => o switch { null => null, ElementNode en => en, - Resource r => r.ToTypedElement(ModelInfo.ModelInspector), + Resource r => r.ToTypedElement(), _ => throw new ArgumentException("Reference resolver must return either a Resource or ElementNode.") }; - + private readonly ValidationSettings _settings; /// diff --git a/test/Firely.Fhir.Validation.Tests/Impl/TestSerialization.cs b/test/Firely.Fhir.Validation.Tests/Impl/TestSerialization.cs index 65347419..9f9b1767 100644 --- a/test/Firely.Fhir.Validation.Tests/Impl/TestSerialization.cs +++ b/test/Firely.Fhir.Validation.Tests/Impl/TestSerialization.cs @@ -87,9 +87,8 @@ public void ValidateSchema() humanName.Add("given", "Patrick", "string"); humanName.Add("given", new string('x', 41), "string"); humanName.Add("given", "1", "integer"); - - - var result = myHumanNameSchema.ToJson().ToString(); + + FluentActions.Invoking(() => myHumanNameSchema.ToJson().ToString()).Should().NotThrow(); var schemaResolver = new InMemoryElementSchemaResolver(new[] { stringSchema }); @@ -121,7 +120,7 @@ public InMemoryElementSchemaResolver(IEnumerable schemas) } public ElementSchema? GetSchema(Canonical schemaUri) => - _schemas.TryGetValue(schemaUri, out var schema) ? schema : null; + _schemas.GetValueOrDefault(schemaUri); } [TestMethod] @@ -133,7 +132,7 @@ public void ValidateBloodPressureSchema() ("code", new CardinalityValidator(min: 1)), ("value[x]", new AllValidator(new CardinalityValidator(min: 1), new FhirTypeLabelValidator("Quantity"))) ) - ); ; + ); static ITypedElement buildCodeableConcept(string system, string code) { @@ -146,20 +145,20 @@ static ITypedElement buildCodeableConcept(string system, string code) return result; } - static CodeableConcept buildCodeableConceptPoco(string system, string code) - { - var result = new CodeableConcept(); - result.Coding.Add(new Coding(system, code)); - return result; - } + // static CodeableConcept buildCodeableConceptPoco(string system, string code) + // { + // var result = new CodeableConcept(); + // result.Coding.Add(new Coding(system, code)); + // return result; + // } var systolicSlice = new SliceValidator.SliceCase("systolic", - new PathSelectorValidator("code", new FixedValidator(buildCodeableConceptPoco("http://loinc.org", "8480-6").ToTypedElement())), + new PathSelectorValidator("code", new FixedValidator(buildCodeableConcept("http://loinc.org", "8480-6").ToTypedElement())), bpComponentSchema ); var dystolicSlice = new SliceValidator.SliceCase("dystolic", - new PathSelectorValidator("code", new FixedValidator(buildCodeableConceptPoco("http://loinc.org", "8462-4").ToTypedElement())), + new PathSelectorValidator("code", new FixedValidator(buildCodeableConcept("http://loinc.org", "8462-4").ToTypedElement())), bpComponentSchema ); From 7260265e038e6baded7dcd65ae0edfd4dcc99ab6 Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Mon, 22 Jan 2024 12:34:46 +0100 Subject: [PATCH 2/2] Fix: PatternBuilder produced FixedValidators. --- .../SchemaBuilders/PatternBuilder.cs | 2 +- src/Firely.Fhir.Validation/Impl/FixedValidator.cs | 2 +- .../SchemaSnaps/PatternSlice.json | 4 ++-- .../SchemaSnaps/PatternSlice.json | 4 ++-- .../SchemaSnaps/PatternSlice.json | 4 ++-- .../Impl/CardinalityValidatorTests.cs | 9 --------- 6 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs index 26894bb4..8a1615d6 100644 --- a/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs +++ b/src/Firely.Fhir.Validation.Compilation.Shared/SchemaBuilders/PatternBuilder.cs @@ -31,7 +31,7 @@ public IEnumerable Build(ElementDefinitionNavigator nav, ElementConv if (def.Pattern is not null) { var inspector = ModelInspector.ForType(def.Pattern.GetType()); - yield return new FixedValidator(def.Pattern.ToTypedElement(inspector)); + yield return new PatternValidator(def.Pattern.ToTypedElement(inspector)); } } } diff --git a/src/Firely.Fhir.Validation/Impl/FixedValidator.cs b/src/Firely.Fhir.Validation/Impl/FixedValidator.cs index 272cfcb8..05085f37 100644 --- a/src/Firely.Fhir.Validation/Impl/FixedValidator.cs +++ b/src/Firely.Fhir.Validation/Impl/FixedValidator.cs @@ -60,7 +60,7 @@ static string displayValue(ITypedElement te) => } /// - public JToken ToJson() => new JProperty($"Fixed[{FixedValue.InstanceType}]", FixedValue.ToPropValue()); + public JToken ToJson() => new JProperty($"fixed[{FixedValue.InstanceType}]", FixedValue.ToPropValue()); } diff --git a/test/Firely.Fhir.Validation.Compilation.Tests.R4/SchemaSnaps/PatternSlice.json b/test/Firely.Fhir.Validation.Compilation.Tests.R4/SchemaSnaps/PatternSlice.json index ba64e65b..5f2d7ba5 100644 --- a/test/Firely.Fhir.Validation.Compilation.Tests.R4/SchemaSnaps/PatternSlice.json +++ b/test/Firely.Fhir.Validation.Compilation.Tests.R4/SchemaSnaps/PatternSlice.json @@ -115,7 +115,7 @@ "pathSelector": { "path": "system", "assertion": { - "Fixed[uri]": "http://example.com/some-bsn-uri" + "fixed[uri]": "http://example.com/some-bsn-uri" } } }, @@ -160,7 +160,7 @@ }, "system": { "id": "#Patient.identifier.system", - "Fixed[uri]": "http://example.com/some-bsn-uri", + "fixed[uri]": "http://example.com/some-bsn-uri", "FastInvariant-ele1": {}, "cardinality": "0..1", "ref": "http://hl7.org/fhir/StructureDefinition/uri" diff --git a/test/Firely.Fhir.Validation.Compilation.Tests.R5/SchemaSnaps/PatternSlice.json b/test/Firely.Fhir.Validation.Compilation.Tests.R5/SchemaSnaps/PatternSlice.json index cdada6a9..4df3d998 100644 --- a/test/Firely.Fhir.Validation.Compilation.Tests.R5/SchemaSnaps/PatternSlice.json +++ b/test/Firely.Fhir.Validation.Compilation.Tests.R5/SchemaSnaps/PatternSlice.json @@ -115,7 +115,7 @@ "pathSelector": { "path": "system", "assertion": { - "Fixed[uri]": "http://example.com/some-bsn-uri" + "fixed[uri]": "http://example.com/some-bsn-uri" } } }, @@ -160,7 +160,7 @@ }, "system": { "id": "#Patient.identifier.system", - "Fixed[uri]": "http://example.com/some-bsn-uri", + "fixed[uri]": "http://example.com/some-bsn-uri", "FastInvariant-ele1": {}, "cardinality": "0..1", "ref": "http://hl7.org/fhir/StructureDefinition/uri" diff --git a/test/Firely.Fhir.Validation.Compilation.Tests.STU3/SchemaSnaps/PatternSlice.json b/test/Firely.Fhir.Validation.Compilation.Tests.STU3/SchemaSnaps/PatternSlice.json index 47bb5fc1..f42fb39e 100644 --- a/test/Firely.Fhir.Validation.Compilation.Tests.STU3/SchemaSnaps/PatternSlice.json +++ b/test/Firely.Fhir.Validation.Compilation.Tests.STU3/SchemaSnaps/PatternSlice.json @@ -99,7 +99,7 @@ "pathSelector": { "path": "system", "assertion": { - "Fixed[uri]": "http://example.com/some-bsn-uri" + "fixed[uri]": "http://example.com/some-bsn-uri" } } }, @@ -139,7 +139,7 @@ }, "system": { "id": "#Patient.identifier.system", - "Fixed[uri]": "http://example.com/some-bsn-uri", + "fixed[uri]": "http://example.com/some-bsn-uri", "cardinality": "0..1", "ref": "http://hl7.org/fhir/StructureDefinition/uri" }, diff --git a/test/Firely.Fhir.Validation.Tests/Impl/CardinalityValidatorTests.cs b/test/Firely.Fhir.Validation.Tests/Impl/CardinalityValidatorTests.cs index b1989972..c0328847 100644 --- a/test/Firely.Fhir.Validation.Tests/Impl/CardinalityValidatorTests.cs +++ b/test/Firely.Fhir.Validation.Tests/Impl/CardinalityValidatorTests.cs @@ -90,14 +90,5 @@ public void InRangeAsync() result = cardinality.Validate(ElementNode.CreateList("1"), ValidationSettings.BuildMinimalContext(), new ValidationState()); Assert.IsFalse(result.IsSuccessful); } - - [TestMethod] - [Ignore] - public void TestFromEvidence() - { - var b = new ValidatorBenchmarks(); - b.GlobalSetup(); - b.WipValidator(); - } } }