Add the possibility to use the serverBaseUrl to generate the url of extension, if %BASE_SERVER_URL% is found

This commit is contained in:
Sebastien Riviere 2017-01-19 14:54:17 +01:00
parent 22f796fa7c
commit cac44c736c
9 changed files with 142 additions and 10 deletions

View File

@ -931,6 +931,10 @@ public abstract class BaseParser implements IParser {
return retVal;
}
protected String getExtensionUrl(final String extensionUrl) {
return StringUtils.isNotBlank(extensionUrl) && StringUtils.isNotBlank(myServerBaseUrl) ? extensionUrl.replace("%BASE_SERVER_URL%", myServerBaseUrl) : extensionUrl;
}
/**
* Used for DSTU2 only
*/

View File

@ -1405,7 +1405,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
getErrorHandler().missingRequiredElement(new ParseLocation(parentElementName), "url");
url = null;
} else {
url = jsonElement.getAsString();
url = getExtensionUrl(jsonElement.getAsString());
}
theState.enteringNewElementExtension(null, url, theIsModifier);
for (String next : nextExtObj.keySet()) {
@ -1758,8 +1758,8 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
public int compareTo(HeldExtension theArg0) {
String url1 = myDef != null ? myDef.getExtensionUrl() : myUndeclaredExtension.getUrl();
String url2 = theArg0.myDef != null ? theArg0.myDef.getExtensionUrl() : theArg0.myUndeclaredExtension.getUrl();
url1 = defaultString(url1);
url2 = defaultString(url2);
url1 = defaultString(getExtensionUrl(url1));
url2 = defaultString(getExtensionUrl(url2));
return url1.compareTo(url2);
}
@ -1771,7 +1771,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
writeCommentsPreAndPost(myValue, theEventWriter);
JsonParser.write(theEventWriter, "url", myDef.getExtensionUrl());
JsonParser.write(theEventWriter, "url", getExtensionUrl(myDef.getExtensionUrl()));
/*
* This makes sure that even if the extension contains a reference to a contained
@ -1797,7 +1797,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser {
private void writeUndeclaredExtension(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonLikeWriter theEventWriter, IBaseExtension<?, ?> ext) throws IOException {
IBase value = ext.getValue();
String extensionUrl = ext.getUrl();
final String extensionUrl = getExtensionUrl(ext.getUrl());
theEventWriter.beginObject();

View File

@ -655,11 +655,11 @@ public class XmlParser extends BaseParser implements IParser {
String childName = childNameAndDef.getChildName();
BaseRuntimeElementDefinition<?> childDef = childNameAndDef.getChildDef();
String extensionUrl = nextChild.getExtensionUrl();
String extensionUrl = getExtensionUrl(nextChild.getExtensionUrl());
if (nextValue instanceof IBaseExtension && myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) {
// This is called for the Query resource in DSTU1 only
extensionUrl = ((IBaseExtension<?, ?>) nextValue).getUrl();
extensionUrl = getExtensionUrl(((IBaseExtension<?, ?>) nextValue).getUrl());
encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extensionUrl, theContainedResource, nextChildElem);
} else if (extensionUrl != null && childName.equals("extension") == false) {
@ -671,7 +671,7 @@ public class XmlParser extends BaseParser implements IParser {
continue;
}
}
encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extension.getUrl(), theContainedResource, nextChildElem);
encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, getExtensionUrl(extension.getUrl()), theContainedResource, nextChildElem);
} else if (nextChild instanceof RuntimeChildNarrativeDefinition && theContainedResource) {
// suppress narratives from contained resources
} else {
@ -902,7 +902,7 @@ public class XmlParser extends BaseParser implements IParser {
theEventWriter.writeAttribute("id", elementId);
}
String url = next.getUrl();
String url = getExtensionUrl(next.getUrl());
theEventWriter.writeAttribute("url", url);
if (next.getValue() != null) {

View File

@ -0,0 +1,29 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.primitive.BooleanDt;
@ResourceDef(name = "Patient", profile = "Patient")
public class CustomPatientDstu2 extends Patient {
private static final long serialVersionUID = 1L;
@Child(name = "homeless", order = 1)
@Extension(url = "%BASE_SERVER_URL%/StructureDefinition/homeless", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The patient being homeless, true if homeless")
private BooleanDt homeless;
public BooleanDt getHomeless() {
return homeless;
}
public void setHomeless(final BooleanDt homeless) {
this.homeless = homeless;
}
}

View File

@ -9,6 +9,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@ -48,6 +49,7 @@ import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum;
import ca.uhn.fhir.model.dstu2.valueset.MaritalStatusCodesEnum;
import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum;
import ca.uhn.fhir.model.dstu2.valueset.UnknownContentCodeEnum;
import ca.uhn.fhir.model.primitive.BooleanDt;
import ca.uhn.fhir.model.primitive.CodeDt;
import ca.uhn.fhir.model.primitive.DateDt;
import ca.uhn.fhir.model.primitive.DateTimeDt;
@ -1871,5 +1873,21 @@ public class JsonParserDstu2Test {
assertEquals("2011-01-01", condition.getDateRecordedElement().getValueAsString());
}
/**
* Test for the url generated based on the server config
*/
@Test
public void testGeneratedUrls() {
final IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true);
jsonParser.setServerBaseUrl("http://myserver.com");
final CustomPatientDstu2 patient = new CustomPatientDstu2();
patient.setHomeless(new BooleanDt(true));
final String parsedPatient = jsonParser.encodeResourceToString(patient);
assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/Patient"));
assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/homeless"));
}
}

