There are special use-cases that each language supports; this document pertains to C# models.
- Generate serializer and deserializer functionality
- Generate models with equals and GetHashCode methods
- Generate models with auto-implemented properties
- Change the collection type for arrays
- Generate custom enum value names
- Generate models with inheritance
- Generate models as records
- Generate code that handles nullable mode
The most widely used usecase for Modelina is to generate models that include serilization and deserialization functionality to convert the models into payload data. This payload data can of course be many different kinds, JSON, XML, raw binary, you name it.
As you normally only need one library to do this, we developers can never get enough with creating new stuff, therefore there might be one specific library you need or want to integrate with. Therefore there is not one specific preset that offers everything. Below is a list of all the supported serialization presets.
Here are all the supported presets and the libraries they use:
To include functionality that convert the models using the System.Text.Json, to use this, use the preset CSHARP_JSON_SERIALIZER_PRESET
.
Check out this example for a live demonstration.
External dependencies Requires System.Text.Json, System.Text.Json.Serialization, System.Text.RegularExpressions and Microsoft.CSharp version 4.7 to work.
To include functionality that convert the models using the Newtonsoft/Json.NET framework, to use this, use the preset CSHARP_NEWTONSOFT_SERIALIZER_PRESET
.
Check out this example for a live demonstration.
External dependencies
Requires Newtonsoft.Json
, Newtonsoft.Json.Linq
and System.Collections.Generic.
Currently not supported, let everyone know you need it!
Currently not supported, let everyone know you need it!
To overwrite the Equals
and GetHashCode
methods, use the preset CSHARP_COMMON_PRESET
and provide the options equal: true
and hashCode: true
Check out this example for a live demonstration.
Note that the Equals
method uses Object.Equals(objA, objB)
. This means that reference types properties that do not override their default Equals
method will perform a reference equality check to determine equality, returning false
if they do not reference the same object.
To generate auto-implemented properties (the ones with { get; set; }
accessors), use the preset CSHARP_COMMON_PRESET
and provide the option autoImplementedProperties: true
Check out this example for a live demonstration.
If you consider the Array Class to be insuitable for your situation, then you might look into setting the collectionType: 'List'
option to your instance of the generator. This will cause all of the collections to be rendered as of type System.Collections.Generic.IEnumerable<T>
.
Check out this example for a live demonstration.
When using AsyncAPI or JSON Schema, it is not possible to associate enum names with values however with extensions it is.
Check out this example for a live demonstration.
If you want the generated models to inherit from a custom class, you can overwrite the existing rendering behavior and create your own class setup.
Check out this example for a live demonstration.
Since C# 9 the language now supports records as an alternative to classes suitable for roles like DTO's. Modelina can generate records by setting the modelType: record
option. Note that this renderer does not support the autoImplementedProperties
option as this is default with records.
Check out this example for a live demonstration.
Since C# 8 the language now supports nullable reference types. Modelina can generate code that handles nullable mode by setting the handleNullable: true
option.
If your project use nullable, you should set this parameter to true
to avoid warnings.
Check out this example for a live demonstration.
This is the most asked questions and answers which should be your GOTO list to check before asking anywhere else. Cause it might already have been answered!
Often times you might encounter variables which as of type dynamic
or dynamic[]
, which is our fallback type when we cannot accurately find the right type.
If you are encountering this when your input is JSON Schema/OpenAPI/AsyncAPI, it most likely is because of a property being defined as having multiple types as a union, which the C# generator cannot natively handle and fallback to dynamic
.