Skip to content

Commit

Permalink
fix: "failing XML emit" test
Browse files Browse the repository at this point in the history
  • Loading branch information
allentiak committed Nov 25, 2024
1 parent 8ff3e98 commit aa5fcfe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 15 additions & 7 deletions modules/rest-util/src/blaze/middleware/fhir/output.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,37 @@
(with-open [_ (prom/timer generate-duration-seconds "json")]
(fhir-spec/unform-json body)))

(defn- generate-xml** [body]
(defn- xml-byte-array [body]
(let [out (ByteArrayOutputStream.)]
(with-open [writer (io/writer out)]
(xml/emit (fhir-spec/unform-xml body) writer))
(.toByteArray out)))

(defn- generate-xml* [body]
(defn- e->xml-byte-array [e]
(-> e
ba/anomaly
handler-util/operation-outcome
xml-byte-array))

(defn- generate-xml* [response]
(try
(generate-xml** body)
(update response :body xml-byte-array)
(catch Throwable e
(generate-xml** (handler-util/operation-outcome (ba/anomaly e))))))
(assoc response
:body (e->xml-byte-array e)
:status 500))))

(defn- generate-xml [body]
(defn- generate-xml [response]
(log/trace "generate XML")
(with-open [_ (prom/timer generate-duration-seconds "xml")]
(generate-xml* body)))
(generate-xml* response)))

(defn- encode-response-json [{:keys [body] :as response} content-type]
(cond-> response body (-> (update :body generate-json)
(ring/content-type content-type))))

(defn- encode-response-xml [{:keys [body] :as response} content-type]
(cond-> response body (-> (update :body generate-xml)
(cond-> response body (-> generate-xml
(ring/content-type content-type))))

(defn- format-key [format]
Expand Down
2 changes: 2 additions & 0 deletions modules/rest-util/test/blaze/middleware/fhir/output_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@

(testing "failing XML emit"
(given (call (special-resource-handler {:fhir/type :fhir/Patient :id "0" :gender #fhir/code"foo\u001Ebar"}) {:headers {"accept" "application/fhir+xml"}})
:status := 500
[:headers "Content-Type"] := "application/fhir+xml;charset=utf-8"
[:body parse-xml :fhir/type] := :fhir/OperationOutcome
[:body parse-xml :issue 0 :diagnostics] := "Invalid white space character (0x1e) in text to output (in xml 1.1, could output as a character entity)")))

(deftest not-acceptable-test
Expand Down

0 comments on commit aa5fcfe

Please sign in to comment.