Fork me on GitHub
caleb.macdonaldblack01:01:38 Says this: Before invoking the :enter functions, the servlet interceptor sets up a “terminator” predicate on the context. It terminates the interceptor chain when the context map returned by an interceptor has a response map attached. My server has this:

(ns wp-server.server
  (:gen-class)                                              ; for -main method in uberjar
    [io.pedestal.http :as server]
    [io.pedestal.http.route :as route]
    [wp-server.service :as service]
    [wp-server.datomic :as datomic]))

(defonce runnable-service (atom nil))

(defn -main
  "The entry-point for 'lein run'"
  [& args]
  (println "/nConnecting to datomic...")
  (println "\nCreating your server...")
  (reset! runnable-service (server/create-servlet service/service))
  (server/start runnable-service))

(defn create-runnable-dev-service
  "The entry-point for 'lein run-dev'"
  [& args]
  (println "\nConnecting to [DEV] database...")
  (println "\nCreating your [DEV] server...")
  (-> service/service
      (merge {:env                     :dev
              ::server/join?           false
              ::server/routes          #(route/expand-routes (deref #'service/routes))
              ::server/allowed-origins {:creds true :allowed-origins (constantly true)}
              ::server/secure-headers  {:content-security-policy-settings {:object-src "none"}}})
My service looks like this:
(ns wp-server.service
    [datomic.api :as d]
    [io.pedestal.http :as http]
    [io.pedestal.http.body-params :as body-params]
    [io.pedestal.http.route :as route]
    [ring.util.response :as ring-resp]
    [wp-server.datomic :as datomic]
    [wp-common.client :as wp-common-client]
    [wp-common.core :as wp-common-core]
    [clojure.spec.alpha :as s]
    [ring.util.response :as ring-response]))

(def home-page
  {:name  :home-page
   :enter (fn [context]
            (prn "TWO")
            (assoc context :response {:status 200 :body "Hello, world!"}))})

(def common-interceptors [datomic/db-interceptor (body-params/body-params) http/html-body])

(defn create-spec-validator
  {:name :validate-spec
         (fn [{{:keys [:edn-params]} :request :as context}]
           (prn "ONE")
           (if-let [explained (s/explain-data spec edn-params)]
             (assoc context
               :response {:status 400 :body explained})))})

(def routes #{
              ;["/" :get (conj common-interceptors `home-page)]
              ["/clients" :post (conj common-interceptors (create-spec-validator ::wp-common-client/schema) home-page)]
              ["/db-test" :get (conj common-interceptors `db-test-page)]
              ["/about" :get (conj common-interceptors `about-page)]})

(def service {:env                     :prod
              ::http/routes            routes
              ::http/type              :jetty
              ::http/port              5000
              ::http/container-options {:h2c? true
                                        :h2?  false
                                        :ssl? false}})
When sending a request to localhost:5000/clients with a body that does not pass spec, the create-spec-validator interceptor adds a response to the context. I have confirmed this by logging the context in the home-page interceptor. I would expect the home-page interceptor to be skipped as per documentation. This does not happen. Instead the :enter function of the home-page interceptor is called and the response overwritten. Why isn’t the home-page interceptor being skipped when the create-spec-validator prior to it is returning context with a response?


I need to use io.pedestal.interceptor.chain/terminate. We believe those docs are out of data


@caleb.macdonaldblack Might not be an issue here, but your :validate-spec interceptor does not return the context on a successful validation. If you return nil, does Pedestal assume the unmodified context?


@caleb.macdonaldblack Here you go: the :response has to be a valid response according to ring.util.response/response?, so it needs to be a map with :status and :headers:

(defn response?
  "True if the supplied value is a valid response map."
  {:added "1.1"}
  (and (map? resp)
       (integer? (:status resp))
       (map? (:headers resp))))