Skip to content

Commit

Permalink
fix(fhir): make sure ECL values are always decoded before used...
Browse files Browse the repository at this point in the history
...in implicit value set computation
  • Loading branch information
cmark committed May 7, 2024
1 parent 3b62aaf commit 1667d1e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.b2international.snowowl.fhir.core.request.valueset;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

import org.elasticsearch.common.Strings;

import com.b2international.commons.CompareUtils;
Expand All @@ -35,6 +35,7 @@
import com.b2international.snowowl.core.request.SearchResourceRequest;
import com.b2international.snowowl.fhir.core.codesystems.FilterOperator;
import com.b2international.snowowl.fhir.core.codesystems.PublicationStatus;
import com.b2international.snowowl.fhir.core.exceptions.BadRequestException;
import com.b2international.snowowl.fhir.core.model.ResourceResponseEntry;
import com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem;
import com.b2international.snowowl.fhir.core.model.dt.Uri;
Expand All @@ -50,6 +51,9 @@
import com.google.common.collect.ImmutableList;
import com.google.common.hash.Hashing;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

/**
* @since 8.0
*/
Expand Down Expand Up @@ -88,7 +92,10 @@ public ValueSet execute(ServiceProvider context) {
} catch (NotFoundException e) {
// if there is no Value Set present for the given URL, then try to parse the URL to a meaningful value if possible and evaluate it
if (uri.startsWith("http://")) {
return computeFhirValueSetUsingUrl(context, uri);
ValueSet implicitVs = computeFhirValueSetUsingUrl(context, uri);
if (implicitVs != null) {
return implicitVs;
}
}

throw e;
Expand Down Expand Up @@ -162,6 +169,14 @@ private ValueSet computeFhirValueSetUsingUrl(ServiceProvider context, String url
String fhirVsValue = query.replace("fhir_vs=", "");
if (fhirVsValue.startsWith("ecl/")) {
String ecl = fhirVsValue.replace("ecl/", "");

// make sure we decode the ECL before using it
try {
ecl = URLDecoder.decode(ecl, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
throw new BadRequestException("Failed to decode ECL expression: " + e.getMessage());
}

req.filterByQuery(ecl);
// configure Value Set for ECL
valueSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.junit.Test;

import com.b2international.commons.json.Json;
import com.b2international.snowowl.fhir.tests.FhirRestTest;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.test.commons.rest.RestExtensions;
Expand Down Expand Up @@ -144,4 +145,60 @@ public void expandSnomedCodeSystemURL_AfterAndNext() throws Exception {
+ "&count=5"
+ "&after=AoIpMjU0MjkxMDAwKTI1NDI5MTAwMA=="));
}

@Test
public void expandOnNonExistentSnomedUrlShouldReturnError() throws Exception {
givenAuthenticatedRequest(FHIR_ROOT_CONTEXT)
.contentType(APPLICATION_FHIR_JSON)
.accept(APPLICATION_FHIR_JSON)
.body(Json.object(
"resourceType", "Parameters",
"parameter", Json.array(
Json.object(
"name", "url",
"valueUri", "http://snomed.info/sct/83821000000107?fhir_vs=ecl/*"
),
Json.object(
"name", "count",
"valueInteger", 0
)
)

))
.when().post("/ValueSet/$expand")
.then()
.assertThat()
.statusCode(404);
}

@Test
public void expandSnomedViaPostRequiresUrlEncoding() throws Exception {
var encodedEcl = RestExtensions.encodeQueryParameter("* {{ D term = wild:'*immersion*', active = true, typeId=900000000000013009}}{{ C active = true}}");
System.err.println(encodedEcl);
givenAuthenticatedRequest(FHIR_ROOT_CONTEXT)
.contentType(APPLICATION_FHIR_JSON)
.accept(APPLICATION_FHIR_JSON)
.body(Json.object(
"resourceType", "Parameters",
"parameter", Json.array(
Json.object(
"name", "url",
"valueUri", String.format("http://snomed.info/sct/900000000000207008?fhir_vs=ecl/%s", encodedEcl)
),
Json.object(
"name", "count",
"valueInteger", 0
)
)

))
.when().post("/ValueSet/$expand")
.then()
.assertThat()
.log().ifValidationFails()
.statusCode(200)
.body("resourceType", equalTo("ValueSet"))
.body("id", notNullValue())
.body("expansion.total", equalTo(0));
}
}

0 comments on commit 1667d1e

Please sign in to comment.