brianpos / fhir-net-web-api

.NET FHIR webapi and netapicore facade library
Other
27 stars 13 forks source link

Facade server only processes first entry in Bundle resource POST #16

Closed haotiansanro closed 1 year ago

haotiansanro commented 1 year ago

Our implementation of the facade server only processes the first entry in a Bundle POST request. By process, I mean the server returns 500 and during debugging the handling only happens for the first entry before it breaks.

I've gone through the code and am pretty convinced it's something we've done because a fresh clone of this repo processes bundles fine. I have spent an embarrassingly long time on this to no avail :) I was hoping someone could point out what I'm missing.

FHIR Bundle POST (via Postman):

{
    "resourceType": "Bundle",
    "meta": {
        "lastUpdated": "2023-05-08T08:44:57.076Z"
    },
    "type": "batch",
    "entry": [
        {
            "request": {
                "method": "POST",
                "url": "Condition"
            },
            "resource": {
                "resourceType": "Observation",
                "meta": {
                    "profile": [
                        "http://hl7.org/fhir/uv/eyecare/StructureDefinition/observation-iop"
                    ]
                },
                "status": "final",
                "category": [
                    {
                        "coding": [
                            {
                                "system": "http://terminology.hl7.org/CodeSystem/observation-category",
                                "code": "exam",
                                "display": "Exam"
                            }
                        ]
                    }
                ],
                "code": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "41633001",
                            "display": "Intraocular pressure"
                        }
                    ]
                },
                "subject": {
                    "reference": "Patient/x.x.28219",
                    "type": "Patient"
                },
                "encounter": {
                    "reference": "Encounter/x.x.130792",
                    "type": "Encounter"
                },
                "effectiveDateTime": "2013-03-27T13:13:46+11:00",
                "valueQuantity": {
                    "value": 17,
                    "unit": "mmHg",
                    "system": "http://unitsofmeasure.org",
                    "code": "mm[Hg]"
                },
                "bodySite": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "18944008",
                            "display": "Right eye structure"
                        }
                    ]
                },
                "method": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "389152008",
                            "display": "Goldmann applanation tonometry"
                        }
                    ]
                }
            }
        },
        {
            "request": {
                "method": "POST",
                "url": "Observation"
            },
            "resource": {
                "resourceType": "Condition",
                "meta": {
                    "profile": [
                        "http://hl7.org/fhir/uv/eyecare/StructureDefinition/condition-base"
                    ]
                },
                "clinicalStatus": {
                    "coding": [
                        {
                            "system": "http://terminology.hl7.org/CodeSystem/condition-clinical",
                            "code": "active",
                            "display": "Active"
                        }
                    ],
                    "text": "Active"
                },
                "verificationStatus": {
                    "coding": [
                        {
                            "system": "http://terminology.hl7.org/CodeSystem/condition-ver-status",
                            "code": "confirmed",
                            "display": "Confirmed"
                        }
                    ]
                },
                "category": [
                    {
                        "coding": [
                            {
                                "system": "http://terminology.hl7.org/CodeSystem/condition-category",
                                "code": "encounter-diagnosis",
                                "display": "Encounter Diagnosis"
                            }
                        ]
                    }
                ],
                "subject": {
                    "reference": "Patient/x.x.28219",
                    "type": "Patient"
                },
                "encounter": {
                    "reference": "Encounter/x.x.130792",
                    "type": "Encounter"
                },
                "onsetDateTime": "2023-05-08T08:44:57.075Z",
                "recordedDate": "2023-05-08T08:44:57.075Z",
                "code": {
                    "coding": [
                        {
                            "system": "http://snomed.info/sct",
                            "code": "414875008",
                            "display": "Nonexudative age-related macular degeneration"
                        }
                    ],
                    "text": "Geographic atrophy"
                },
                "bodySite": [
                    {
                        "coding": [
                            {
                                "system": "http://snomed.info/sct",
                                "code": "18944008",
                                "display": "Right eye structure"
                            },
                            {
                                "system": "http://snomed.info/sct",
                                "code": "264209008",
                                "display": "Subfoveal"
                            }
                        ],
                        "text": "Subfoveal right eye"
                    }
                ]
            }
        }
    ]
}

Server log:

info: Hl7.Fhir.WebApi.FhirR4Controller[0]
POST: https://8f3b-223-25-113-49.ngrok-free.app/
System.Threading.Tasks.Task1[Hl7.Fhir.Model.Resource]
crit: Hl7.Fhir.WebApi.FhirExceptionFilter[0]
      Unhandled exception in the Server
      System.AggregateException: One or more errors occurred. (Object reference
not set to an instance of an object.)
       ---> System.NullReferenceException: Object reference not set to an instan
