Giter Site home page Giter Site logo

Allow custom route names about sinja HOT 4 OPEN

mwpastore avatar mwpastore commented on May 23, 2024
Allow custom route names

from sinja.

Comments (4)

jgnagy avatar jgnagy commented on May 23, 2024

This may be the solution to my problem, but is there no way to stop sinja from deciding where to put dashes in controller names (routes)? I have updated my test repo @ https://github.com/jgnagy/test-api (which happily works now thanks to #12 being resolved), but it takes the TestKey and forces the route to be /test-key. What if I didn't want a dash, and instead simply wanted /testkey? Is this configurable? If not, maybe this is something I could help with / submit a PR for.

from sinja.

mwpastore avatar mwpastore commented on May 23, 2024

Sinja draws the routes based on the symbol passed to the resource keyword. The symbol is dasherized and pluralized. Currently, there's no way to override this behavior, which is what this issue is intended to track. It's something I'm willing to prioritize if it helps an early adopter!

All that being said, your resource is named :keys, so the resource route should be /keys, not /test-keys. I think you may be looking at the links in the response payload. These are actually generated by JSONAPI::Serializers, and it is customizable! Just override the type method in your serializer class:

class TestKeySerializer
  include JSONAPI::Serializer

  attribute :name
  attribute :created_at

  def type
    'testkey'
  end
end

More information is available here.

This does create a bit of a problem because the links in the payload must line up with the routes drawn in the Sinja application (or else the consuming application may try to access a URL that doesn't exist on a subsequent request). You can kinda-sorta "customize" the Sinja routes by passing in a different symbol, for example resource :testkey (/testkeys) instead of resource :test_key (/test-keys). Unfortunately, as I described above, there's currently no way to get /testkey due to the pluralization rule.

from sinja.

jgnagy avatar jgnagy commented on May 23, 2024

Yes, that helped quite a bit for test scenario (I committed my update to reflect that), but unfortunately I think it needs to be done at a deeper level in sinja because it is failing in my application while including a related has_many resource. In my real use-case, I have GET /users/1?include=apikeys%2Cidentities (bringing along both apikeys and identities), but the response payload doesn't seem to rely on the underlying APIKey's type instance method when creating the has_many collection.

I am trying to build an app that relies on your library to implement the {json:api} spec, so I don't know if that makes me an early adopter, but I would certainly appreciate this customization being available. I can successfully POST to /apikeys and create a new key, but subsequently listing a user's keys produces unexpected results. Going from dashes to underscores in a consistent manner across libraries and possible languages is difficult, so being able to explicitly dictate the route would be helpful.

from sinja.

mwpastore avatar mwpastore commented on May 23, 2024

Nothing you've described makes me think you need this feature. Do you have some time to chat with me on Gitter or somewhere else so I can help you work through this use case?

from sinja.

Related Issues (19)

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.