Understand the structure of generated C# models
masterThe SpecGen tool generates C# models that combine both QueryString parameters and JSON body models into a single object. This design simplifies the calling API by allowing a single parameter object to represent all data required for a remote API call.
Query String Parameters
Parameters intended for the URL query string are decorated with [QueryStringParameter]. To handle optionality, the tool uses nullable types (e.g., bool?). This allows the client to distinguish between a parameter being absent versus being explicitly set to a default value like false.
Example of a model with query string parameters:
[DataContract]
public class ContainerAttachParameters
{
[QueryStringParameter("stream", false, typeof(BoolQueryStringConverter))]
public bool? Stream { get; set; }
[QueryStringParameter("stdin", false, typeof(BoolQueryStringConverter))]
public bool? Stdin { get; set; }
}JSON Body Parameters
Parameters intended for the request body are decorated with [DataMember]. The EmitDefaultValue = false setting ensures that if a property's value matches its C# default value, it is omitted from the resulting JSON payload, preventing unnecessary data from being sent to the Docker engine.
Example of a model with JSON body parameters:
[DataContract]
public class Config
{
[DataMember(Name = "Hostname", EmitDefaultValue = false)]
public string Hostname { get; set; }
[DataMember(Name = "Domainname", EmitDefaultValue = false)]
public string Domainname { get; set; }
}Customizations (Enums and Types)
SpecGen allows for custom serialization logic to improve API usability. For example, it can map integer values from the engine-api to strongly-typed C# enums. This is achieved via a typeCustomizations map within the tool's source code (specgen.go).
using System.Runtime.Serialization;
namespace Docker.DotNet.Models
{
[DataContract]
public class ContainerAttachParameters
{
[QueryStringParameter("stream", false, typeof(BoolQueryStringConverter))]
public bool? Stream { get; set; }
[QueryStringParameter("stdin", false, typeof(BoolQueryStringConverter))]
public bool? Stdin { get; set; }
}
}