public void AnnotationsFromParsingTest()
        {
            var _sdsProvider = new PocoStructureDefinitionSummaryProvider();
            var patientJson  = "{\"resourceType\":\"Patient\", \"active\":\"true\"}";
            var patient      = FhirJsonNode.Parse(patientJson);
            var typedPatient = patient.ToTypedElement(_sdsProvider, "Patient");
            var sourceNode   = typedPatient.ToSourceNode();

            var result = patient.Annotation <ISourceNode>();

            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(FhirJsonNode), result.GetType(), "ISourceNode is provided by FhirJsonNode");
            Assert.AreSame(patient, result);

            var result2 = sourceNode.Annotation <ISourceNode>();

            Assert.IsNotNull(result2);
            Assert.AreEqual(typeof(TypedElementToSourceNodeAdapter), result2.GetType(), "Now ISourceNode is provided by TypedElementToSourceNodeAdapter");
            Assert.AreSame(sourceNode, result2);

            var result3 = sourceNode.Annotation <IResourceTypeSupplier>();

            Assert.IsNotNull(result3);
            Assert.AreEqual(typeof(TypedElementToSourceNodeAdapter), result3.GetType());
        }
示例#2
0
        private static Bundle ParseJSON(string content, bool permissive)
        {
            Bundle bundle = null;

            // Grab all errors found by visiting all nodes and report if not permissive
            if (!permissive)
            {
                List <string> entries = new List <string>();
                ISourceNode   node    = FhirJsonNode.Parse(content, "Bundle", new FhirJsonParsingSettings {
                    PermissiveParsing = permissive
                });
                foreach (Hl7.Fhir.Utility.ExceptionNotification problem in node.VisitAndCatch())
                {
                    entries.Add(problem.Message);
                }
                if (entries.Count > 0)
                {
                    throw new System.ArgumentException(String.Join("; ", entries).TrimEnd());
                }
            }
            // Try Parse
            try
            {
                FhirJsonParser parser = new FhirJsonParser(GetParserSettings(permissive));
                bundle = parser.Parse <Bundle>(content);
            }
            catch (Exception e)
            {
                throw new System.ArgumentException(e.Message);
            }

            return(bundle);
        }
示例#3
0
        public void CompareToOtherElementNavigator()
        {
            var json = TestDataHelper.ReadTestData("TestPatient.json");
            var xml  = TestDataHelper.ReadTestData("TestPatient.xml");

            var pocoP = (new FhirJsonParser()).Parse <Patient>(json).ToTypedElement();
            var jsonP = FhirJsonNode.Parse(json, settings: new FhirJsonParsingSettings {
                AllowJsonComments = true
            })
                        .ToTypedElement(new PocoStructureDefinitionSummaryProvider());
            var xmlP = FhirXmlNode.Parse(xml).ToTypedElement(new PocoStructureDefinitionSummaryProvider());

            doCompare(pocoP, jsonP, "poco<->json");
            doCompare(pocoP, xmlP, "poco<->xml");

            void doCompare(ITypedElement one, ITypedElement two, string what)
            {
                var compare = one.IsEqualTo(two);

                if (compare.Success == false)
                {
                    Debug.WriteLine($"{what}: Difference in {compare.Details} at {compare.FailureLocation}");
                    Assert.Fail();
                }
            }
        }
示例#4
0
        internal override ResourceElement GetPatchedResourceElement(ResourceWrapper resourceToPatch)
        {
            var resourceJsonNode = (FhirJsonNode)FhirJsonNode.Parse(resourceToPatch.RawResource.Data);

            try
            {
                PatchDocument.ApplyTo(resourceJsonNode.JsonObject);
            }
            catch (JsonPatchException e)
            {
                throw new RequestNotValidException(e.Message, OperationOutcomeConstants.IssueType.Processing);
            }
            catch (ArgumentNullException e)
            {
                throw new RequestNotValidException(e.Message, OperationOutcomeConstants.IssueType.Processing);
            }

            Resource resourcePoco;

            try
            {
                resourcePoco = resourceJsonNode
                               .ToTypedElement(ModelInfoProvider.StructureDefinitionSummaryProvider)
                               .ToPoco <Resource>();
            }
            catch (Exception e)
            {
                throw new RequestNotValidException(string.Format(Core.Resources.PatchResourceError, e.Message));
            }

            return(resourcePoco.ToResourceElement());
        }
 private ITypedElement ParseJsonToTypedElement(string json)
 {
     try
     {
         return(FhirJsonNode.Parse(json).ToTypedElement(_provider));
     }
     catch (Exception ex)
     {
         throw new InvalidInputException($"The input FHIR resource JSON is invalid.", ex);
     }
 }
