diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index c9d433a89b8..92b6ca02688 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -204,31 +204,6 @@ public abstract class BaseParser implements IParser { } private void containResourcesForEncoding(ContainedResources theContained, IBaseResource theResource, IBaseResource theTarget) { - - if (theTarget instanceof IResource) { - List containedResources = ((IResource) theTarget).getContained().getContainedResources(); - for (IResource next : containedResources) { - String nextId = next.getId().getValue(); - if (StringUtils.isNotBlank(nextId)) { - if (!nextId.startsWith("#")) { - nextId = '#' + nextId; - } - theContained.getExistingIdToContainedResource().put(nextId, next); - } - } - } else if (theTarget instanceof IDomainResource) { - List containedResources = ((IDomainResource) theTarget).getContained(); - for (IAnyResource next : containedResources) { - String nextId = next.getIdElement().getValue(); - if (StringUtils.isNotBlank(nextId)) { - if (!nextId.startsWith("#")) { - nextId = '#' + nextId; - } - theContained.getExistingIdToContainedResource().put(nextId, next); - } - } - } - List allReferences = getAllBaseReferences(theResource); for (IBaseReference next : allReferences) { IBaseResource resource = next.getResource(); @@ -268,6 +243,31 @@ public abstract class BaseParser implements IParser { protected void containResourcesForEncoding(IBaseResource theResource) { ContainedResources contained = new ContainedResources(); + + if (theResource instanceof IResource) { + List containedResources = ((IResource) theResource).getContained().getContainedResources(); + for (IResource next : containedResources) { + String nextId = next.getId().getValue(); + if (StringUtils.isNotBlank(nextId)) { + if (!nextId.startsWith("#")) { + nextId = '#' + nextId; + } + contained.getExistingIdToContainedResource().put(nextId, next); + } + } + } else if (theResource instanceof IDomainResource) { + List containedResources = ((IDomainResource) theResource).getContained(); + for (IAnyResource next : containedResources) { + String nextId = next.getIdElement().getValue(); + if (StringUtils.isNotBlank(nextId)) { + if (!nextId.startsWith("#")) { + nextId = '#' + nextId; + } + contained.getExistingIdToContainedResource().put(nextId, next); + } + } + } + containResourcesForEncoding(contained, theResource, theResource); contained.assignIdsToContainedResources(); myContainedResources = contained;