Fork me on GitHub
#reitit
<
2021-08-02
>
dharrigan04:08:14

Hi @raymcdermott I tried it out myself, and apart from a small change, it works as I would expect:

dharrigan04:08:37

(defn routes
  []
  ["/api"
   ["/episodes"
    {:swagger {:tags ["episodes"]}
     :post    {:summary    "persist data for the episode"
               :parameters {:body [:map [:number int?]]}
               :responses  {200 {:body [:map [:number int?]]}}
               :handler    (fn [{{{:keys [number]} :body} :parameters}]
                             (println :number number)
                             {:status 200 :body {:number number}})}}]

   ["/episodes/:number"
    {:swagger {:tags ["episodes"]}
     :get     {:summary    "fetch data for the episode"
               :parameters {:path [:map [:number int?]]}
               :responses  {200 {:body [:map [:number int?]]}}
               :handler    (fn [{{{:keys [number]} :path} :parameters}]
                             (println :number number)
                             {:status 200 :body {:number number}})}}]])

dharrigan04:08:06

The handler still needs to return the correct response (the :responses only validates it)

dharrigan04:08:24

Here's a POST

dharrigan04:08:28

❯ http --verbose POST :18081/api/episodes number:=1
POST /api/episodes HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 13
Content-Type: application/json
Host: localhost:18081
User-Agent: HTTPie/2.4.0

{
    "number": 1
}


HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json;charset=utf-8

{
    "number": 1
}

dharrigan04:08:33

and here's a GET

dharrigan04:08:42

❯ http --verbose :18081/api/episodes/1
GET /api/episodes/1 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Host: localhost:18081
User-Agent: HTTPie/2.4.0



HTTP/1.1 200 OK
Content-Length: 12
Content-Type: application/json;charset=utf-8

{
    "number": 1
}

dharrigan04:08:11

I hope you get it to work for you 🙂

dharrigan04:08:46

(as an aside, you can nest the routes...)

dharrigan04:08:48

(defn routes
  [app-config]
  ["/api"
   ["/episodes"
    ["" {:swagger {:tags ["episodes"]}
         :post    {:summary    "persist data for the episode"
                   :parameters {:body [:map [:number int?]]}
                   :responses  {200 {:body [:map [:number int?]]}}
                   :handler    (fn [{{{:keys [number]} :body} :parameters}]
                                 (println :number number)
                                 {:status 200 :body {:number number}})}}]

    ["/:number" {:swagger {:tags ["episodes"]}
                  :get     {:summary    "fetch data for the episode"
                            :parameters {:path [:map [:number int?]]}
                            :responses  {200 {:body [:map [:number int?]]}}
                            :handler    (fn [{{{:keys [number]} :path} :parameters}]
                                          (println :number number)
                                          {:status 200 :body {:number number}})}}]]])

dharrigan04:08:38

I also have a further, more detailed route setup on a little show-and-tell project I use

raymcdermott13:08:33

@dharrigan thanks for the suggestions and for the link - I'm making progress 🙂

raymcdermott13:08:51

picking up on your project I managed to get the error reported properly

raymcdermott13:08:57

500 Undocumented Error: Wrong number of args (3) passed to: server.web-site/fn--12283

raymcdermott13:08:26

if I add two additional parameter placeholders the request never completes

raymcdermott13:08:45

it prints this

raymcdermott13:08:50

:number 1 :arg2 #object[reitit.ring.coercion$fn__10487$fn__10489$fn__10490$fn__10491 0x729976a2 [email protected]976a2] :arg3 #object[reitit.ring.coercion$fn__10500$fn__10502$fn__10503$fn__10506 0x1651e4bc [email protected]1e4bc]

raymcdermott13:08:23

it's pretty weird that such a trivial thing is creating such mayhem

raymcdermott13:08:14

I'm going back to ring at this point to get my simple thing working and will follow this to see if I can make it back here

raymcdermott14:08:58

haha somehow I had set :async true on the jetty adapter ... panic over 🙂

raymcdermott15:08:55

[ FYI I found it by testing my server in the REPL with the hello world example from ring ]