metosin / compojure-api

Sweet web apis with Compojure & Swagger
http://metosin.github.io/compojure-api/doc/
Eclipse Public License 1.0
1.12k stars 149 forks source link

Inst getting truncated #408

Closed willspecht closed 3 years ago

willspecht commented 5 years ago

Library Version(s) "2.0.0-alpha28"

Problem

Inst are getting truncated in response. Should be returning {:some-field 1970-01-01T00:06:49.123Z} But instead get {:some-field 1970-01-01T00:06:49Z}

This is causing tests to fail since I'm generating the response with spec and it doesn't match the response mocked from the route. I tried turning off coercion for the response but the truncating is still happening. Any way I can prevent this truncation from happening?

The example response in swagger looks correct as well but the actual response is truncated.

danielcompton commented 5 years ago

Can you give some more information about the content type of the response?

willspecht commented 5 years ago

"content-type": "application/json; charset=utf-8"

danielcompton commented 5 years ago

Can you print out the full HTTP request/response? What you've provided there doesn't look like JSON?

willspecht commented 5 years ago
(ns c2.spec
  (:require [compojure.api.sweet :refer [context GET POST resource]]
            [ring.util.http-response :refer [ok]]
            [clojure.spec.alpha :as s]
            [spec-tools.spec :as spec]
            [clojure.spec.gen.alpha :as gen]))

(s/def ::inst inst?)
(s/def ::inst-map (s/keys :req-un [::inst]))

(def routes
  (context "/" []
    :tags ["spec"]
    :coercion :spec

   (GET "/gen-inst" []
      :summary "plus with clojure.spec"
      :return ::inst-map
      (ok {:inst (gen/generate (s/gen ::inst))}))

Tried to make a simple example using the c2 code example as a base.

the endpoint returns

{
  "inst": "1969-12-31T23:59:59Z"
}
willspecht commented 5 years ago

screen shot 2019-01-24 at 3 35 25 pm

Above you can see how the example response has the untruncated inst value, but the response is truncated, dropping the decimal value.

willspecht commented 5 years ago

Is there any additional information I can provide to help resolve this?

miikka commented 4 years ago

The solution is to set jsonista's :date-format via Muuntaja settings – by default the date format does not include milliseconds.

laurilehmijoki commented 3 years ago

Thanks for the tip. @miikka is there an example where a custom :date-format is defined in an app that uses Compojure API?

Deraen commented 3 years ago

Docs have example on setting Transit readers and writers, configuring Jsonista should be quite similar.

https://github.com/metosin/compojure-api/wiki/2.0.0-Content-Negotiation-(with-Muuntaja)#with-muuntaja-instance

Muuntaja format encoder-opts and decoder-opts properties are used to configure Jsonista object-mappers.

https://cljdoc.org/d/metosin/muuntaja/0.6.7/doc/configuration https://metosin.github.io/jsonista/jsonista.core.html#var-object-mapper

Something like

(def muuntaja
  (m/create
    (update-in 
      muuntaja/default-options
      [:formats "application/json"] 
      merge 
      {:encoder-opts {:date-format "..."}})))

(api
  {:formats muuntaja}
  ...)
miikka commented 3 years ago

Since this is basically a solved issue, I'll close it.