Configuration
OpenAPI Code Generator provides a rich set of options to customize the generated C# output. Options can be set via CLI flags or programmatically through GeneratorOptions.
Options Overview
Section titled “Options Overview”| Option | CLI Flag | Default | Description |
|---|---|---|---|
Namespace | -n, --namespace | GeneratedModels | C# namespace for generated types |
GenerateDocComments | --no-doc-comments | true | Include XML doc comments |
GenerateFileHeader | --no-header | true | Auto-generated file header |
DefaultNonNullable | --no-default-non-nullable | true | Defaults as non-nullable |
AddDefaultValuesToProperties | --no-add-default-values | true | Add default values from OpenAPI to properties |
UseImmutableArrays | --mutable-arrays | true | Use IReadOnlyList<T> |
UseImmutableDictionaries | --mutable-dictionaries | true | Use IReadOnlyDictionary |
Namespace
Section titled “Namespace”Controls the C# namespace for all generated types.
CLI:
openapi-codegen spec.yaml -o Models.cs -n MyApp.Api.ModelsLibrary:
new GeneratorOptions { Namespace = "MyApp.Api.Models" }Output:
namespace MyApp.Api.Models;XML Doc Comments
Section titled “XML Doc Comments”When enabled, OpenAPI description fields are converted to XML doc comments on the generated types and properties.
CLI: Enabled by default. Disable with --no-doc-comments.
Example output with doc comments:
/// <summary>/// A pet available in the store/// </summary>public record Pet{ /// <summary> /// The unique identifier for the pet /// </summary> [JsonPropertyName("id")] public long Id { get; init; }}Without doc comments:
public record Pet{ [JsonPropertyName("id")] public long Id { get; init; }}File Header
Section titled “File Header”When enabled, adds an auto-generated comment header to prevent accidental manual editing:
// <auto-generated>// This file was auto-generated by OpenApiCodeGenerator.// Do not make direct changes to the file.// </auto-generated>CLI: Enabled by default. Disable with --no-header.
Default Non-Nullable
Section titled “Default Non-Nullable”When enabled, properties with default values in the OpenAPI spec are treated as non-nullable.
CLI: Enabled by default. Disable with --no-default-non-nullable.
Add Default Values to Properties
Section titled “Add Default Values to Properties”When enabled, properties with default values in the OpenAPI spec will have those default values added to the generated C# properties.
CLI: Enabled by default. Disable with --no-add-default-values.
Immutable Collections
Section titled “Immutable Collections”Arrays
Section titled “Arrays”Controls whether array types use IReadOnlyList<T> (immutable) or List<T> (mutable).
CLI: Immutable by default. Use --mutable-arrays for List<T>.
Immutable (default):
public IReadOnlyList<string> Tags { get; init; }Mutable:
public List<string> Tags { get; init; }Dictionaries
Section titled “Dictionaries”Controls whether dictionary types use IReadOnlyDictionary<string, T> or Dictionary<string, T>.
CLI: Immutable by default. Use --mutable-dictionaries for mutable.
Immutable (default):
public IReadOnlyDictionary<string, object> Metadata { get; init; }Mutable:
public Dictionary<string, object> Metadata { get; init; }