reitit icon indicating copy to clipboard operation
reitit copied to clipboard

Making alterations to a response schema produces an error relating to the request object

Open marrs opened this issue 4 years ago • 1 comments

Steps to reproduce:

Run the code provided in the gist below [1]. The two endpoints are identical save for the difference between the schemas for response bodies. /success declares that an object should be returned while /failure declares that a bool should be returned. Both are valid JSON so I would expect them both to produce a 200 success response, but /failure fails with a 400 error, and complains about an invalid request body.

Responses are reproduced below:

/* for /success endpoint*/
{
  "success": true
}

/* for /failure endpoint */
{
  "schema": {
    "a": "java.lang.String"
  },
  "errors": "(not (map? nil))",
  "type": "reitit.coercion/request-coercion",
  "coercion": "schema",
  "value": null,
  "in": [
    "request",
    "body-params"
  ]
}

[1] https://gist.github.com/marrs/bded1dfa0b97893610b48b181b6fb50a

marrs avatar Mar 18 '20 13:03 marrs

Hi. I think the merged routers is not right: you should merge both routes and route data. Instead of merging routers, I propose you either:

  1. create two separate ring/ring-router and combine them with ring/routes (e.g. linear scan over the two handlers)

  2. merge just the route data (recommended):

(def dummy-routes
  [["/dummy" (constantly {:status 200, :body "dummy"})]])

(def api-routes
  [["/api" (constantly {:status 200, :body "api"})]])

(def ring-handler
   (ring/ring-handler
     (ring/router 
       [dummy-routes api-routes]
       {:data ...})
     (ring/routes ...)))

ikitommi avatar Mar 20 '20 14:03 ikitommi