Fork me on GitHub
#ring-swagger
<
2017-06-27
>
ikitommi14:06:24

@lambder hi, that should work. Just added a test with :body of string?, please check the test at https://github.com/metosin/compojure-api/blob/master/test19/compojure/api/coercion/spec_coercion_test.clj

ikitommi14:06:40

ah, lambder, you are sending text/json, by default, only application/json is read.

ikitommi14:06:16

Muuntaja does exact matches on the content-type, you can map text/json to be JSON or you can do regex-matches for content-type. There is a configuration guide in https://github.com/metosin/muuntaja/wiki/Configuration

lambder14:06:27

@ikitommi thanks your your answer. I was struggling with it for some time and finally got it.

lambder14:06:44

what is was is simple

lambder14:06:54

the request was text/json

lambder14:06:01

instead of application/json

lambder14:06:16

and ring middleware got confused

lambder14:06:24

that’s why I got no body bind

lambder14:06:44

would be good to have some error reporting

lambder14:06:52

and here is a question

lambder14:06:06

say I got a compojure-api resource

ikitommi14:06:08

@eoliphant Swagger routes are defined as map, so only one POST route can exist for an route - compojure-api only uses first match for the docs.

lambder14:06:12

method POST

lambder14:06:43

if I hit foo with POST with the content not supported by compojure-api

lambder14:06:58

will it search for other resources possibly matching the path?

lambder14:06:28

from what I saw the declared handler was actually called

lambder14:06:35

should it?

lambder14:06:03

I believe yada would return some error related to content negotiation

ikitommi14:06:13

@lambder resource (and all other endpoint-types) are just ring-handlers, if they return nil, the routing continues to search for next route match.

lambder14:06:49

how can I make these resources return nil if the content negotiation fails?

lambder14:06:13

is there any content negotiation in ring at all?

ikitommi14:06:22

@eoliphant you can add manually swagger definitions to the first route to cover rest of the routes, with :swagger. Not sure if that’s a good idea, but can be done.

ikitommi14:06:03

lambder: just return nil from the :handler function

lambder14:06:52

@ikitommi , so do I need to have custom logic , right?

ikitommi14:06:28

there is now api-level content-negotiation, via https://github.com/metosin/muuntaja

lambder14:06:48

wow, how cool

lambder14:06:04

is it a ring middleware?

lambder14:06:28

nw, I’ll check it put. Thanks a lot @ikitommi

ikitommi14:06:15

yes, it’s a middleware, np.

tmclane18:06:02

Is there any way to document and return a different object (including schema definition) based on the resource URI and the Accept header value? I'd like to return a differently structured object depending on if it was application/json or application/vnd.api+json (JSON API) response. Based on the swagger 2.0 documentation this would appear to be impossible.

tmclane18:06:41

Also can someone provide an example where only json-kw and edn are allowed and show up in the swagger documentation for content type support? I tried to feed the 'api' call a list of just :formats [:json-kw :end] but that didn't work out. I am attempting to use compojure.api.sweet to do this. Hopefully someone here knows the answer or can help me figure it out.

barry21:06:28

@tmclane aside, do you have a library you’re using for JSON API serialization? I’ve been looking for something to use instead of rolling my own.

tmclane21:06:07

@barry Currently I am using compojure.api which includes mitosis/muuntaja which is automatically serializing the objects. I dislike that I can't tell it what the 'content-type' is however since it always says it is application/json which is true but it's also application/vnd.api+json which is more specific.

tmclane21:06:19

dang auto correct

tmclane21:06:26

metosin/muuntaja

tmclane21:06:02

I am contemplating removing the extra code and just using Ring / http-kit directly and processing the headers myself. This can be done in a wrapper and have it do the same thing the compojure-api library does.

tmclane21:06:22

Swagger can't represent what I am looking to do anyhow it seems.

barry22:06:36

Does muuntaja support JSON API (vnd.api+json) serialization? It seems like it’s just ‘raw’ JSON out of the box.

tmclane22:06:37

@barry JSONAPI is merely JSON after all.. so yes it works although it will put the wrong content-type header on the response.

tmclane22:06:53

I'm working on figuring out how to override this and I am making a small bit of progress.

barry22:06:31

haha, yes, just JSON, but hand-crafting spec-compliant JSON-API isn’t trivial, e.g. for includes, relationships, etc.

barry22:06:48

Deserializing JSON-API bodies isn’t the most fun I’ve had either.

tmclane22:06:06

The includes, relationships can't be automatically created with a serializer

barry22:06:17

i’d be psyched to hear your approach for the headers too …

tmclane22:06:19

that's something your code will have to do.

barry22:06:25

correct — I may be hunting for something from fairy-tale land. JSON-API seems somewhat tied to relational database mindset, and many of the libraries tie themselves to an ORM

barry22:06:47

since we’re dealing with POJOs (mostly associate maps),

tmclane22:06:59

you just need to build your object properly and it will serialize correctly.

barry22:06:05

i’m trying to find a way to create some of the includes/relationship serialization bits automatically

barry22:06:30

well, yes. building it correctly for complicated queries ins’t trivial.

tmclane22:06:39

that would be the job of your storage code in my opinion.

barry22:06:43

that’s why we get the big $, I suppose

barry22:06:57

fair enough

tmclane22:06:37

ha. yes. Too bad I'm not getting paid for this. It's a hobby project I am working on that has me trying to figure out what and how to get this helper layer to help me. Instead it seems to be making it harder.

tmclane22:06:46

I'll let you know what I figure out.

barry22:06:36

thanks, and good luck!

tmclane22:06:57

which library are you using?

tmclane22:06:14

are you using https://github.com/metosin/compojure-api? or ring-swagger itself?

barry22:06:28

I’m using compojure-api

tmclane22:06:42

@barry Got it to return whichever 'Content-Type' was requested using that format. This is with 2.0.0-alpha4.

barry22:06:00

that’s really useful

tmclane22:06:27

Seems ugly and wrong. Ok figured out how to change the default response type

tmclane22:06:30

if not requested.

tmclane22:06:31

I updated the code snippet