From 31d61100db41590b9760daf9e0942ad553ff69e2 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Mon, 22 Dec 2014 22:37:24 -0500 Subject: [PATCH] Fix #67 - Correctly parse local IDs in IdDt even if they look like real IDs --- .../ca/uhn/fhir/model/primitive/IdDt.java | 18 ++++---- .../org.eclipse.wst.common.component | 3 ++ .../ca/uhn/fhir/model/primitive/IdDtTest.java | 46 +++++++++++++++++++ src/changes/changes.xml | 7 +++ 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java index cd3c955b640..c0e25cd1c3a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java @@ -283,7 +283,8 @@ public class IdDt implements IPrimitiveDatatype { b.append('/'); b.append(myUnqualifiedVersionId); } - myValue = b.toString(); + String value = b.toString(); + myValue = value; } return myValue; } @@ -384,10 +385,17 @@ public class IdDt implements IPrimitiveDatatype { myValue = theValue; myHaveComponentParts = false; if (StringUtils.isBlank(theValue)) { + myBaseUrl = null; myValue = null; myUnqualifiedId = null; myUnqualifiedVersionId = null; myResourceType = null; + } else if (theValue.charAt(0)== '#') { + myValue = theValue; + myUnqualifiedId = theValue; + myUnqualifiedVersionId=null; + myResourceType = null; + myHaveComponentParts = true; } else { int vidIndex = theValue.indexOf("/_history/"); int idIndex; @@ -452,13 +460,7 @@ public class IdDt implements IPrimitiveDatatype { } public IdDt toVersionless() { - String value = getValue(); - int i = value.indexOf(Constants.PARAM_HISTORY); - if (i > 1) { - return new IdDt(value.substring(0, i - 1)); - } else { - return this; - } + return new IdDt(getBaseUrl(), getResourceType(), getIdPart(), null); } public IdDt withResourceType(String theResourceName) { diff --git a/hapi-fhir-jpaserver-uhnfhirtest/.settings/org.eclipse.wst.common.component b/hapi-fhir-jpaserver-uhnfhirtest/.settings/org.eclipse.wst.common.component index 124f33524d7..7d2c3dbcedf 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/.settings/org.eclipse.wst.common.component +++ b/hapi-fhir-jpaserver-uhnfhirtest/.settings/org.eclipse.wst.common.component @@ -14,6 +14,9 @@ uses + + uses + consumes diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/primitive/IdDtTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/primitive/IdDtTest.java index aca7aa2519d..ab21c9aeeaf 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/primitive/IdDtTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/model/primitive/IdDtTest.java @@ -26,6 +26,43 @@ public class IdDtTest { } + /** + * See #67 + */ + @Test + public void testComplicatedLocal() { + IdDt id = new IdDt("#Patient/cid:Patient-72/_history/1"); + assertTrue(id.isLocal()); + assertNull(id.getBaseUrl()); + assertNull(id.getResourceType()); + assertNull(id.getVersionIdPart()); + assertEquals("#Patient/cid:Patient-72/_history/1", id.getIdPart()); + + IdDt id2 = new IdDt("#Patient/cid:Patient-72/_history/1"); + assertEquals(id, id2); + + id2 = id2.toUnqualified(); + assertTrue(id2.isLocal()); + assertNull(id2.getBaseUrl()); + assertNull(id2.getResourceType()); + assertNull(id2.getVersionIdPart()); + assertEquals("#Patient/cid:Patient-72/_history/1", id2.getIdPart()); + + id2 = id2.toVersionless(); + assertTrue(id2.isLocal()); + assertNull(id2.getBaseUrl()); + assertNull(id2.getResourceType()); + assertNull(id2.getVersionIdPart()); + assertEquals("#Patient/cid:Patient-72/_history/1", id2.getIdPart()); + + id2 = id2.toUnqualifiedVersionless(); + assertTrue(id2.isLocal()); + assertNull(id2.getBaseUrl()); + assertNull(id2.getResourceType()); + assertNull(id2.getVersionIdPart()); + assertEquals("#Patient/cid:Patient-72/_history/1", id2.getIdPart()); + } + @Test public void testDetermineBase() { @@ -80,6 +117,15 @@ public class IdDtTest { } + + @Test + public void testViewMethods() { + IdDt i = new IdDt("http://foo/fhir/Organization/123/_history/999"); + assertEquals("Organization/123/_history/999", i.toUnqualified().getValue()); + assertEquals("http://foo/fhir/Organization/123", i.toVersionless().getValue()); + assertEquals("Organization/123", i.toUnqualifiedVersionless().getValue()); + } + @Test public void testParseValueWithVersion() { Patient patient = new Patient(); diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 0e95f8d4fd2..ae2160e7767 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -16,6 +16,13 @@ (e.g. because they don't have a no-argument constructor) in order to avoid failing later + + IdDt failed to recognize local identifiers containing fragments that look like + real identifiers as being local identifiers even though they started with '#'. + For example, a local resource reference of "#aa/_history/aa" would be incorrectly + parsed as a non-local reference. + Thanks to Mohammad Jafari for reporting! +