scott-mcdonald / JsonApiFramework

JsonApiFramework is a fast, extensible, and portable .NET framework for the reading and writing of JSON API documents. Currently working on ApiFramework 1.0 which is a new framework that supports the many enhancements documented in the 2.0 milestone of this project while being media type agnostic but will support media types like {json:api} and GraphQL for serialization/deserialization purposes.
Other
96 stars 11 forks source link

Pluralization #66

Closed joshjeppson closed 5 years ago

joshjeppson commented 5 years ago

I'm switching our code base over from REST to JSONAPI using this framework. We made the decision long ago to use persons as the plural of person, not people. In our Ember clients this is easily overridden using an irregular inflector setting. Is there a way within this framework to override pluralization for only one specific instance like this?

scott-mcdonald commented 5 years ago

There is a pluralization convention for API resource identity type names so I am assuming you are using this convention first of all. But you can always explicitly set any service model or schema level properties which does take precedence over any conventions.

So for your example of Person it would be something like the following:

public class PersonConfiguration : ResourceTypeBuilder<PersonResource>
{
    // PUBLIC CONSTRUCTORS //////////////////////////////////////////////
    #region Constructors
    public PersonConfiguration()
    {
        // Hypermedia
        this.Hypermedia()
            .SetApiCollectionPathSegment("persons");

        // Resource Identity
        this.ResourceIdentity(x => x.Id)
            .SetApiType("persons"); // This is explicitly setting json:api "type" to "persons"

        // Attributes

        // Relationships
        this.ToOneRelationship<ProfileResource>("profile");

        // Ignored
        this.Attribute(x => x.ProfileId).Ignore();
        this.Attribute(x => x.Profile).Ignore();
    }
    #endregion
}
joshjeppson commented 5 years ago

Thanks! Exactly what I needed.