Skip to content

Commit

Permalink
Fix Returning Resources with Non-Matching Identifier
Browse files Browse the repository at this point in the history
Closes: #2111
  • Loading branch information
alexanderkiel committed Oct 10, 2024
1 parent 0c36e2b commit 1e3c449
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
20 changes: 9 additions & 11 deletions modules/db/src/blaze/db/impl/search_param/token.clj
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@
(and value (nil? system))
(conj (str "|" value)))))

(defn- remove-non-matching [db expression value resource-handle]
(defn- matches-identifier-values? [db expression value-set resource-handle]
(let [resource @(d/pull db resource-handle)
values (fhir-path/eval noop-resolver expression resource)]
(assert (not (ba/anomaly? values)))
(some #{value} (mapcat identifier-values values))))
(some value-set (mapcat identifier-values values))))

(defrecord SearchParamTokenIdentifier [name url type base code target c-hash expression]
p/SearchParam
Expand All @@ -241,23 +241,21 @@
(-resource-handles [_ batch-db tid modifier value]
(let [c-hash (c-hash-w-modifier c-hash code modifier)
resource-handles (resource-handles batch-db c-hash tid value)]
(if (< 1 (count resource-handles))
(filterv (partial remove-non-matching batch-db expression value) resource-handles)
resource-handles)))
(filterv (partial matches-identifier-values? batch-db expression #{value}) resource-handles)))

(-resource-handles [_ batch-db tid modifier value start-id]
(let [c-hash (c-hash-w-modifier c-hash code modifier)
resource-handles (resource-handles batch-db c-hash tid value start-id)]
(if (< 1 (count resource-handles))
(filterv (partial remove-non-matching batch-db expression value) resource-handles)
resource-handles)))
(filterv (partial matches-identifier-values? batch-db expression #{value}) resource-handles)))

(-compartment-keys [_ _ _ _ _])

(-matcher [_ batch-db modifier values]
(r-sp-v/value-prefix-filter (:snapshot batch-db)
(c-hash-w-modifier c-hash code modifier)
(mapv codec/v-hash values)))
(comp
(r-sp-v/value-prefix-filter (:snapshot batch-db)
(c-hash-w-modifier c-hash code modifier)
(mapv codec/v-hash values))
(filter (partial matches-identifier-values? batch-db expression (set values)))))

(-compartment-ids [_ _ _])

Expand Down
30 changes: 29 additions & 1 deletion modules/db/test/blaze/db/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5338,7 +5338,35 @@
(given (pull-type-query node "Patient" [["active" "true"] ["identifier" "system-115849|"]])
count := 2
[0 :id] := "0"
[1 :id] := "1")))))
[1 :id] := "1"))))

(testing "doesn't find the Observation with identifier with hash collision"
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Observation :id "0"
:identifier
[#fhir/Identifier{:value "2404351199702_20240422094702_DELTA-HE"}]}]]]

(is (empty? (pull-type-query node "Observation" [["identifier" "2410301332030_20241009113701_FDP-D"]]))))

(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Observation :id "0"
:identifier
[#fhir/Identifier{:value "2404351199702_20240422094702_DELTA-HE"}]}]]
[[:put {:fhir/type :fhir/Observation :id "1"
:identifier
[#fhir/Identifier{:value "2404351199702_20240422094702_DELTA-HE"}]}]]]

(is (empty? (pull-type-query node "Observation" [["identifier" "2410301332030_20241009113701_FDP-D"]] "1"))))

(testing "as second clause"
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Observation :id "0"
:status #fhir/code"final"
:identifier
[#fhir/Identifier{:value "2404351199702_20240422094702_DELTA-HE"}]}]]]

(is (empty? (pull-type-query node "Observation" [["status" "final"]
["identifier" "2410301332030_20241009113701_FDP-D"]])))))))

(deftest type-query-tag-test
(with-system-data [{:blaze.db/keys [node]} config]
Expand Down

0 comments on commit 1e3c449

Please sign in to comment.