elide icon indicating copy to clipboard operation
elide copied to clipboard

Add ability to auto-pluralize root collections

Open cdeszaq opened this issue 7 years ago • 4 comments

Many clients, like Ember, expect pluralized root-level collections, and a number of other APIs / frameworks do this by default (ie. Ruby on Rails). Elide supports changing the by manually setting the type parameter on the @Include annotation, but this is the sort of thing that automation and defaults are well suited to handle. Here's how this looks today:

// Assume you have a class titled Report
@Include(rootLevel = true, type = "reports")

Instead of having to do this for every root-level object, it would be amazing if Elide had a property to turn on auto-pluralization of root-level collections by default.

The EntityDictionary likely holds most everything needed, and Apache Commons may even provide a pluralizer that can be used for this. At first glance, the EntityDictionary::getJsonAliasFor(Class<?> entityClass) method seems like the place to add this new capability, though there's concern that that method calls into an entity binding. If so, this would require modifying the entity binding to bind the right thing or else types could get confused.

Another oddity of this approach is that the type for all instances would always be plural, which is likely undesired, so it might be better to be smarter and pluralize / unpluralize where relevant. This would let the type be singular, while having it's root-level json alias be plural.

To accomplish this extra set of smarts, this may need to happen in the state machine, with StartState.java being the only piece that would care about this.

Either way, the general guidance for this feature would be to follow what Ember expects by default. An Ember app is usually backed by a JSON-API, often served by a Ruby on Rails web service, and that is likely a common-enough pattern that other plural-expecting clients will be very similar or even the same with what they expect.

cdeszaq avatar Apr 19 '17 17:04 cdeszaq

Another thing to consider is the general formatting of the type beyond the inflection. For example some clients could expect the path to be formatted with camel case, underscores, or dasherized.

The reason Ember Data wants a camelized plural type is because that is the default implementation of pathForType

...
export default DS.RESTAdapter.extend({
  pathForType: function(modelName) {
    var camelized = Ember.String.camelize(modelName);
    return Ember.String.pluralize(camelized);
  }
});

It might be worth considering the addition of a similar hook to allow for any transformations to the type/path.

jkusa avatar Apr 19 '17 17:04 jkusa

This would be a helpful feature, ideally the default behavior would be part of settings but override-able in @Include.

Is there an easy hack to pluralize locally until the feature is completed?

sdhawley avatar Mar 07 '18 19:03 sdhawley

Unfortunately not. You can use @Include to rename the entity to the plural form to make the URLs pretty but that has the side effect of changing the type field to the plural form

clayreimann avatar Mar 07 '18 19:03 clayreimann

There are three components to this ticket:

  1. Adding a global setting for spring & standalone to turn on/off this feature because it changes the API. Turning on the feature could set the pluralizer class that will be used to pluralize the type.
  2. The EntityDictionary is going to need new methods to lookup a type by its plural form or get the plural form for a given type.
  3. Both StartState and SwaggerBuilder will require changes to parse/build URL paths respectively using the plural form for a given type.

aklish avatar Mar 11 '21 21:03 aklish