Skip to content

Commit a708949

Browse files
authored
SearchParameter validation was not being skipped on updates, even if … (hapifhir#6667)
* SearchParameter validation was not being skipped on updates, even if requested. This has been fixed * spotless fixes * removed unecessary code * reverting the code back * added external JIRA ref
1 parent a4fa654 commit a708949

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
type: fix
3+
issue: 6615
4+
jira: SMILE-9569
5+
title: "SearchParameter validation was not being skipped on updates, even if requested. This has been fixed"

hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/provider/r4/ResourceProviderR4Test.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ca.uhn.fhir.interceptor.api.Pointcut;
88
import ca.uhn.fhir.jpa.api.config.JpaStorageSettings;
99
import ca.uhn.fhir.jpa.api.dao.IFhirResourceDao;
10+
import ca.uhn.fhir.jpa.api.model.DaoMethodOutcome;
1011
import ca.uhn.fhir.jpa.dao.data.ISearchDao;
1112
import ca.uhn.fhir.jpa.entity.Search;
1213
import ca.uhn.fhir.jpa.model.dao.JpaPidFk;
@@ -19,6 +20,7 @@
1920
import ca.uhn.fhir.jpa.provider.BaseResourceProviderR4Test;
2021
import ca.uhn.fhir.jpa.search.SearchCoordinatorSvcImpl;
2122
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
23+
import ca.uhn.fhir.jpa.searchparam.submit.interceptor.SearchParamValidatingInterceptor;
2224
import ca.uhn.fhir.jpa.term.ZipCollectionBuilder;
2325
import ca.uhn.fhir.jpa.test.config.TestR4Config;
2426
import ca.uhn.fhir.jpa.util.MemoryCacheService;
@@ -62,6 +64,8 @@
6264
import ca.uhn.fhir.util.StopWatch;
6365
import ca.uhn.fhir.util.TestUtil;
6466
import ca.uhn.fhir.util.UrlUtil;
67+
import ca.uhn.test.util.LogbackTestExtension;
68+
import ca.uhn.test.util.LogbackTestExtensionAssert;
6569
import com.google.common.base.Charsets;
6670
import com.google.common.collect.Lists;
6771
import jakarta.annotation.Nonnull;
@@ -170,16 +174,21 @@
170174
import org.junit.jupiter.api.Disabled;
171175
import org.junit.jupiter.api.Nested;
172176
import org.junit.jupiter.api.Test;
177+
import org.junit.jupiter.api.extension.RegisterExtension;
173178
import org.junit.jupiter.params.ParameterizedTest;
174179
import org.junit.jupiter.params.provider.Arguments;
175180
import org.junit.jupiter.params.provider.CsvSource;
176181
import org.junit.jupiter.params.provider.MethodSource;
177182
import org.junit.jupiter.params.provider.ValueSource;
183+
import ch. qos. logback. classic.Level;
178184
import org.springframework.beans.factory.annotation.Autowired;
179185
import org.springframework.test.util.AopTestUtils;
180186
import org.springframework.transaction.TransactionStatus;
181187
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
182188
import org.springframework.transaction.support.TransactionTemplate;
189+
import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
190+
import ca.uhn.fhir.rest.client.apache.ResourceEntity;
191+
183192

184193
import java.io.BufferedReader;
185194
import java.io.IOException;
@@ -222,6 +231,9 @@ public class ResourceProviderR4Test extends BaseResourceProviderR4Test {
222231
@Autowired
223232
private ISearchDao mySearchEntityDao;
224233

234+
@RegisterExtension
235+
public LogbackTestExtension myLogbackTestExtension = new LogbackTestExtension(SearchParamValidatingInterceptor.class, Level.WARN);
236+
225237
@Override
226238
@AfterEach
227239
public void after() throws Exception {
@@ -264,7 +276,31 @@ public void before() throws Exception {
264276
myStorageSettings.setSearchPreFetchThresholds(new JpaStorageSettings().getSearchPreFetchThresholds());
265277
}
266278

279+
@Test
280+
public void testSearchParameterValidation() {
281+
// setup
282+
SystemRequestDetails requestDetails = new SystemRequestDetails();
283+
requestDetails.getUserData().put(SearchParamValidatingInterceptor.SKIP_VALIDATION, true);
284+
285+
SearchParameter sp = new SearchParameter();
286+
sp.setUrl("http://example.com/name");
287+
sp.setId("name");
288+
sp.setCode("name");
289+
sp.setType(Enumerations.SearchParamType.STRING);
290+
sp.setStatus(Enumerations.PublicationStatus.RETIRED);
291+
sp.addBase("Patient");
292+
sp.setExpression("Patient.name");
293+
294+
// test
295+
DaoMethodOutcome outcome = mySearchParameterDao.update(sp, requestDetails);
267296

297+
myCaptureQueriesListener.clear();
298+
sp.setId(outcome.getId());
299+
sp.setStatus(Enumerations.PublicationStatus.ACTIVE);
300+
mySearchParameterDao.update(sp, requestDetails);
301+
302+
LogbackTestExtensionAssert.assertThat(myLogbackTestExtension).hasWarnMessage("Skipping validation of submitted SearchParameter because " + SearchParamValidatingInterceptor.SKIP_VALIDATION + " flag is true");
303+
}
268304

269305
@Test
270306
public void testParameterWithNoValueThrowsError_InvalidChainOnCustomSearch() throws IOException {

hapi-fhir-storage/src/main/java/ca/uhn/fhir/jpa/searchparam/submit/interceptor/SearchParamValidatingInterceptor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import jakarta.annotation.Nullable;
4141
import org.hl7.fhir.instance.model.api.IBaseExtension;
4242
import org.hl7.fhir.instance.model.api.IBaseResource;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4345
import org.springframework.beans.factory.annotation.Autowired;
4446

4547
import java.util.HashSet;
@@ -54,6 +56,8 @@
5456
@Interceptor
5557
public class SearchParamValidatingInterceptor {
5658

59+
private static final Logger logger = LoggerFactory.getLogger(SearchParamValidatingInterceptor.class);
60+
5761
public static final String SEARCH_PARAM = "SearchParameter";
5862
public static final String SKIP_VALIDATION = SearchParamValidatingInterceptor.class.getName() + ".SKIP_VALIDATION";
5963

@@ -142,6 +146,19 @@ public void validateSearchParamOnUpdate(IBaseResource theResource, RequestDetail
142146
if (isNotSearchParameterResource(theResource)) {
143147
return;
144148
}
149+
150+
// avoid a loop when loading our hard-coded core FhirContext SearchParameters
151+
// skip Search Param validation if been set in the request
152+
boolean isStartup = theRequestDetails != null
153+
&& Boolean.TRUE == theRequestDetails.getUserData().get(SKIP_VALIDATION);
154+
if (isStartup) {
155+
logger.warn(
156+
"Skipping validation of submitted SearchParameter because {} flag is {}",
157+
SKIP_VALIDATION,
158+
Boolean.TRUE);
159+
return;
160+
}
161+
145162
RuntimeSearchParam runtimeSearchParam = mySearchParameterCanonicalizer.canonicalizeSearchParameter(theResource);
146163
if (runtimeSearchParam == null) {
147164
return;

0 commit comments

Comments
 (0)