Fork me on GitHub
#ring-swagger
<
2018-12-05
>
danielcompton09:12:04

Is there a way to use the resource function with :spec coercion and make a default value for a parameter?

danielcompton09:12:33

Using the macro syntax I would do {id :- :my/id nil}

danielcompton09:12:26

but I'm not sure how to do it in the map syntax. I've tried making the spec nilable, but that didn't work

danielcompton09:12:57

It looks like :parameters {:query-params {:x int?, :y int?}} doesn't make :y optional, like it did above

danielcompton09:12:58

Ah, I've found a way, use s/keys on the parameters with an :opt

danielcompton09:12:15

:parameters {:query-params ::input-settings}

danielcompton09:12:38

(s/def ::input-settings (s/and (s/keys :req-un [::endpoint
                                                ::requestor-id]
                                       :opt-un [::from-year
                                                ::requestor-name])))

ikitommi09:12:32

@danielcompton we just added the working default-value handling to schema-tools, could be added to spec-tools if there would be extra hours in days…. but you can declare the defaults to swagger already, they get injected into the ui: instead of just int? you can say (st/spec {:spec int? :swagger/default 1})

ikitommi09:12:24

optional key with data-spec syntax: :parameters {:query-params {:x int?, (ds/opt :y) int?}}

🆒 8
danielcompton10:12:44

Nice! That's very useful. Thanks!

ikitommi15:12:55

@kjothen not sure what you mean, swagger with compojure-api without the auto-generated route docs?

ikitommi15:12:09

spec-tools readme has an example how to use that without c-api. You can write the spec manually too, but the end result we be ~the same, right?

kjothen16:12:30

@ikitommi I posed the question because I’m using clojure.spec to define my message protocol front-to-back, but was struggling to generate swagger from routes using clojure.spec. However, I have it working now, but of course my swagger is now missing some niceties such as object definitions.

ikitommi18:12:31

@florinbraghis I think you can't have both names & the fnk syntax. Ideas welcome on this.

florinbraghis20:12:58

Our API has dozens of inline schemas and the only solution that kind of works is to use defschema to create named schemas and use those instead of the inline ones. Needless to say, that’s an almost total rewrite of our API and still there’s the issue of how to name these schemas. To automate this, I’ve ended up using alter-var-root to override ring.swagger.swagger2/ensure-body-and-response-schema-names and generate the schema name from the combination of method-path-body (for body-params) and method-path-response-code for responses. I’m not entirely happy with having to use alter-var-root — maybe there’s another place where I could plug my schema-name-generating function ?

florinbraghis16:12:56

A couple of ideas that could help: 1. There could be an optional callback that users can supply in the options, which receives this Swagger map that they can modify freely, called just before ensure-body-and-response-schema-names 2. Instead of BodyXXX and ResponseXXX, adopt a more predictable naming scheme for unnamed schemas. For example, MethodPathBody for body params inline schemas (eg. GetApiPlusBody, GetDocuments{id}Body) and MethodPathResponseCode for responses (eg. GetApiPlusResponse200 GetDocuments{id}Response401). 3. Both or something even better? What do you think about these solutions ?

ikitommi18:12:28

@florinbraghis I think providing the options is a good idea, also the callback for the schema naming. Are there any other (non-clojure) naming conventions for swagger out there? Would you like to do a PR for spec-tools?

ikitommi18:12:21

oh, you are using Schema. We started to port Schema->Swagger from ring-swagger to schema-tools. Not on par yet, but at some point will make c-api use that instead.

florinbraghis19:12:06

Thanks ikitommi, I’ll try to come up with a PR for this, we can take this discussion to github then!

ikitommi18:12:27

spec-integration is still kinda rough, as both coercion and spec walking are out of clojure.spec and we need to reverse-engineer a lot of things to get it working. Good to hear it works for you! Also looking forward to the spec object definitions.