Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Returning Resources with Non-Matching Identifier #2112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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