From 9745ab748259e83977ae121a8da6096836794b08 Mon Sep 17 00:00:00 2001 From: Leandro Doctors Date: Fri, 6 Dec 2024 14:08:30 +0100 Subject: [PATCH] add more debugging code --- .../src/blaze/middleware/fhir/output.clj | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/modules/rest-util/src/blaze/middleware/fhir/output.clj b/modules/rest-util/src/blaze/middleware/fhir/output.clj index 742d15521..ccb7695b3 100644 --- a/modules/rest-util/src/blaze/middleware/fhir/output.clj +++ b/modules/rest-util/src/blaze/middleware/fhir/output.clj @@ -46,6 +46,11 @@ (xml/emit (fhir-spec/unform-xml body) writer)) (.toByteArray out))) +(comment + (-> (generate-xml** {:fhir/type :fhir/Patient :id "0" :gender #fhir/code"foo\u001Ebar"}) + (update :body parse-xml)) + :end) + (defn- generate-xml* [response] (try (update response :body generate-xml**) @@ -59,10 +64,40 @@ (with-open [_ (prom/timer generate-duration-seconds "xml")] (generate-xml* response))) +(comment + (try (generate-xml** {:fhir/type :fhir/Patient :id "0" :gender "foobar"}) + (catch Throwable e (ex-message e))) + ;; => "Invalid white space character (0x1e) in text to output (in xml 1.1, could output as a character entity)" + + (-> (generate-xml* {:status 200, :headers {}, :body {:fhir/type :fhir/Patient :id "0" :gender "foobar"}}) + (update :body parse-xml)) + ;; => {:status 500, :headers {}, :body {:issue [{:severity #fhir/code"error", :code #fhir/code"exception", :diagnostics "Invalid white space character (0x1e) in text to output (in xml 1.1, could output as a character entity)", :fhir/type :fhir.OperationOutcome/issue}], :fhir/type :fhir/OperationOutcome}} + + :end) + (defn- generate-binary** [body] (when (:data body) (.decode (Base64/getDecoder) ^String (type/value (:data body))))) +(comment + + (generate-binary* {:status 200, :headers {}, :body {:data "MTANjECg==" :content-type nil}}) + ;; => {:status 500, :headers {}, :body nil} + + (generate-binary** {:status 200, :headers {}, :body {:data "MTANjECg==" :content-type nil}}) + ;; => nil + + ;; BUG: this should give me an exception, but gives me nil. + ;; To do this, I need something like `fhir-spec/unform-xml`, but for base64 + ;; I should validate the `:data` part of the `body`. If it is an invalid base64 string, it should give me an exception + + + (try (generate-binary** {:status 200, :headers {}, :body {:data "MTANjECg==" :content-type nil}}) + (catch Throwable e (ex-message e))) +;; => nil + + :end) + (defn- generate-binary* [response] (try (update response :body generate-binary**) @@ -76,6 +111,16 @@ (with-open [_ (prom/timer generate-duration-seconds "binary")] (generate-binary* response))) +(comment + (defn- parse-xml [body] + (with-open [reader (io/reader body)] + (fhir-spec/conform-xml (xml/parse reader)))) + + (-> (generate-binary {:status 200, :headers {}, :body {:data "MTANjECg==" :content-type nil}})) +;; => {:status 500, :headers {}, :body nil} + + :end) + (defn- encode-response-json [{:keys [body] :as response} content-type] (cond-> response body (-> (update :body generate-json) (ring/content-type content-type))))