metosin / ring-swagger

Swagger Spec for Clojure Web Apps
http://metosin.github.io/ring-swagger/doc/
371 stars 84 forks source link

Add schema helper function for schema.core.Record #102

Closed eunmin closed 8 years ago

eunmin commented 8 years ago

Hi! I am having trouble when I use s/defrecord (issue #103):

(s/defrecord User [id :- s/Str
                   name :- s/Str])           

{:responses {200 {:schema User :description "Found it!"}
             404 {:description "Ohnoes."}}}                           

but ring-swagger throws:

500 : {"type":"unknown-exception","class":"java.lang.IllegalArgumentException"} http://localhost:3000/swagger.json
(require '[schema.utils :as su])
{:responses {200 {:schema (:schema (su/class-schmea User)) :description "Found it!"}
             404 {:description "Ohnoes."}}}

But plain map doesn't have name. So I Add schema name using schema.core/schema-with-name.

Usage with compojure-api:

(ns luminus-swagger.routes.services
  (:require [ring.util.http-response :refer :all]
            [compojure.api.sweet :refer :all]
            [ring.swagger.schema :as rss]
            [schema.core :as s]))

(s/defrecord User [id :- s/Str
                   name :- s/Str])

(defapi service-routes
  {:swagger {:ui "/swagger-ui"
             :spec "/swagger.json"
             :data {:info {:version "1.0.0"
                           :title "Sample API"
                           :description "Sample Services"}}}}
  (context "/users" []
    :tags ["user"]

    (POST "/" []
      :return (rss/class-schema User)
      :body        [user (rss/class-schema User)]
      :summary     "create user"
      (ok))))
ikitommi commented 8 years ago

This will be solved by supporting schema.core/defrecord directly, #103.