View File

@ -2726,6 +2726,23 @@ public class XmlParserDstu2Test {
assertEquals("Patient", reincarnatedPatient.getId().getResourceType());
}
/**
* Test for the url generated based on the server config
*/
@Test
public void testGeneratedUrls() {
final IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
xmlParser.setServerBaseUrl("http://myserver.com");
final CustomPatientDstu2 patient = new CustomPatientDstu2();
patient.setHomeless(new BooleanDt(true));
final String parsedPatient = xmlParser.encodeResourceToString(patient);
assertTrue(parsedPatient.contains("<profile value=\"http://myserver.com/StructureDefinition/Patient\"/>"));
assertTrue(parsedPatient.contains("<extension url=\"http://myserver.com/StructureDefinition/homeless\">"));
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -0,0 +1,29 @@
package ca.uhn.fhir.parser;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import org.hl7.fhir.dstu3.model.BooleanType;
import org.hl7.fhir.dstu3.model.Patient;
@ResourceDef(name = "Patient", profile = "Patient")
public class CustomPatientDstu3 extends Patient {
private static final long serialVersionUID = 1L;
@Child(name = "homeless", order = 1)
@Extension(url = "%BASE_SERVER_URL%/StructureDefinition/homeless", definedLocally = true, isModifier = false)
@Description(shortDefinition = "The patient being homeless, true if homeless")
private BooleanType homeless;
public BooleanType getHomeless() {
return homeless;
}
public void setHomeless(final BooleanType homeless) {
this.homeless = homeless;
}
}

View File

@ -40,6 +40,7 @@ import org.hl7.fhir.dstu3.model.Attachment;
import org.hl7.fhir.dstu3.model.AuditEvent;
import org.hl7.fhir.dstu3.model.Basic;
import org.hl7.fhir.dstu3.model.Binary;
import org.hl7.fhir.dstu3.model.BooleanType;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.Bundle.BundleType;
@ -2176,6 +2177,23 @@ public class JsonParserDstu3Test {
assertTrue(result.isSuccessful());
}
/**
* Test for the url generated based on the server config
*/
@Test
public void testGeneratedUrls() {
final IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true);
jsonParser.setServerBaseUrl("http://myserver.com");
final CustomPatientDstu3 patient = new CustomPatientDstu3();
patient.setHomeless(new BooleanType(true));
final String parsedPatient = jsonParser.encodeResourceToString(patient);
assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/Patient"));
assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/homeless"));
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();

View File

@ -3139,6 +3139,23 @@ public class XmlParserDstu3Test {
}
/**
* Test for the url generated based on the server config
*/
@Test
public void testGeneratedUrls() {
final IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
xmlParser.setServerBaseUrl("http://myserver.com");
final CustomPatientDstu3 patient = new CustomPatientDstu3();
patient.setHomeless(new BooleanType(true));
final String parsedPatient = xmlParser.encodeResourceToString(patient);
assertTrue(parsedPatient.contains("<profile value=\"http://myserver.com/StructureDefinition/Patient\"/>"));
assertTrue(parsedPatient.contains("<extension url=\"http://myserver.com/StructureDefinition/homeless\">"));
}
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();