Always include SUBSETTED tag when _elements is used on server

This commit is contained in:
jamesagnew 2016-07-18 07:05:11 -04:00
parent 196c11bfa2
commit 5073692eda
6 changed files with 196 additions and 120 deletions

View File

@ -667,16 +667,16 @@ public abstract class BaseParser implements IParser {
return parseTagList(new StringReader(theString));
}
protected List<? extends IBase> preProcessValues(BaseRuntimeChildDefinition metaChildUncast, IBaseResource theResource, List<? extends IBase> theValues) {
protected List<? extends IBase> preProcessValues(BaseRuntimeChildDefinition theMetaChildUncast, IBaseResource theResource, List<? extends IBase> theValues) {
if (myContext.getVersion().getVersion().isRi()) {
/*
* If we're encoding the meta tag, we do some massaging of the meta values before
* encoding. Buf if there is no meta element at all, we create one since we're possibly going to be
* encoding. But if there is no meta element at all, we create one since we're possibly going to be
* adding things to it
*/
if (theValues.isEmpty() && metaChildUncast.getElementName().equals("meta")) {
BaseRuntimeElementDefinition<?> metaChild = metaChildUncast.getChildByName("meta");
if (theValues.isEmpty() && theMetaChildUncast.getElementName().equals("meta")) {
BaseRuntimeElementDefinition<?> metaChild = theMetaChildUncast.getChildByName("meta");
if (IBaseMetaType.class.isAssignableFrom(metaChild.getImplementingClass())) {
IBaseMetaType newType = (IBaseMetaType) metaChild.newInstance();
theValues = Collections.singletonList(newType);
@ -842,7 +842,7 @@ public abstract class BaseParser implements IParser {
}
protected boolean shouldAddSubsettedTag() {
return isSummaryMode() || isSuppressNarratives();
return isSummaryMode() || isSuppressNarratives() || getEncodeElements() != null;
}
protected boolean shouldEncodeResourceId(IBaseResource theResource) {

View File

@ -590,7 +590,6 @@ public class XmlParser extends BaseParser implements IParser {
}
@SuppressWarnings("rawtypes")
private void encodeCompositeElementToStreamWriter(IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, boolean theContainedResource, CompositeChildElement theParent)
throws XMLStreamException, DataFormatException {

View File

@ -108,6 +108,10 @@ public class ElementsParameter implements IParameter {
if (retVal.isEmpty()) {
return null;
}
// Always include the meta element even for subsetted values
retVal.add("meta");
return retVal;
} else {
return null;

View File

@ -0,0 +1,183 @@
package ca.uhn.fhir.rest.server;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.annotation.OptionalParam;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.param.StringParam;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.VersionUtil;
public class MetadataDstu3Test {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu3();
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(MetadataDstu3Test.class);
@Test
public void testSummary() throws Exception {
String output;
// With
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_summary=true&_pretty=true");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<Conformance"));
assertThat(output, stringContainsInOrder("<meta>", "SUBSETTED", "</meta>"));
assertThat(output, not(stringContainsInOrder("searchParam")));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
// Without
httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_pretty=true");
status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<Conformance"));
assertThat(output, not(stringContainsInOrder("<meta>", "SUBSETTED", "</meta>")));
assertThat(output, stringContainsInOrder("searchParam"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testElements() throws Exception {
String output;
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata?_elements=fhirVersion&_pretty=true");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
ourLog.info(output);
assertThat(output, containsString("<Conformance"));
assertThat(output, stringContainsInOrder("<meta>", "SUBSETTED", "</meta>"));
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@Test
public void testHttpMethods() throws Exception {
String output;
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata");
CloseableHttpResponse status = ourClient.execute(httpPost);
try {
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(output, containsString("<Conformance"));
assertEquals("HAPI FHIR " + VersionUtil.getVersion() + " REST Server (FHIR Server; FHIR " + FhirVersionEnum.DSTU3.getFhirVersionString() + "/DSTU3)", status.getFirstHeader("X-Powered-By").getValue());
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
try {
httpPost = new HttpPost("http://localhost:" + ourPort + "/metadata");
status = ourClient.execute(httpPost);
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(405, status.getStatusLine().getStatusCode());
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"/metadata request must use HTTP GET\"/></issue></OperationOutcome>", output);
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
/*
* There is no @read on the RP below, so this should fail. Otherwise it
* would be interpreted as a read on ID "metadata"
*/
try {
httpPost = new HttpGet("http://localhost:" + ourPort + "/Patient/metadata");
status = ourClient.execute(httpPost);
output = IOUtils.toString(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
} finally {
IOUtils.closeQuietly(status.getEntity().getContent());
}
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
servlet = new RestfulServer(ourCtx);
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
@Search
public List<Patient> search(@OptionalParam(name="foo") StringParam theFoo) {
throw new UnsupportedOperationException();
}
@Validate()
public MethodOutcome validate(@ResourceParam Patient theResource) {
return new MethodOutcome();
}
}
}

View File

@ -1,114 +0,0 @@
package ca.uhn.fhir.rest.server;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.hl7.fhir.dstu3.model.Patient;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.FhirVersionEnum;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.annotation.Validate;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.util.PortUtil;
import ca.uhn.fhir.util.TestUtil;
import ca.uhn.fhir.util.VersionUtil;
public class MetadataDstu3Test2 {
private static CloseableHttpClient ourClient;
private static final FhirContext ourCtx = FhirContext.forDstu3();
private static int ourPort;
private static Server ourServer;
private static RestfulServer servlet;
@Test
public void testHttpMethods() throws Exception {
String output;
HttpRequestBase httpPost = new HttpGet("http://localhost:" + ourPort + "/metadata");
HttpResponse status = ourClient.execute(httpPost);
output = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(200, status.getStatusLine().getStatusCode());
assertThat(output, containsString("<Conformance"));
assertEquals("HAPI FHIR " + VersionUtil.getVersion() + " REST Server (FHIR Server; FHIR " + FhirVersionEnum.DSTU3.getFhirVersionString() + "/DSTU3)", status.getFirstHeader("X-Powered-By").getValue());
httpPost = new HttpPost("http://localhost:" + ourPort + "/metadata");
status = ourClient.execute(httpPost);
output = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(405, status.getStatusLine().getStatusCode());
assertEquals("<OperationOutcome xmlns=\"http://hl7.org/fhir\"><issue><severity value=\"error\"/><code value=\"processing\"/><diagnostics value=\"/metadata request must use HTTP GET\"/></issue></OperationOutcome>", output);
/*
* There is no @read on the RP below, so this should fail. Otherwise it
* would be interpreted as a read on ID "metadata"
*/
httpPost = new HttpGet("http://localhost:" + ourPort + "/Patient/metadata");
status = ourClient.execute(httpPost);
output = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
assertEquals(400, status.getStatusLine().getStatusCode());
}
@AfterClass
public static void afterClassClearContext() throws Exception {
ourServer.stop();
TestUtil.clearAllStaticFieldsForUnitTest();
}
@BeforeClass
public static void beforeClass() throws Exception {
ourPort = PortUtil.findFreePort();
ourServer = new Server(ourPort);
DummyPatientResourceProvider patientProvider = new DummyPatientResourceProvider();
ServletHandler proxyHandler = new ServletHandler();
servlet = new RestfulServer(ourCtx);
servlet.setResourceProviders(patientProvider);
ServletHolder servletHolder = new ServletHolder(servlet);
proxyHandler.addServletWithMapping(servletHolder, "/*");
ourServer.setHandler(proxyHandler);
ourServer.start();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(connectionManager);
ourClient = builder.build();
}
public static class DummyPatientResourceProvider implements IResourceProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
@Validate()
public MethodOutcome validate(@ResourceParam Patient theResource) {
return new MethodOutcome();
}
}
}

View File

@ -58,6 +58,10 @@
Fluent client should ignore parameter values which are null instead of including
them as <![CDATA[<code>?foo=null</code>]]>
</action>
<action type="fix">
When using <![CDATA[<code>_elements</code>]]> parameter on server, the server was not
automatically adding the <![CDATA[<code>SUBSETTED</code>]]> tag as it should
</action>
</release>
<release version="1.6" date="2016-07-07">
<action type="fix">