Last active
October 18, 2017 08:30
-
-
Save slipset/016b6f4ebb10fea8e596221ed638302d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(json-gen/add-encoder org.joda.time.DateTime | |
(fn [c jsonGenerator] | |
(.writeString jsonGenerator (str c)))) | |
(defn str->date-time [_ value] | |
(try | |
(DateTime. value) | |
(catch Exception e | |
::spec/invalid))) | |
(def my-date-time-conforming | |
(st/type-conforming | |
(assoc conform/string-type-conforming | |
:date-time | |
str->date-time))) | |
(def custom-coercion | |
(-> compojure.api.coercion.spec/default-options | |
(assoc-in | |
[:body :formats "application/json"] | |
(st/type-conforming | |
(merge | |
conform/json-type-conforming | |
{:date-time str->date-time} | |
conform/strip-extra-keys-type-conforming))) | |
compojure.api.coercion.spec/create-coercion)) | |
(spec/def ::id int?) | |
(spec/def ::name string?) | |
(spec/def ::date (st/spec (partial instance? DateTime) {:type :date-time | |
:reason "FAIL" | |
:json-schema/default "2017-10-12T05:04:57.585Z"})) | |
(spec/def ::id-name-date (spec/keys :req-un [::id ::name ::date])) | |
(spec/def ::id-name (spec/keys :req-un [::id ::name ])) | |
(context "/spec-test" [] | |
:coercion custom-coercion | |
(GET "/foo" [] | |
:return ::id-name-date | |
(ok {:id 1 :name "foo" :date (DateTime.)})) ;; <- change this to {:id 1 :name "foo" :date "lol"} and you get the same muuntaja error | |
(POST "/foo" [] | |
:return ::id-name-date | |
:body [b ::id-name-date] | |
(ok (do (println b) b #_(assoc b :date "lol"))))) | |
Wed Oct 18 08:23:11 UTC 2017 [worker-3] ERROR - POST /spec-test/foo | |
clojure.lang.ExceptionInfo: Malformed application/json in :muuntaja/encode {:type :muuntaja/encode, :format "application/json"} | |
at clojure.core$ex_info.invokeStatic(core.clj:4744) | |
at clojure.core$ex_info.invoke(core.clj:4744) | |
at muuntaja.core$on_exception.invokeStatic(core.clj:66) | |
at muuntaja.core$on_exception.invoke(core.clj:64) | |
at muuntaja.core$create_coder$f__13240.invoke(core.clj:106) | |
at clojure.core$update.invokeStatic(core.clj:6123) | |
at clojure.core$update.invoke(core.clj:6113) | |
at muuntaja.core$handle_response.invokeStatic(core.clj:398) | |
at muuntaja.core$handle_response.invoke(core.clj:394) | |
at muuntaja.core$format_response.invokeStatic(core.clj:423) | |
at muuntaja.core$format_response.invoke(core.clj:417) | |
at muuntaja.middleware$wrap_format_response$fn__13397.invoke(middleware.clj:131) | |
at muuntaja.middleware$wrap_format_negotiate$fn__13390.invoke(middleware.clj:95) | |
at ring.middleware.keyword_params$wrap_keyword_params$fn__11594.invoke(keyword_params.clj:36) | |
at ring.middleware.nested_params$wrap_nested_params$fn__11652.invoke(nested_params.clj:89) | |
at ring.middleware.params$wrap_params$fn__11726.invoke(params.clj:67) | |
at compojure.api.middleware$wrap_inject_data$fn__16254.invoke(middleware.clj:100) | |
at compojure.api.routes.Route.invoke(routes.clj:86) | |
at clojure.lang.Var.invoke(Var.java:381) | |
at compojure.core$routing$fn__6221.invoke(core.clj:185) | |
at clojure.core$some.invokeStatic(core.clj:2698) | |
at clojure.core$some.invoke(core.clj:2689) | |
at compojure.core$routing.invokeStatic(core.clj:185) | |
at compojure.core$routing.doInvoke(core.clj:182) | |
at clojure.lang.RestFn.applyTo(RestFn.java:139) | |
at clojure.core$apply.invokeStatic(core.clj:659) | |
at clojure.core$apply.invoke(core.clj:652) | |
at compojure.core$routes$fn__6225.invoke(core.clj:192) | |
at compojure.api.routes.Route.invoke(routes.clj:86) | |
at clojure.lang.Var.invoke(Var.java:381) | |
at compojure.core$wrap_routes$fn__6324.invoke(core.clj:348) | |
at compojure.core$routing$fn__6221.invoke(core.clj:185) | |
at clojure.core$some.invokeStatic(core.clj:2698) | |
at clojure.core$some.invoke(core.clj:2689) | |
at compojure.core$routing.invokeStatic(core.clj:185) | |
at compojure.core$routing.doInvoke(core.clj:182) | |
at clojure.lang.RestFn.applyTo(RestFn.java:139) | |
at clojure.core$apply.invokeStatic(core.clj:659) | |
at clojure.core$apply.invoke(core.clj:652) | |
at compojure.core$routes$fn__6225.invoke(core.clj:192) | |
at compojure.api.routes.Route.invoke(routes.clj:86) | |
at ring.middleware.flash$wrap_flash$fn__18758.invoke(flash.clj:39) | |
at ring.middleware.session$wrap_session$fn__19012.invoke(session.clj:108) | |
at ring.middleware.keyword_params$wrap_keyword_params$fn__11594.invoke(keyword_params.clj:36) | |
at ring.middleware.nested_params$wrap_nested_params$fn__11652.invoke(nested_params.clj:89) | |
at ring.middleware.multipart_params$wrap_multipart_params$fn__19173.invoke(multipart_params.clj:172) | |
at ring.middleware.params$wrap_params$fn__11726.invoke(params.clj:67) | |
at ring.middleware.cookies$wrap_cookies$fn__18891.invoke(cookies.clj:175) | |
at ring.middleware.absolute_redirects$wrap_absolute_redirects$fn__19260.invoke(absolute_redirects.clj:47) | |
at ring.middleware.resource$wrap_resource$fn__19189.invoke(resource.clj:37) | |
at ring.middleware.content_type$wrap_content_type$fn__17407.invoke(content_type.clj:34) | |
at ring.middleware.default_charset$wrap_default_charset$fn__19232.invoke(default_charset.clj:31) | |
at ring.middleware.not_modified$wrap_not_modified$fn__17437.invoke(not_modified.clj:53) | |
at ring.middleware.x_headers$wrap_x_header$fn__18721.invoke(x_headers.clj:22) | |
at ring.middleware.x_headers$wrap_x_header$fn__18721.invoke(x_headers.clj:22) | |
at ring.middleware.x_headers$wrap_x_header$fn__18721.invoke(x_headers.clj:22) | |
at ring.middleware.gzip$wrap_gzip$fn__18037.invoke(gzip.clj:94) | |
at org.httpkit.server.HttpHandler.run(RingHandler.java:91) | |
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) | |
at java.util.concurrent.FutureTask.run(FutureTask.java:266) | |
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) | |
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) | |
at java.lang.Thread.run(Thread.java:748) | |
Caused by: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class java.lang.Class: class org.joda.time.DateTime | |
at cheshire.generate$generate.invokeStatic(generate.clj:152) | |
at cheshire.generate$generate.invoke(generate.clj:116) | |
at cheshire.generate$generate.invokeStatic(generate.clj:128) | |
at cheshire.generate$generate.invoke(generate.clj:116) | |
at cheshire.generate$generate.invokeStatic(generate.clj:122) | |
at cheshire.generate$generate.invoke(generate.clj:116) | |
at cheshire.generate$generate.invokeStatic(generate.clj:130) | |
at cheshire.generate$generate.invoke(generate.clj:116) | |
at cheshire.generate$generate.invokeStatic(generate.clj:122) | |
at cheshire.generate$generate.invoke(generate.clj:116) | |
at cheshire.core$generate_string.invokeStatic(core.clj:74) | |
at cheshire.core$generate_string.invoke(core.clj:49) | |
at muuntaja.format.json$make_json_encoder$fn__12971.invoke(json.clj:21) | |
at muuntaja.core$create_coder$f__13240.invoke(core.clj:104) | |
... 58 more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment