diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java index 42ce4a4677a..1cc7ed4fbdb 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/ParserState.java @@ -25,6 +25,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotEmpty; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -1870,13 +1871,17 @@ class ParserState { } else if (theLocalPart.equals("profile")) { @SuppressWarnings("unchecked") List profiles = (List) myMap.get(ResourceMetadataKeyEnum.PROFILES); - if (profiles == null) { - profiles = new ArrayList(); - myMap.put(ResourceMetadataKeyEnum.PROFILES, profiles); + List newProfiles; + if (profiles != null) { + newProfiles = new ArrayList(profiles.size() + 1); + newProfiles.addAll(profiles); + } else { + newProfiles = new ArrayList(1); } IdDt profile = new IdDt(); push(new PrimitiveState(getPreResourceState(), profile)); - profiles.add(profile); + newProfiles.add(profile); + myMap.put(ResourceMetadataKeyEnum.PROFILES, Collections.unmodifiableList(newProfiles)); } else if (theLocalPart.equals("tag")) { TagList tagList = (TagList) myMap.get(ResourceMetadataKeyEnum.TAG_LIST); if (tagList == null) {