示例#6
0
        public ResourceElement Patch(ResourceWrapper resourceToPatch, JsonPatchDocument patchDocument, WeakETag weakETag)
        {
            EnsureArg.IsNotNull(resourceToPatch, nameof(resourceToPatch));

            Validate(resourceToPatch, weakETag, patchDocument);

            var node = (FhirJsonNode)FhirJsonNode.Parse(resourceToPatch.RawResource.Data);

            // Capture the state of properties that are immutable
            ITypedElement resource = node.ToTypedElement(_modelInfoProvider.StructureDefinitionSummaryProvider);

            (string path, object result)[] preState = _immutableProperties.Select(x => (path: x, result: resource.Scalar(x))).ToArray();
示例#7
0
        public void TryInvalidUntypedSource()
        {
            var jsonNav = FhirJsonNode.Parse("{ 'resourceType': 'Patient', 'active':true }");

            try
            {
                var output = jsonNav.ToXml();
                Assert.Fail();
            }
            catch (NotSupportedException)
            {
            }
        }
 public ITypedElement DeserializeToElement(
     string resourceContent,
     FhirJsonParsingSettings settings = null)
 {
     if (_fhirVersion == FhirVersion.Stu3)
     {
         return(FhirJsonNode.Parse(resourceContent, settings: settings).ToTypedElement(_stu3Provider));
     }
     else
     {
         return(FhirJsonNode.Parse(resourceContent).ToTypedElement(_r4Provider));
     }
 }
示例#9
0
 public void PreservesParsingExceptionDetails()
 {
     try
     {
         var nav   = FhirJsonNode.Parse("<bla", "test");
         var dummy = nav.Text;
         Assert.Fail();
     }
     catch (FormatException fe)
     {
         Assert.IsInstanceOfType(fe.InnerException, typeof(JsonException));
     }
 }
示例#10
0
        internal static ITypedElement ParseToTypedElement(string json, IStructureDefinitionSummaryProvider provider, string rootName = null,
                                                          FhirJsonParsingSettings settings = null, TypedElementSettings tnSettings = null)
        {
            if (json == null)
            {
                throw Error.ArgumentNull(nameof(json));
            }
            if (provider == null)
            {
                throw Error.ArgumentNull(nameof(provider));
            }

            return(FhirJsonNode.Parse(json, rootName, settings).ToTypedElement(provider, null, tnSettings));
        }
示例#11
0
        public void CatchNullErrors()
        {
            var nav = FhirJsonNode.Parse("{ 'a': null }", "test");

            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ '_a': null }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': null, '_a' : null }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': [null] }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': [null], '_a': [null] }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());
        }
        public void SourceNodeFromElementNodeReturnsResourceTypeSupplier()
        {
            var _sdsProvider = new PocoStructureDefinitionSummaryProvider();
            var patientJson  = "{\"resourceType\":\"Patient\", \"active\":\"true\"}";
            var patientNode  = FhirJsonNode.Parse(patientJson);
            var typedPatient = patientNode.ToTypedElement(_sdsProvider, "Patient");

            var elementNode = ElementNode.FromElement(typedPatient);
            var adapter     = elementNode.ToSourceNode();

            Assert.AreEqual(typeof(TypedElementToSourceNodeAdapter), adapter.GetType(), "ISourceNode is provided by TypedElementToSourceNodeAdapter");

            var result = adapter.Annotation <IResourceTypeSupplier>();

            Assert.IsNotNull(result);
            Assert.AreEqual(typeof(TypedElementToSourceNodeAdapter), result.GetType());
            Assert.AreEqual("Patient", adapter.GetResourceTypeIndicator());
            Assert.AreSame(adapter, result);
        }
        public void CannotUseAbstractType()
        {
            var bundleJson  = "{\"resourceType\":\"Bundle\", \"entry\":[{\"fullUrl\":\"http://example.org/Patient/1\"}]}";
            var bundle      = FhirJsonNode.Parse(bundleJson);
            var typedBundle = bundle.ToTypedElement(provider, "Bundle");

            //Type of entry is BackboneElement, but you can't set that, see below.
            Assert.Equal("BackboneElement", typedBundle.Select("$this.entry[0]").First().InstanceType);

            var entry = SourceNode.Node("entry", SourceNode.Valued("fullUrl", "http://example.org/Patient/1"));

            try
            {
                var typedEntry =
                    entry.ToTypedElement(provider, "Element");
                Microsoft.VisualStudio.TestTools.UnitTesting.Assert.Fail("Should have thrown on invalid Div format");
            }
            catch (ArgumentException)
            {
            }
        }
