Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active October 1, 2015 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deraen/7abc08fe085526d204c0 to your computer and use it in GitHub Desktop.
Save Deraen/7abc08fe085526d204c0 to your computer and use it in GitHub Desktop.
JSON key-fn with compojure-api and ring-middleware-format
(ns examples.thingie
(:require [ring.util.http-response :refer :all]
[compojure.api.sweet :refer :all]
[camel-snake-kebab.core :refer :all]))
;; Requires compojure-api 0.24.0-SNAPSHOT and ring-middleware-format 0.7.0-SNAPSHOT
;;
;; Routes
;;
(defn key-json->clj [x]
(keyword (->kebab-case x)))
(defn key-clj->json [x]
(->camelCase (if (keyword? x) (name x) x)))
(defapi app
{:format {:formats [:json]
:params-opts {:json {:key-fn key-json->clj}}
:response-opts {:json {:key-fn key-clj->json}}}}
(swagger-ui)
(swagger-docs
{:info {:version "1.0.0"
:title "Thingies API"}})
(GET* "/foo" []
(ok {:test-foo 5
:asd-asd "Foo"}))
(POST* "/foo" []
:body [body {:test-foo Long
:asd-ads String}]
(println body)
(ok body)))
@benearlam
Copy link

This is really interesting. However i've tried it and it doesnt work - it would appear that the format specific options are ignored for json-kw formatting. Is that intentional?

@Deraen
Copy link
Author

Deraen commented Oct 1, 2015

@benearlam I didn't get around to finalizing ring-middleware-format changes 5 months ago.

Now in latest snapshot ring-middleware-format (ngrunwald repo, not metosin fork) should support this. Latest compojure-api depends on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment