Skip to content

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.

OptionCLI FlagDefaultDescription
Namespace-n, --namespaceGeneratedModelsC# namespace for generated types
GenerateDocComments--no-doc-commentstrueInclude XML doc comments
GenerateFileHeader--no-headertrueAuto-generated file header
DefaultNonNullable--no-default-non-nullabletrueDefaults as non-nullable
AddDefaultValuesToProperties--no-add-default-valuestrueAdd default values from OpenAPI to properties
UseImmutableArrays--mutable-arraystrueUse IReadOnlyList<T>
UseImmutableDictionaries--mutable-dictionariestrueUse IReadOnlyDictionary

Controls the C# namespace for all generated types.

CLI:

Terminal window
openapi-codegen spec.yaml -o Models.cs -n MyApp.Api.Models

Library:

new GeneratorOptions { Namespace = "MyApp.Api.Models" }

Output:

namespace MyApp.Api.Models;

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; }
}

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.

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.

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.

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; }

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; }