Coder Social home page Coder Social logo

Comments (9)

Deraen avatar Deraen commented on August 29, 2024

I guess your wrap-dashed-params middleware is only editing request :params. You should edit :query-params and :body-params because those are the parts of request that Compojure-api reads.

from compojure-api.

arnab avatar arnab commented on August 29, 2024

Ah, that makes sense. AFK. I'll try it out later.

Thanks for the quick reply.

I guess your wrap-dashed-params middleware is only editing request
:params. You should edit :query-params and :body-params because those are
the parts of request that Compojure-api reads.


Reply to this email directly or view it on GitHub
#40 (comment)
.

from compojure-api.

arnab avatar arnab commented on August 29, 2024

Hi @Deraen, I changed my middleware and got the transliteration done. However, compojure-api (rather swagger and prismatic-schema) fails the incoming HTTP request as invalid. That's because

  • the schema specifies per_page as the param.
    • That's what I want the swagger doc and api to look like, so it's natural from JS or Rails to call from.
  • The incoming HTTP request indeed sends per_page and that's fine.
  • But the middleware now changes it to per-page and that does not match the schema anymore.
    • I want my app's clojure code to work with idiomatic dasherized names.

Is there a way around this (short of calling something like this on every route):

(ok (underscorize (api/index (dasherize params))))

I could write a HOF to make the duplication less, but that's still not an ideal solution, IMO. I basically want something that allows the schema/api to have _s and the internal of my app to deal with - instead of _s.

from compojure-api.

ikitommi avatar ikitommi commented on August 29, 2024

I would resolve this with overriding compojure-api restucturing dispatch functions :query and :query-params. Like this:

(original here):

(require '[compojure.api.meta :as meta])

(declare dasherize) ;; implement
(declare un-dasherize) ;; implement

(defmethod meta/restructure-param :query-params [_ query-params acc]
  (let [schema (meta/fnk-schema query-params)
        pimped (un-dasherize schema)]
       (-> acc
           (update-in [:parameters :parameters] conj {:type :query :model pimped})
           (update-in [:letks] into [query-params `(dasherize ~(meta/src-coerce! pimped :query-params :query))]))))

with this, you can say:

(GET* "/" []
  :return Long
  :query-params [per-page :- Long]
  (ok per-page))

... while the input models (& swagger-docs) have the per_page.

more info at doing your own restructuring at: https://github.com/metosin/compojure-api#creating-your-own-metadata-handlers

hope this helps.

from compojure-api.

ikitommi avatar ikitommi commented on August 29, 2024

works for me, will close this. please reopen if still need help with this one.

cheers.

from compojure-api.

arnab avatar arnab commented on August 29, 2024

Thanks, I forgot to reply here. We ended up writing an HOF to do the camel-case on the way in and underscore on the way out.

from compojure-api.

ikitommi avatar ikitommi commented on August 29, 2024

ok, great to hear you got it working. But what is HOF?

from compojure-api.

ikitommi avatar ikitommi commented on August 29, 2024

a higher order function, found it :)

from compojure-api.

arnab avatar arnab commented on August 29, 2024

Heh. sorry :) I thought that was lingua franca in Clojure :)

from compojure-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.