Skip to content

Commit

Permalink
fix: "invalid JSON" logic (WIP - with debugging code)
Browse files Browse the repository at this point in the history
Update both the request body and status in case of errors, not just the
body.
  • Loading branch information
allentiak committed Nov 25, 2024
1 parent 298901c commit 03eceb4
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions modules/rest-util/src/blaze/middleware/fhir/output.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,57 +37,74 @@
(defn- generate-json** [body]
(fhir-spec/unform-json body))


(comment
(defn- parse-json [body]
(fhir-spec/conform-json (fhir-spec/parse-json body)))


(parse-json (generate-json** {:fhir/type :fhir/Patient :id "0"}))
;; => {:fhir/type :fhir/Patient, :id "0"}
:end)

(defn- generate-json* [response]
(try
(update response :body generate-json**)
(catch Throwable e
(update response :body (constantly (generate-json** (handler-util/operation-outcome (ba/anomaly e))))))))

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

(comment
(def invalid-body {:fhir/type :fhir/Patient :id "0" :gender #fhir/code"foo\u001Ebar"})
(def valid-body {:fhir/type :fhir/Patient :id "0"})

(ring/response valid-body)
;; => {:status 200, :headers {}, :body {:fhir/type :fhir/Patient, :id "0"}}
;; => {:status 200, :headers {}, :body {:fhir/type :fhir/Patient, :id "0"}}

(def valid-resp (ring/response valid-body))

(ring/response invalid-body)
;; => {:status 200, :headers {}, :body {:fhir/type :fhir/Patient, :id "0", :gender #fhir/code"foobar"}}
;; => {:status 200, :headers {}, :body {:fhir/type :fhir/Patient, :id "0", :gender #fhir/code"foobar"}}

(def invalid-resp (ring/response invalid-body))

(defn- parse-json [body]
(fhir-spec/conform-json (fhir-spec/parse-json body)))

(parse-json valid-body)
(generate-json* valid-resp)
;; => {:status 200, :headers {}, :body #object["[B" 0x72aa21ba "[B@72aa21ba"]}
(-> valid-resp
generate-json*
parse-json)
;; => {:cognitect.anomalies/category :cognitect.anomalies/incorrect, :cognitect.anomalies/message "Invalid JSON representation of a resource.", :x #:cognitect.anomalies{:category :cognitect.anomalies/incorrect, :message "No implementation of method: :-read-value of protocol: #'jsonista.core/ReadValue found for class: clojure.lang.PersistentArrayMap"}, :fhir/issues [#:fhir.issues{:severity "error", :code "value", :diagnostics "Given resource does not contain a `resourceType` property."}]}

(parse-json (j/write-value-as-string {:fhir/type :fhir/Patient :id "0"}))
;; => {:cognitect.anomalies/category :cognitect.anomalies/incorrect, :cognitect.anomalies/message "Invalid JSON representation of a resource.", :x {:fhir/type "fhir/Patient", :id "0"}, :fhir/issues [#:fhir.issues{:severity "error", :code "value", :diagnostics "Given resource does not contain a `resourceType` property."}]}

:end)


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

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


(comment

(defn- parse-xml [body]
(with-open [reader (io/reader body)]
(fhir-spec/conform-xml (xml/parse reader))))

(parse-xml (xml-byte-array {:fhir/type :fhir/Patient :id "0"}))
;; => {:id "0", :fhir/type :fhir/Patient}

:end)


(defn- generate-xml* [response]
(try
(update response :body xml-byte-array)
Expand Down

0 comments on commit 03eceb4

Please sign in to comment.