liberator icon indicating copy to clipboard operation
liberator copied to clipboard

render-seq-generic strange behaviour for "text/html" "text/plain" "text/csv"

Open petoalbert opened this issue 6 years ago • 1 comments

Lets take the following resource to demonstrate the problem:

(defresource my-resource
  :available-media-types ["text/plain" "text/html" "text/csv" "application/json"]
  :available-charsets ["utf-8"]
  :handle-ok ["wow" "cool"])

It works fine for "application/json", but accessing it as "text/plain" leads to the following exception: nth not supported on this type: Character

With "text/csv" and "text/html", I get this: java.lang.Character cannot be cast to java.util.Map$Entry. If I get it correctly, this is caused by the implementations of render-seq-generic trying to interpret their data argument as a sequence of Maps for these MIME types, but I cannot imagine why.

For example, render-seq-generic for "text/plain" directly calls render-map-generic on the sequence items: https://github.com/clojure-liberator/liberator/blob/218febdc21bb6c7aec2abd44247ac576f06c8927/src/liberator/representation.clj#L154

Can somebody explain to me what the representations for these MIME types are intended to look like?

FYI: I discovered this by running the examples from the project and accessing athletes-resource at /drag-drop/athletes

petoalbert avatar Jun 04 '18 23:06 petoalbert

The HTML, CSV and TSV rendering provided by render-seq-generic is meant to represent a table of (named) values, so the sequence should look like [{:reaction "wow" :attitude "cool"} {:reaction "nope" :attitude "cool"} {:reaction "haha" :attitude "pleased"}], resulting in the following CSV:

reaction,attitude
wow,cool
nope,cool
haha,pleased

All maps should have identical keys, or at least the keyset of the first element in the seq should be a superset of the rest, otherwise the headers will be broken.

vidraj avatar Mar 10 '21 18:03 vidraj