A dotnet CLI to automatically generate strongly typed C# models/DTO's from Contentful contenttypes.
The generated classes are based on the content-types in your Contentful space/environment and are generated as .NET/C# partial records with properties that match the (required or optional) fields in the content-types.
The generated files do have a dependency on Contentful.Core.Models in the contentful.csharp-package.
This project is a clone and refactor of the Contentful.ModelsCreator.Cli that at the time of creating this project ran on .NET Core 2.1.
This repository is now a public archive. The Contentful.ModelsGenerator.Cli is a .NET 8.0/9.0 project and is intended to be used in projects with the same or newer version number. It fixes some issues in the original project and adds some new features like:
- using records
- making required/optional explicit
- generate the types as partial
- generate the types with an internal or public access modifier
The CLI tool uses the "global tools" feature of .NET 8.0/9.0 and requires the .NET SDK to be installed: https://dotnet.microsoft.com/en-us/download/dotnet/ .
Before installation the project must be published and packed (default to Nuget). Otherwise, run this command as a console app (Debug | Start without debugging).
Run dotnet tool install -g contentful.modelsgenerator.cli from your command line.
Once installed you should now be able to run contentful.modelsgenerator.cli --help to list all the available commands.
-
Miscelenious parameters:
-hto list this help-vto show the version of the tool installed
-
Parameters for communicating with Contentful:
-kor-apikey: sets the Contentful (delivery) Api Key. Required.-sor-spaceid: sets the Contentful Space id of your Contentful instance Required.-eor-environment: specifies the environment that contains the content-types that need to be generated. Optional, defaults tomaster.
-
Code generation parameters:
-nsor-namespace: sets the namespace for the classes being created, Optional, defaults toContentful.Models.-for-force: a flag that forces an overwrite of any existing files. Optional, default istrue.-por-path: sets the output folder where the class files should be generated. Optional, defaults to aModels-folder under the current directory.-ior-internal: a flag to generate the classes with an internal access modifier. Optional, is defaultfalse. If omitted classes will be generated with apublicaccess modifier. If-iis provided, aninternalaccess modifier is used.
Running contentful.modelsgenerator.cli -k YOUR_API_KEY -s YOUR_SPACE_ID -e YOUR_ENVIRONMENT will create a number of public classes (records) in a Models-folder under your current working directory.
If you want to specify the namespace of the created classes use the -ns switch: contentful.modelsgenerator.cli -k YOUR_API_KEY -s YOUR_SPACE_ID -e YOUR_ENVIRONMENT -ns MyProject.ASubNamespace.Models
If you want to specify the path to create the DTO's in use the -p switch: contentful.modelsgenerator.cli -k YOUR_API_KEY -s YOUR_SPACE_ID -e YOUR_ENVIRONMENT -p c:\\\\projects\\MyProject\\ASubNamespace\\Models
If you want to run the tool direclty from within Visual Studio, add a Properties\launchSettings.json-file to the project with the following content:
{
"profiles": {
"ModelsGenerator.Cli": {
"commandName": "Project",
"commandLineArgs": "-k MY_APIKEY -s MY_SPACE_ID -e MY_ENVIRONMENT"
}
}
}The generated code contains, besides one file per content type, also a file called GeneratedContentTypeResolver.g.cs.
For the Contentful client to be able to deserialize the content types, this file is needed.
It also is advised to use the ResolveEntriesSelectively-option in the ContentfulOptions-object when creating the client.
See the code below:
public IContentfulClient BuildClient()
{
var options = new ContentfulOptions
{
DeliveryApiKey = "MY API KEY",
SpaceId = "MY SPACE ID",
Environment = "My ENVIRONMENT",
UsePreviewApi = false,
ResolveEntriesSelectively = true,
};
return new ContentfulClient(factory.CreateClient(nameof(ContentfulClientBuilder)), options)
{
ContentTypeResolver = new GeneratedContentTypeResolver()
};
}Note: A big thank you to @kheurterincentro for explaining some of the hidden SDK internals and providing the initial implementation of the
ContentTypeResolverWriter.
An implementation example can be found here.