Darshana
Darshana

Reputation: 621

Error in binding Given Property of Patient(FHIR .net model as request model) in Core 3.1 App

I have .net core 3.1 web app,

Use HL7.Fhir.R4(1.7.0).

I use Patient model as request model,

        [HttpPost]
        public Hl7.Fhir.Model.Patient Create([FromBody] Hl7.Fhir.Model.Patient patient)
        {
            //Use the client to create patient
            return patient;
        }

Here is a request model,

{
    "ResourceType": "Patient",
    "Active": true,
    "Name": [
        {
            "Use": 1,
            "Family": "Kirk",
            "Given": [
                "Darshana"
            ]
        },
        {
            "Use": 0,
            "Given": [
                "Jim"
            ]
        }
    ],
    "Gender": 1,
    "BirthDate": "1994-08-25"
}

Getting below error:

System.InvalidCastException: Unable to cast object of type 'SelectListIterator`2[Hl7.Fhir.Model.FhirString,System.String]' to type 'System.Collections.Generic.IList`1[System.String]'.
   at System.Text.Json.JsonSerializer.ApplyValueToEnumerable[TProperty](TProperty& value, ReadStack& state)
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnReadEnumerable(ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.ReadEnumerable(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadCore(JsonReaderState& readerState, Boolean isFinalBlock, ReadOnlySpan`1 buffer, JsonSerializerOptions options, ReadStack& readStack)
   at System.Text.Json.JsonSerializer.ReadAsync[TValue](Stream utf8Json, Type returnType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
   at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
   at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)
   at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I read code, It is setting value into other object using Linq.Select where it is failing.

How can I bind Given property? Please help.

Upvotes: 1

Views: 472

Answers (1)

Darshana
Darshana

Reputation: 621

Maybe this can help someone or person who worked on it can verify or suggest if something they have,

I have created ResourceJsonInputFormatter and ResourceJsonOutputFormatter, Register formatter on startup, ultimately use FhirSerializer to serialize API request and response. Now it works fine.

Upvotes: 1

Related Questions