/Contentful.ModelGenerator.Cli

A dotnet tool to generate c# classes representing your Contentful model types.

Primary LanguageC#MIT LicenseMIT

Contentful Model Generator tool

A dotnet tool to generate c# classes representing your Contentful model types.

Install

This CLI is installed via dotnet tools feature. To install simply run

$ dotnet tool install contentful-model-generator

Usage

After installing you can invoke the tool through the command line. You can list all available parameters using:

$ contentful-model-generator generate --help 

Options

  • -a|--api-key: Mandatory. The Contentful API key for the Content Delivery API.
  • -s|--space: Mandatory. The Space ID to fetch content model from.
  • -e|--environment: The Environment to fetch the content model from. Default to "master".
  • -n|--namespace: The namespace of the generated classes. Defaults to "Contentful.ContentModels".
  • -p|--path: Path to the directory where the files will be created. Defaults to the current directory.
  • -f|--force: Overwrite already existing files. False by default.
  • -h|--help: Shows help text.

Full usage example

$ contentful-model-generator generate --api-key <your-key> --space <your-spaceId> --environment "master" --namespace "Contentful.ContentModels" --path "./Data/Models" --force

This will generate content models for environment master with namespace Contentful.ContentModels in a folder ./Data/Models relative to the current directory.

Generated models

A typical generated model look like the following:

MyContentType.Generated.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by Contentful.ModelGenerator.Cli tool.
//     Runtime Version: 1.0.0.0
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contentful.Core.Models;

namespace Contentful.ContentModels
{
    public partial class MyContentType
    {
        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public const string ContentTypeId = "myContentType";

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public SystemProperties Sys { get; set; }

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public Document RichTextField { get; set; }

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public Asset Picture { get; set; }

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public IEnumerable<ChildItem> Items { get; set; }

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public object ModelReference { get; set; }

        [GeneratedCode("Contentful.ModelGenerator.Cli", "1.0.0.0")]
        public async Task<string> RenderRichTextFieldAsync()
                => await new HtmlRenderer().ToHtml(RichTextField);
    }
}

Features

ContentTypeId

A reference to the content type id string. This is useful to be used with the Contentful C# client.

partial class

The generated types are defined as partial classes, to allow you to implement extra details in other files.

GeneratedCode attribute

Marking the auto-generated parts of the class with this attribute allows third party analyzers to ignore it for example to avoid styling or code coverage issues.

RenderXXXAsync()

If your content type has any rich text field, it will be represented with a Document property, and a convenient method to render the Html markup using the HtmlRenderer util from Contentful. It will return a plain string, so if you plan to use it for example inside a razor page, remember to allow the string to print using @Html.Raw(...).

Known issues

Due to API limitations, it is not possible to determine for a single reference field what type relates to. So at this time the tool generates an object field.

This is a snippet you can implement to patch this behavior using this code:

MyContentType.cs

using Newtonsoft.Json.Linq;

namespace Contentful.ContentModels
{
    public partial class MyContentType
    {
        public ModelReference GetModelReference()
        {
            if (this.ModelReference is JObject o)
                return o.ToObject<ModelReference>();
            return null;
        }
    }
}

Built With

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Diego RodrĂ­guez - Initial work - RodriBus

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

This proyect is a conceptual fork from contentful/dotnet-models-creator-cli, some of the code is inspired by their implementation.