This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-19
Channels
- # aleph (11)
- # aws (1)
- # beginners (14)
- # bitcoin (1)
- # boot (41)
- # cider (6)
- # cljs-dev (1)
- # cljsrn (13)
- # clojure (138)
- # clojure-italy (10)
- # clojure-nl (1)
- # clojure-poland (2)
- # clojure-russia (62)
- # clojure-sg (2)
- # clojure-spec (31)
- # clojure-uk (51)
- # clojurescript (109)
- # core-matrix (1)
- # core-typed (1)
- # cursive (63)
- # datomic (10)
- # emacs (9)
- # euroclojure (1)
- # hoplon (112)
- # immutant (16)
- # jobs (2)
- # lumo (5)
- # off-topic (14)
- # om (54)
- # onyx (17)
- # parinfer (23)
- # pedestal (2)
- # re-frame (41)
- # ring-swagger (23)
- # spacemacs (9)
- # specter (10)
- # uncomplicate (5)
- # vim (1)
>Is it possible that you are redefining the Tag
later in the code`?
Nope the code is pretty simple
Hmm.. did a new project from the template and run the code with 1.1.10, works as expected. Could you try this:
with this contents:
(ns c1.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[schema.core :as s]))
(s/defschema Collection
{:id s/Int
:name s/Str})
(s/defschema Tag
{:id s/Int
:tag s/Str})
(def app
(api
(swagger-routes)
(context "/:storeId" []
(GET "/collections" {:as request params :params}
:summary "Get Collections"
:path-params [storeId :- s/Str]
:return [Collection]
(ok [{:id 101 :name "My Collection"}]))
(GET "/tags" {:as request params :params}
:summary "Get Tags"
:path-params [storeId :- s/Str]
:return [Tag]
(ok [{:id 101 :tag "My Collection"}])))))
because :return
only sets the response validation for status code 200 (in master at least).
@ikitommi can anyone edit the wiki? if not, can you fix a typo/rename in the Coersion docs that explain how to revert to the pre-2.0 coerce+validation of responses. The example shows schema-coercion/json-coercion
and at some point, it became schema-coercion/json-coercion-matcher
. Full (working) example is something like this:
(def compojure-1x-coercion
(schema-coercion/create-coercion
(assoc-in schema-coercion/default-options [:response :default] schema-coercion/json-coercion-matcher)))
@bja thanks. anyone can contribute to wiki. Looking forward to awesome enchancements ;)
@ikitommi, i did work pre-2.0.0 with 200 responses, not sure about 201 (or other http status)
i will examine your snippet and check if adding :responses
instead of :return
fixes the problem
thank you
@ikitommi, just updated my code like your snippet, and instead of getting a 500 status code the api is returning 400 and raising this strange exception
Exception in thread "async-dispatch-6" clojure.lang.ExceptionInfo: Response validation failed: #compojure.api.coercion.core.CoercionError
`
here is the http repsonse from the server
{
"spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:payment-gateway.spec/cpf :payment-gateway.spec/name :payment-gateway.spec/email] :opt-un [:payment-gateway.spec/creditCard]), :type :map, :keys #{:cpf :email :name :creditCard}})",
"problems": [
{
"path": [],
"pred": "map?",
"val": null,
"via": [],
"in": []
}
],
"type": "compojure.api.exception/request-validation",
"coercion": "spec",
"value": null,
"in": [
"request",
"body-params"
]
}
@plins looks like you are sending empty body while the endpoint expects something. You have also an input spec defined?
(def routes
(api/context "/api" []
:coercion :spec
(api/POST "/user" []
:summary "Cadastra Taxistas ou Passageiros"
:body [request (spec/keys :req-un [::s/cpf ::s/name ::s/email]
:opt-un [::s/creditCard])]
:responses {201 {:schema (spec/keys :req-un [::s/customerId]
:opt-un [::s/creditCardId])}}
(go (res/created "" request)))))
since the input and the output expects different specs, and im returning the route input
POST /api/user HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Cache-Control: no-cache
{
"name" : "Batman dos Santos Oliveira Junior",
"email" : "",
"cpf" : "29774456092",
"creditCard" : {
"holdersName" : "BATMAN S O JUNIOR",
"creditCardNumber" : "1111222233334444",
"expirationMonth" : "07",
"expirationYear" : "2020",
"cvc" : "1323"
}
}