Kaladel.Kiota.NullabilityFix.CSharp 1.1.0

dotnet tool install --global Kaladel.Kiota.NullabilityFix.CSharp --version 1.1.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local Kaladel.Kiota.NullabilityFix.CSharp --version 1.1.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Kaladel.Kiota.NullabilityFix.CSharp&version=1.1.0
                    
nuke :add-package Kaladel.Kiota.NullabilityFix.CSharp --version 1.1.0
                    

Kaladel.Kiota.NullabilityFix.CSharp

NuGet

A small .NET tool that post-processes Kiota-generated C# API clients so that model properties correctly reflect the required constraint from the source OpenAPI document.

The problem

Kiota deliberately generates every C# model property as nullable, regardless of whether the property is listed under required in the OpenAPI schema. This is the same known, intentional limitation of Kiota's generators that affects the TypeScript generator (see Kaladel.Kiota.NullabilityFix.TypeScript), but it means consumers of the generated client lose a significant amount of compile-time safety: there's no way for C# to tell you that a property is guaranteed to be present on a response, or that it must be supplied on a request.

This tool closes that gap. After Kiota generates (or regenerates) your client, run this tool against the generated Models folder and the OpenAPI document that produced it, and it will:

  • Tighten any property listed in the schema's required array (and not also marked nullable) from a nullable to a non-nullable C# property.
  • Leave properties that are required and nullable (OpenAPI 3.1 type: ["string", "null"]-style nullability) untouched, since they may legitimately be null even though they must be present.
  • Leave all other (non-required) properties untouched.

The tool operates as a targeted, line-based text rewrite of Kiota's generated output rather than a full C#/Roslyn parser/AST transform. This is deliberate: the input is fully machine-generated, highly regular boilerplate (one class per schema, one property per block, consistent formatting), which makes this approach both safe and far simpler than depending on a full Roslyn-based toolchain.

Installation

Install as a .NET global tool:

dotnet tool install --global Kaladel.Kiota.NullabilityFix.CSharp

This makes the kiota-nullfix-cs command available on your PATH. You can also install it as a local tool scoped to a repository (via a tool manifest):

dotnet new tool-manifest # if you don't already have one
dotnet tool install Kaladel.Kiota.NullabilityFix.CSharp

Usage

Run the tool after generating (or regenerating) your Kiota C# client, pointing it at the OpenAPI document used to generate the client and the directory Kiota generated into:

kiota-nullfix-cs --openapi ./openapi.json --kiota-output ./src/generated
Option Required Description
--openapi Yes Path to the OpenAPI document that was used to generate the Kiota C# client.
--kiota-output Yes Path to the directory Kiota generated the C# client into (the directory containing a Models subfolder).

The tool rewrites every *.cs file under the Models folder in place and prints a short summary of how many files/classes were scanned and how many properties were tightened.

Typical workflow

Add a step after your existing kiota generate invocation (e.g. in an MSBuild target, Makefile, or CI pipeline):

kiota generate --openapi ./openapi.json --output ./src/generated --language csharp
kiota-nullfix-cs --openapi ./openapi.json --kiota-output ./src/generated

Re-run both commands whenever the OpenAPI document or generated client changes, since the rewrite is applied directly to Kiota's output and is not preserved across regeneration.

Example

Given an OpenAPI schema where email is required and non-nullable, and subscribedToNewsletter is required and non-nullable:

{
  "type": "object",
  "required": ["email", "subscribedToNewsletter"],
  "properties": {
    "email": { "type": "string" },
    "subscribedToNewsletter": { "type": "boolean" }
  }
}

Kiota generates:

public partial class AccountPostRequest : IAdditionalDataHolder, IParsable
{
    /// <summary>The email property</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
    public string? Email { get; set; }
#nullable restore
#else
    public string Email { get; set; }
#endif
    /// <summary>The subscribedToNewsletter property</summary>
    public bool? SubscribedToNewsletter { get; set; }
}

After running this tool:

public partial class AccountPostRequest : IAdditionalDataHolder, IParsable
{
    /// <summary>The email property</summary>
    public string Email { get; set; }
    /// <summary>The subscribedToNewsletter property</summary>
    public bool SubscribedToNewsletter { get; set; }
}

Limitations

  • Drops down-level target framework compatibility for tightened properties. Kiota wraps nullable reference type properties in an #if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER / #else block so the generated client can also be compiled against older target frameworks that don't understand nullable reference type annotations. This tool collapses that conditional block down to a single, non-conditional, non-nullable property declaration for any property it tightens. If your project needs to support netstandard2.0 or similarly old target frameworks, do not run this tool, or expect those build configurations to no longer compile for tightened properties.
  • Only targets *.cs files under the Models subfolder of the Kiota output directory; it does not modify request builders or other generated files.
  • Relies on Kiota's current, highly regular C# output format. Significant changes to Kiota's generator output format in future versions may require updates to this tool.
  • Schema names are matched by the OpenAPI component schema name, so this will not help with inline (non-referenced) schemas that Kiota names based on request/response context, if those names diverge from the component schema name.

Contributing

Issues and pull requests are welcome. This is a small, focused tool — before proposing new functionality, please open an issue to discuss the change first.

License

Licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last Updated
1.1.0 128 11/10/2026
1.0.0 85 11/10/2026

Initial release.