Skip to content

Commit

Permalink
Fix #335 (also fixes #336) - Correctly parse and serialize IDs on ele…
Browse files Browse the repository at this point in the history
…ments within a resource
  • Loading branch information
jamesagnew committed Apr 17, 2016
1 parent 3f2b5fd commit 1af65ff
Show file tree
Hide file tree
Showing 13 changed files with 669 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void readExternal(ObjectInput theIn) throws IOException, ClassNotFoundExc
}

@Override
public IPrimitiveType<T> setValue(T theValue) throws DataFormatException {
public BasePrimitive<T> setValue(T theValue) throws DataFormatException {
myCoercedValue = theValue;
updateStringValue();
return this;
Expand Down
42 changes: 28 additions & 14 deletions hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseCoding;
import org.hl7.fhir.instance.model.api.IBaseElement;
import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
import org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions;
import org.hl7.fhir.instance.model.api.IBaseMetaType;
Expand All @@ -65,6 +66,7 @@
import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IIdentifiableElement;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
Expand Down Expand Up @@ -105,10 +107,10 @@ public BaseParser(FhirContext theContext, IParserErrorHandler theParserErrorHand
}

protected Iterable<CompositeChildElement> compositeChildIterator(IBase theCompositeElement, final boolean theContainedResource, final CompositeChildElement theParent) {

BaseRuntimeElementCompositeDefinition<?> elementDef = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition(theCompositeElement.getClass());
final List<BaseRuntimeChildDefinition> children = elementDef.getChildrenAndExtension();

return new Iterable<BaseParser.CompositeChildElement>() {
@Override
public Iterator<CompositeChildElement> iterator() {
Expand Down Expand Up @@ -143,9 +145,9 @@ public boolean hasNext() {
/*
* There are lots of reasons we might skip encoding a particular child
*/
// if (myNext.getDef().getElementName().equals("extension") || myNext.getDef().getElementName().equals("modifierExtension")) {
// myNext = null;
// } else
// if (myNext.getDef().getElementName().equals("extension") || myNext.getDef().getElementName().equals("modifierExtension")) {
// myNext = null;
// } else
if (myNext.getDef().getElementName().equals("id")) {
myNext = null;
} else if (!myNext.shouldBeEncoded()) {
Expand Down Expand Up @@ -377,7 +379,7 @@ public final void encodeResourceToWriter(IBaseResource theResource, Writer theWr
if (theResource.getStructureFhirVersionEnum() != myContext.getVersion().getVersion()) {
throw new IllegalArgumentException("This parser is for FHIR version " + myContext.getVersion().getVersion() + " - Can not encode a structure for version " + theResource.getStructureFhirVersionEnum());
}

doEncodeResourceToWriter(theResource, theWriter);
}

Expand Down Expand Up @@ -411,6 +413,18 @@ protected IIdType fixContainedResourceId(String theValue) {
return retVal;
}

protected String getCompositeElementId(IBase theElement) {
String elementId = null;
if (!(theElement instanceof IBaseResource)) {
if (theElement instanceof IBaseElement) {
elementId = ((IBaseElement) theElement).getId();
} else if (theElement instanceof IIdentifiableElement) {
elementId = ((IIdentifiableElement) theElement).getElementSpecificId();
}
}
return elementId;
}

ContainedResources getContainedResources() {
return myContainedResources;
}
Expand Down Expand Up @@ -552,14 +566,14 @@ public Bundle parseBundle(String theXml) throws ConfigurationException, DataForm

@Override
public <T extends IBaseResource> T parseResource(Class<T> theResourceType, Reader theReader) throws DataFormatException {

if (theResourceType != null) {
RuntimeResourceDefinition def = myContext.getResourceDefinition(theResourceType);
if (def.getStructureVersion() != myContext.getVersion().getVersion()) {
throw new IllegalArgumentException("This parser is for FHIR version " + myContext.getVersion().getVersion() + " - Can not parse a structure for version " + def.getStructureVersion());
}
}

T retVal = doParseResource(theResourceType, theReader);

RuntimeResourceDefinition def = myContext.getResourceDefinition(retVal);
Expand Down Expand Up @@ -684,32 +698,32 @@ protected List<? extends IBase> preProcessValues(BaseRuntimeChildDefinition meta

@SuppressWarnings("unchecked")
List<IBase> retVal = (List<IBase>) theValues;

for (int i = 0; i < retVal.size(); i++) {
IBase next = retVal.get(i);

/*
* If we have automatically contained any resources via
* their references, this ensures that we output the new
* local reference
*/
if (next instanceof IBaseReference) {
IBaseReference nextRef = (IBaseReference)next;
IBaseReference nextRef = (IBaseReference) next;
String refText = determineReferenceText(nextRef);
if (!StringUtils.equals(refText, nextRef.getReferenceElement().getValue())) {

if (retVal == theValues) {
retVal = new ArrayList<IBase>(theValues);
}
IBaseReference newRef = (IBaseReference) myContext.getElementDefinition(nextRef.getClass()).newInstance();
myContext.newTerser().cloneInto(nextRef, newRef, true);
newRef.setReference(refText);
retVal.set(i, newRef);

}
}
}

return retVal;
}

Expand Down

0 comments on commit 1af65ff

Please sign in to comment.