Is it possible to have List of non-contained resources inside another resource?

In my current FHIR implementation for 834 x12 , I have a requirement while creating my data model that list of a resource should reside inside another resource , but we want that list of resources to not be contained resources as there is a need for that resources to exist independently so that they can be later enquired by direct GET calls .
To give an example , here I am trying to have list of Domain Resource - Coverage inside another resource MHEnrollmentRequest which extends domain resource EnrollmentRequest :

public class MHEnrollmentRequest extends EnrollmentRequest {

  @Child(name = "coverages")
  @Description(shortDefinition = "X12 uses more than 1 coverage, so we need some extras")
  private List<Coverage> coverages;

When using IParser’s method encodeResourceToString , it lets me have one coverage in above list of coverages and I can see json printed fine for EnrollmentRequest, but moment second coverage is added to list of coverages , parser starts failing with this error -
Exception in thread “main” java.lang.Error: Encountered IOException during write to string - This should not happen!
at ca.uhn.fhir.parser.BaseParser.encodeResourceToString(

Is there a way to have list of independent resources inside another resource in FHIR ? Any other suggestion that doesn’t include contained resources will be very useful !

Thank you

First, be cautious about using Resource.contained. One resource should only be nested inside another when it has no independent identity and must always be created, updated, and otherwise manipulated as part of the container (for both originator and recipient). It’s not a way of packaging information for easy transmission.

All resources that are contained must either be referenced by or reference the containing resource.

Finally, you can’t change the cardinality of references that exist in a resource. EnrollmentRequest references a single Coverage. If you want to deal with multiple coverages in a single enrollment request, you’ll have to use extension - and you should consult the community before doing so. My guess is that the community will say that you should have a distinct EnrollmentRequest for each Coverage (as each can presumably be accepted or denied independently).

1 Like