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 008b7fb3e0d..864f8e8b6e9 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 @@ -305,11 +305,16 @@ public class IdDt extends UriDt implements /*IPrimitiveDatatype, */IIdTy b.append(myResourceType); } - if (b.length() > 0) { + if (b.length() > 0 && isNotBlank(myUnqualifiedId)) { + b.append('/'); + } + + if (isNotBlank(myUnqualifiedId)) { + b.append(myUnqualifiedId); + } else if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); } - b.append(myUnqualifiedId); if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); b.append(Constants.PARAM_HISTORY); @@ -527,7 +532,21 @@ public class IdDt extends UriDt implements /*IPrimitiveDatatype, */IIdTy if (typeIndex == -1) { myResourceType = theValue.substring(0, idIndex); } else { - myResourceType = theValue.substring(typeIndex + 1, idIndex); + if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) { + typeIndex = theValue.indexOf('/', typeIndex + 1); + } + if (typeIndex >= idIndex) { + // e.g. http://example.org/foo + // 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id. + // Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly + // recreate the url + myResourceType = myUnqualifiedId; + myUnqualifiedId = null; + super.setValue(null); + myHaveComponentParts = true; + } else { + myResourceType = theValue.substring(typeIndex + 1, idIndex); + } if (typeIndex > 4) { myBaseUrl = theValue.substring(0, typeIndex); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceLink.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceLink.java index 5209106280d..174b7d3ed81 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceLink.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceLink.java @@ -197,8 +197,16 @@ public class ResourceLink implements Serializable { public void setTargetResourceUrl(IIdType theTargetResourceUrl) { Validate.isTrue(theTargetResourceUrl.hasBaseUrl()); Validate.isTrue(theTargetResourceUrl.hasResourceType()); - Validate.isTrue(theTargetResourceUrl.hasIdPart()); - + + if (theTargetResourceUrl.hasIdPart()) { + // do nothing + } else { + // Must have set an url like http://example.org/something + // We treat 'something' as the resource type because of fix for #659. Prior to #659 fix, 'something' was + // treated as the id and 'example.org' was treated as the resource type + // TODO: log a warning? + } + myTargetResourceType = theTargetResourceUrl.getResourceType(); myTargetResourceUrl = theTargetResourceUrl.getValue(); } diff --git a/hapi-fhir-structures-dstu2.1/src/main/java/org/hl7/fhir/dstu2016may/model/IdType.java b/hapi-fhir-structures-dstu2.1/src/main/java/org/hl7/fhir/dstu2016may/model/IdType.java index 8a6df569886..f5e4ee31a06 100644 --- a/hapi-fhir-structures-dstu2.1/src/main/java/org/hl7/fhir/dstu2016may/model/IdType.java +++ b/hapi-fhir-structures-dstu2.1/src/main/java/org/hl7/fhir/dstu2016may/model/IdType.java @@ -358,11 +358,16 @@ public final class IdType extends UriType implements IPrimitiveType, IId b.append(myResourceType); } - if (b.length() > 0) { + if (b.length() > 0 && isNotBlank(myUnqualifiedId)) { + b.append('/'); + } + + if (isNotBlank(myUnqualifiedId)) { + b.append(myUnqualifiedId); + } else if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); } - b.append(myUnqualifiedId); if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); b.append("_history"); @@ -554,7 +559,21 @@ public final class IdType extends UriType implements IPrimitiveType, IId if (typeIndex == -1) { myResourceType = theValue.substring(0, idIndex); } else { - myResourceType = theValue.substring(typeIndex + 1, idIndex); + if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) { + typeIndex = theValue.indexOf('/', typeIndex + 1); + } + if (typeIndex >= idIndex) { + // e.g. http://example.org/foo + // 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id. + // Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly + // recreate the url + myResourceType = myUnqualifiedId; + myUnqualifiedId = null; + super.setValue(null); + myHaveComponentParts = true; + } else { + myResourceType = theValue.substring(typeIndex + 1, idIndex); + } if (typeIndex > 4) { myBaseUrl = theValue.substring(0, typeIndex); diff --git a/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/IdType.java b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/IdType.java index 02535643be3..530d9dfa284 100644 --- a/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/IdType.java +++ b/hapi-fhir-structures-dstu3/src/main/java/org/hl7/fhir/dstu3/model/IdType.java @@ -357,11 +357,16 @@ public final class IdType extends UriType implements IPrimitiveType, IId b.append(myResourceType); } - if (b.length() > 0) { + if (b.length() > 0 && isNotBlank(myUnqualifiedId)) { + b.append('/'); + } + + if (isNotBlank(myUnqualifiedId)) { + b.append(myUnqualifiedId); + } else if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); } - b.append(myUnqualifiedId); if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); b.append("_history"); @@ -553,7 +558,21 @@ public final class IdType extends UriType implements IPrimitiveType, IId if (typeIndex == -1) { myResourceType = theValue.substring(0, idIndex); } else { - myResourceType = theValue.substring(typeIndex + 1, idIndex); + if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) { + typeIndex = theValue.indexOf('/', typeIndex + 1); + } + if (typeIndex >= idIndex) { + // e.g. http://example.org/foo + // 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id. + // Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly + // recreate the url + myResourceType = myUnqualifiedId; + myUnqualifiedId = null; + super.setValue(null); + myHaveComponentParts = true; + } else { + myResourceType = theValue.substring(typeIndex + 1, idIndex); + } if (typeIndex > 4) { myBaseUrl = theValue.substring(0, typeIndex); diff --git a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/IdType.java b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/IdType.java index 5239236c4ec..06b82519282 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/IdType.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/main/java/org/hl7/fhir/instance/model/IdType.java @@ -384,11 +384,16 @@ public final class IdType extends UriType implements IPrimitiveType, IId b.append(myResourceType); } - if (b.length() > 0) { + if (b.length() > 0 && isNotBlank(myUnqualifiedId)) { + b.append('/'); + } + + if (isNotBlank(myUnqualifiedId)) { + b.append(myUnqualifiedId); + } else if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); } - b.append(myUnqualifiedId); if (isNotBlank(myUnqualifiedVersionId)) { b.append('/'); b.append("_history"); @@ -571,7 +576,21 @@ public final class IdType extends UriType implements IPrimitiveType, IId if (typeIndex == -1) { myResourceType = theValue.substring(0, idIndex); } else { - myResourceType = theValue.substring(typeIndex + 1, idIndex); + if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) { + typeIndex = theValue.indexOf('/', typeIndex + 1); + } + if (typeIndex >= idIndex) { + // e.g. http://example.org/foo + // 'foo' was the id but we're making that the resource type. Nullify the id part because we don't have an id. + // Also set null value to the super.setValue() and enable myHaveComponentParts so it forces getValue() to properly + // recreate the url + myResourceType = myUnqualifiedId; + myUnqualifiedId = null; + super.setValue(null); + myHaveComponentParts = true; + } else { + myResourceType = theValue.substring(typeIndex + 1, idIndex); + } if (typeIndex > 4) { myBaseUrl = theValue.substring(0, typeIndex);