示例#14
0
        public void CatchesArrayMismatch()
        {
            var nav = FhirJsonNode.Parse("{ 'a': [2,3,4], '_a' : [2,4] }", "test");

            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': 2, '_a' : [2] }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': [2,3,4], '_a' : {} }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ '_a': [4,5,6] }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': [2,3,4] }", "test");
            Assert.IsTrue(nav.Children().Any());

            nav = FhirJsonNode.Parse("{ 'a': [null,2], '_a' : [{'active':true},null] }", "test");
            Assert.IsTrue(nav.Children().Any());
        }
示例#15
0
        public ITypedElement Build()
        {
            // To build a CapabilityStatement we use a custom JsonConverter that serializes
            // the ListedCapabilityStatement into a CapabilityStatement poco

            var json = JsonConvert.SerializeObject(_statement, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Converters       = new List <JsonConverter>
                {
                    new DefaultOptionHashSetJsonConverter(),
                    new EnumLiteralJsonConverter(),
                },
                NullValueHandling = NullValueHandling.Ignore,
            });

            ISourceNode jsonStatement = FhirJsonNode.Parse(json);

            // Using a version specific StructureDefinitionSummaryProvider ensures the metadata to be
            // compatible with the current FhirSerializer/output formatter.
            return(jsonStatement.ToTypedElement(_modelInfoProvider.StructureDefinitionSummaryProvider));
        }
        private static ITypedElement parseResource(string bodyText, string contentType, IStructureDefinitionSummaryProvider provider, bool throwOnFormatException)
        {
            if (bodyText == null)
            {
                throw Error.ArgumentNull(nameof(bodyText));
            }
            if (provider == null)
            {
                throw Error.ArgumentNull(nameof(provider));
            }

            var fhirType = ContentType.GetResourceFormatFromContentType(contentType);

            if (fhirType == ResourceFormat.Unknown)
            {
                throw new UnsupportedBodyTypeException(
                          "Endpoint returned a body with contentType '{0}', while a valid FHIR xml/json body type was expected. Is this a FHIR endpoint?"
                          .FormatWith(contentType), contentType, bodyText);
            }

            if (!SerializationUtil.ProbeIsJson(bodyText) && !SerializationUtil.ProbeIsXml(bodyText))
            {
                throw new UnsupportedBodyTypeException(
                          "Endpoint said it returned '{0}', but the body is not recognized as either xml or json.".FormatWith(contentType), contentType, bodyText);
            }

            try
            {
                return((fhirType == ResourceFormat.Json)
                    ? FhirJsonNode.Parse(bodyText).ToTypedElement(provider)
                    : FhirXmlNode.Parse(bodyText).ToTypedElement(provider));
            }
            catch (FormatException) when(!throwOnFormatException)
            {
                return(null);
            }
        }
示例#17
0
        public void CanUseBackboneTypeForEntry()
        {
            var _sdsProvider = new PocoStructureDefinitionSummaryProvider();
            var bundleJson   = "{\"resourceType\":\"Bundle\", \"entry\":[{\"fullUrl\":\"http://example.org/Patient/1\"}]}";
            var bundle       = FhirJsonNode.Parse(bundleJson);
            var typedBundle  = bundle.ToTypedElement(_sdsProvider, "Bundle");

            //Type of entry is BackboneElement, but you can't set that, see below.
            Assert.Equal("BackboneElement", typedBundle.Select("$this.entry[0]").First().InstanceType);

            var entry = SourceNode.Node("entry", SourceNode.Valued("fullUrl", "http://example.org/Patient/1"));

            //What DOES work:
            var typedEntry = entry.ToTypedElement(_sdsProvider, "Element"); //But you can't use BackboneElement here, see below.

            Assert.Equal("Element", typedEntry.InstanceType);

            //But this leads to a System.ArgumentException:
            //Type BackboneElement is not a mappable Fhir datatype or resource
            //Parameter name: type
            typedEntry = entry.ToTypedElement(_sdsProvider, "BackboneElement");
            Assert.Equal("BackboneElement", typedEntry.InstanceType);
            // Expected to be able to use BackboneElement as type for the entry SourceNode;
        }
示例#18
0
        public void CatchesUnsupportedFeatures()
        {
            var nav = FhirJsonNode.Parse("{ 'a': '   ' }", "test");

            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': {}, '_a' : {} }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': {'active':true}, '_a': {'dummy':4} }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ '_a' : {} }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': 3, '_a' : 4 }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ 'a': new DateTime() }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());

            nav = FhirJsonNode.Parse("{ '_a': new DateTime() }", "test");
            Assert.ThrowsException <FormatException>(() => nav.VisitAll());
        }
示例#19
0
 ISourceNode FhirJsonNodeParse(string json, string rootName) =>
 FhirJsonNode.Parse(json, rootName, new FhirJsonParsingSettings()
 {
     PermissiveParsing = false
 });
示例#20
0
 public ISourceNode getJsonNodeU(string json, FhirJsonParsingSettings settings = null) =>
 FhirJsonNode.Parse(json, settings: settings);
