Search code examples
c#hl7-fhir

Returning a CareTeam as Bundle from a REST endpoint results in Bundle/null


I'm creating an endpoint that will return a Careteam as a Bundle for a FHIR client. So this endpoint will consult its database and from that result result it will create a Bundle:

public class BundleFactory : IBundleFactory

{ public Bundle CreateBundle(SearchResult result) { var bundle = new Bundle(); bundle.Type = Bundle.BundleType.Searchset;

    var entry = new Bundle.EntryComponent();

    CareTeam careTeam = new CareTeam();

    var contained = new List<Resource>();

    int reference = 1;
    foreach (var persoonDto in resultaat.Resultaten.Where(p => p.SpreekuurIdpId.HasValue))
    {
        var participant = new CareTeam.ParticipantComponent
        {
            Member = new ResourceReference
            {
                Reference = reference.ToString()
            }
        };
        careTeam.Participant.Add(participant);

        reference++;
    }

    careTeam.Contained = contained;

    entry.Resource = careteam;

    bundle.Entry.Add(entry);

    return bundle;
}

In my integration tests the resulting JSON looks like this {"Bundle/null"} and parsing it back to a Bundle will result in an exception. I'm probably missing some properties I need to set on the Bundle.


Solution

  • The {"Bundle/null"} you mention is probably not your JSON output, but what you see in your debug window when you look at the Bundle. If you do not set the Id field, that is what it shows.

    In your example code you have probably deleted some actual parts. If I run this (with some changes to the foreach), the JSON output does not make much sense: each reference is just a number, instead of an actual FHIR reference. But it results in a correct Bundle when I serialize to JSON and parse that back again.

    What is the reason for the list of resources that stays empty and then is added as contained resource? Since it doesn't do anything now, my code works. But I'm thinking that this part of your code could lead to errors. Why not add the resources to the Bundle, instead of using contained? Why create a Bundle anyway, if the client wants to get a CareTeam?