Search code examples
clojureswaggerringcompojure

Best way to customize Swagger / Compojure 404 response


In Compojure one can define default 404 behavior, e.g.,

(defroutes app-routes
  ;; ...
  (route/not-found "These aren't the droids you're looking for."))

As we've been increasing the number and complexity of REST endpoints, we've been looking into switching to Swagger / compojure-api. Swagger seems higher level and quite different from basic Compojure. What's the correct idiom for customizing 404 behavior using this library?


Solution

  • There is no definition in the Swagger Spec for the "missed everything else" default handler of Compojure. Swagger is about documenting existing apis, not the non-existing. Just discussed about this at #swagger on freenode.

    Still, you can describe the default routes manually with compojure-api, created a sample here: https://gist.github.com/ikitommi/cdf19eeaf4918efb051a.

    Note: Swagger spec doesn't actually understand the ANY -http method, but does seem to work with the 1.2 Swagger UI - will not work with codegens. To be standard compliant, you should add manually handlers for all http methods (https://github.com/swagger-api/swagger-spec/blob/e42dd2011340a12efbb531f593c0f99c831c4582/schemas/v1.2/operationObject.json#L10) to the end of your route definitions.

    hope this helps.