示例#21
0
        /// <inheritdoc />
        public void Load(IServiceCollection services)
        {
            EnsureArg.IsNotNull(services, nameof(services));

            var jsonParser     = new FhirJsonParser(DefaultParserSettings.Settings);
            var jsonSerializer = new FhirJsonSerializer();

            var xmlParser     = new FhirXmlParser();
            var xmlSerializer = new FhirXmlSerializer();

            services.AddSingleton(jsonParser);
            services.AddSingleton(jsonSerializer);
            services.AddSingleton(xmlParser);
            services.AddSingleton(xmlSerializer);
            services.AddSingleton <BundleSerializer>();

            FhirPathCompiler.DefaultSymbolTable.AddFhirExtensions();

            ResourceElement SetMetadata(Resource resource, string versionId, DateTimeOffset lastModified)
            {
                resource.VersionId        = versionId;
                resource.Meta.LastUpdated = lastModified;

                return(resource.ToResourceElement());
            }

            services.AddSingleton <IReadOnlyDictionary <FhirResourceFormat, Func <string, string, DateTimeOffset, ResourceElement> > >(_ =>
            {
                return(new Dictionary <FhirResourceFormat, Func <string, string, DateTimeOffset, ResourceElement> >
                {
                    {
                        FhirResourceFormat.Json, (str, version, lastModified) =>
                        {
                            Resource resource = null;
                            var parsed = false;
                            var i = 0;
                            do
                            {
                                try
                                {
                                    resource = jsonParser.Parse <Resource>(str);
                                    parsed = true;
                                }
                                catch (StructuralTypeException ex)
                                {
                                    var match = MessageChecker.Match(ex.Message);
                                    if (match.Success && match.Groups.Count == 4 && match.Groups["type"].Value == "date")
                                    {
                                        i++;
                                        if (i > 100)
                                        {
                                            throw;
                                        }

                                        var valueToReplace = match.Groups["value"].Value;
                                        var location = match.Groups["location"].Value;
                                        var replace = valueToReplace.Substring(0, 10);
                                        var root = FhirJsonNode.Parse(str, KnownResourceTypes.Resource);
                                        var currentNode = root;
                                        while (currentNode != null)
                                        {
                                            foreach (var child in currentNode.Children())
                                            {
                                                if (location.StartsWith(child.Location, StringComparison.OrdinalIgnoreCase))
                                                {
                                                    currentNode = child;
                                                    break;
                                                }
                                            }

                                            if (currentNode.Location == location)
                                            {
                                                break;
                                            }
                                        }

                                        (currentNode as FhirJsonNode).JsonValue.Value = replace;
                                        str = root.ToJson();
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }
                            }while (!parsed);
                            return SetMetadata(resource, version, lastModified);
                        }
                    },
                    {
                        FhirResourceFormat.Xml, (str, version, lastModified) =>
                        {
                            var resource = xmlParser.Parse <Resource>(str);

                            return SetMetadata(resource, version, lastModified);
                        }
                    },
                });
            });

            services.Add <ResourceDeserializer>()
            .Singleton()
            .AsSelf()
            .AsService <IResourceDeserializer>();

            services.Add <FormatterConfiguration>()
            .Singleton()
            .AsSelf()
            .AsService <IPostConfigureOptions <MvcOptions> >();

            services.AddSingleton <IFormatParametersValidator, FormatParametersValidator>();
            services.AddSingleton <OperationOutcomeExceptionFilterAttribute>();
            services.AddSingleton <ValidateFormatParametersAttribute>();
            services.AddSingleton <ValidateExportRequestFilterAttribute>();
            services.AddSingleton <ValidateReindexRequestFilterAttribute>();

            // Support for resolve()
            FhirPathCompiler.DefaultSymbolTable.AddFhirExtensions();

            services.Add <FhirJsonInputFormatter>()
            .Singleton()
            .AsSelf()
            .AsService <TextInputFormatter>();

            services.Add <FhirJsonOutputFormatter>()
            .Singleton()
            .AsSelf()
            .AsService <TextOutputFormatter>();

            services.Add <FhirRequestContextAccessor>()
            .Singleton()
            .AsSelf()
            .AsService <RequestContextAccessor <IFhirRequestContext> >();

            services.AddSingleton <CorrelationIdProvider>(_ => () => Guid.NewGuid().ToString());

            // Add conformance provider for implementation metadata.
            services.Add <SystemConformanceProvider>()
            .Singleton()
            .AsSelf()
            .AsImplementedInterfaces();

            services.TypesInSameAssembly(KnownAssemblies.All)
            .AssignableTo <IProvideCapability>()
            .Transient()
            .AsService <IProvideCapability>();

            services.AddSingleton <IClaimsExtractor, PrincipalClaimsExtractor>();

            ModelExtensions.SetModelInfoProvider();
            services.Add(_ => ModelInfoProvider.Instance).Singleton().AsSelf().AsImplementedInterfaces();

            // Register a factory to resolve a scope that returns all components that provide capabilities
            services.AddFactory <IScoped <IEnumerable <IProvideCapability> > >();

            // Register pipeline behavior to intercept create/update requests and check presence of provenace header.
            services.Add <ProvenanceHeaderBehavior>().Scoped().AsSelf().AsImplementedInterfaces();
            services.Add <ProvenanceHeaderState>().Scoped().AsSelf().AsImplementedInterfaces();

            // Register pipeline behavior to check service permission for CUD actions on StructuredDefinition,ValueSet,CodeSystem, ConceptMap.

            services.Add <ProfileResourcesBehaviour>().Singleton().AsSelf().AsImplementedInterfaces();

            services.AddLazy();
            services.AddScoped();
        }