MasterMastic
MasterMastic

Reputation: 21306

JSON representation problem with liberator

I'm trying to define a really simple resource with Liberator. To start off a minimal example, I wrote an handler like this:

(resource
  :handle-ok {:hello "world"})

But despite clearly passing a value (that isn't nil) I got the following exception:

java.lang.IllegalArgumentException: No method in multimethod 'render-map-generic' for dispatch value: null.

And that issue has been present in my actual resource too, so I tried to be explicit about how it should represent the map, and I included: :available-media-types ["application/json"]:

(resource
  :available-media-types ["application/json"]
  :handle-ok {:hello "world"})

Now it works (surprised me I had to do that), but now whenever I indicate the resource doesn't exist, I get a really weird error response (not an exception). And to demonstrate that:

(resource
  :available-media-types ["application/json"]
  :exists? false
  :handle-ok {:hello "world"})

Now I'm getting a 404 (which I do appreciate), with the following response body:

There was an error parsing JSON data

Unexpected token R in JSON at position 0

And here's the x-liberator-trace:

:decision (:initialize-context {}); :decision (:service-available? true); :decision (:known-method? :get); :decision (:uri-too-long? false); :decision (:method-allowed? :get); :decision (:malformed? false); :decision (:authorized? true); :decision (:allowed? true); :decision (:valid-content-header? true); :decision (:known-content-type? true); :decision (:valid-entity-length? true); :decision (:is-options? false); :decision (:accept-exists? [false {:representation {:media-type "application/json"}}]); :decision (:accept-language-exists? nil); :decision (:accept-charset-exists? nil); :decision (:accept-encoding-exists? nil); :decision (:processable? true); :decision (:exists? false); :decision (:if-match-star-exists-for-missing? false); :decision (:method-put? false); :decision (:existed? false); :decision (:post-to-missing? false); :handler (:handle-not-found)

And you might suggest from it that I could just hack my way out and define a :handle-not-found handler, and I have, but it just gets completely overridden by this response.

I'm not sure if it's a bug or if I'm just not using Liberator right. With the example being so simple, I assume it's the latter. But on the other hand, when I use "application/edn" instead, it works flawlessly for it. I also wondered if it's a regression so I checked a couple of versions back but same results.

Upvotes: 4

Views: 142

Answers (0)

Related Questions