ce of an object.
         at Hl7.Fhir.Utility.AnnotatedExtensions.Annotation[A](IAnnotated annota
ted)
         at Hl7.Fhir.WebApi.BatchOperationProcessing1.ProcessBatchEntry(ModelBa
seInputs1 request, Bundle outcome, Dictionary2 mappedResourceIds, EntryCompone
nt entry)
at Hl7.Fhir.WebApi.BatchOperationProcessing1.ProcessBatch(ModelBaseInp
uts1 request, Bundle batch)
--- End of inner exception stack trace ---
![image 2](https://github.com/brianpos/fhir-net-web-api/assets/121207588/64721a1b-b77c-424a-acad-e4294551c8b6)

at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCa
nceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, Cancella
tionToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at bpvip_resources.SystemService1.ProcessBatch(ModelBaseInputs1 reque
st, Bundle batch) in C:\Users\sanro.admin\Desktop\bpvip-server\bpvip-resources\S
ystemService.cs:line 95
at Hl7.Fhir.WebApi.FhirR4Controller.ProcessBatch(Bundle batch)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfI
ActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecuto
r executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<Inv
okeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask
1 actionResultValueTask)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<Inv
okeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task l
astTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Reth
row(ActionExecutedContextSealed context)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next
(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Invo
keInnerFilterAsync()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextE
xceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, Stat
e next, Scope scope, Object state, Boolean isCompleted)
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action Hl7.Fhir.WebApi.FhirR4Controller.ProcessBatch (Hl7.Fhir.R4
.WebApi.AspNetCore) in 3771.1764ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Hl7.Fhir.WebApi.FhirR4Controller.ProcessBatch (Hl7.Fhir
.R4.WebApi.AspNetCore)'
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HMR5DSVO3DE5", Request id "0HMR5DSVO3DE5:00000002": An unh
andled exception was thrown by the application.
      System.AggregateException: One or more errors occurred. (Object reference
not set to an instance of an object.)
       ---> System.NullReferenceException: Object reference not set to an instan
ce of an object.
         at Hl7.Fhir.Utility.AnnotatedExtensions.Annotation[A](IAnnotated annota
ted)
         at Hl7.Fhir.WebApi.BatchOperationProcessing1.ProcessBatchEntry(ModelBa
seInputs1 request, Bundle outcome, Dictionary2 mappedResourceIds, EntryCompone
nt entry)
at Hl7.Fhir.WebApi.BatchOperationProcessing1.ProcessBatch(ModelBaseInp
uts1 request, Bundle batch)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCa
nceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, Cancella
tionToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at bpvip_resources.SystemService1.ProcessBatch(ModelBaseInputs1 reque
st, Bundle batch) in C:\Users\sanro.admin\Desktop\bpvip-server\bpvip-resources\S
ystemService.cs:line 95
at Hl7.Fhir.WebApi.FhirR4Controller.ProcessBatch(Bundle batch)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfI
ActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecuto
r executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<Inv
okeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`
1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<Inv
okeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task l
astTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Reth
row(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next
(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Invo
keInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextE
xceptionFilterAsync>g__Awaited|26_0(ResourceInvoker invoker, Task lastTask, Stat
e next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(Exce
ptionContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&
next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextRe
sourceFilter()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(Reso
urceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&
next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilter
PipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync

g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync
g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequ
estTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(
HttpContext context)
at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddlewa
![image 2](https://github.com/brianpos/fhir-net-web-api/assets/121207588/c58714cb-cee9-40cd-a8f0-1df9b95a0d7e)

re.InvokeCore(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.
ProcessRequests[TContext](http://ihttpapplication%601%20application/)
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished HTTP/1.1 POST https://8f3b-223-25-113-49.ngrok-free.app/
application/json 5836 - 500 0 - 3845.3013ms

Attachment (possibly relevant missing method in Task? - see highlighted in blue at bottom of image)

https://drive.google.com/file/d/1O7KL9yZsf4IKjVZNn6rwAleA6zhPjVb6/view?usp=sharing

brianpos commented 1 year ago

Check that your model is setting the httpstatus and create/update annotation correcting on the resource returned from the Create overload https://github.com/brianpos/fhir-net-web-api/blob/5d57694e7b111912dc0fe669a8cc93f1b696daaf/src/Hl7.Fhir.DemoFileSystemServer/DirectoryResourceService.cs#L107-L110

haotiansanro commented 1 year ago

Works now, thank you so much!