Skip to content

Kiota should unconditionally generate C# classes with nullable reference types #3944

@0xced

Description

@0xced

Currently, Kiota generates C# classes with conditional compilation to use nullable reference types. For example:

// <auto-generated/>
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;

// […snip…]

        /// <summary>The Notes property</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
        public string? Notes { get; set; }
#nullable restore
#else
        public string Notes { get; set; }
#endif
        /// <summary>The subject property</summary>
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#nullable enable
        public string? Subject { get; set; }
#nullable restore
#else
        public string Subject { get; set; }
#endif

That's a lot of noise making the generated code hard to read. Instead, it should look like this:

// <auto-generated/>
#nullable enable
using Microsoft.Kiota.Abstractions.Serialization;
using System.Collections.Generic;

// […snip…]

        /// <summary>The Notes property</summary>
        public string? Notes { get; set; }
        /// <summary>The subject property</summary>
        public string? Subject { get; set; }

I know this has been already discussed in #2594 (comment) but I think the conclusion was wrong.

Nullable reference types, introduced in C# 8 are a compiler feature. So it's totally fine to use nullable reference types even when targeting lower than .NET Standard 2.1, such as .NET Standard 2.0 or .NET Framework 4.6.2, 4.7.x etc.

In order to make the code with nullable reference types compile, one simply has to explicitly specify the C# language version to at least 8.0 in the csproj file:

<PropertyGroup>
  <TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
  <LangVersion>8.0</LangVersion>
</PropertyGroup>

The requirement for nullable reference types to work is the version of the SDK that is used, not the target framework. C# 8 was introduced along with .NET Core 3.0 meaning that it will work with any SDK from 3.0.0 onwards. And, as stated in Kiota documentation, the .NET 8 SDK is required anyway.

Activity

added 2 commits that reference this issue on Dec 21, 2023
e862299
b750fa2
added a commit that references this issue on Dec 22, 2023
e418974
added this to the Kiota v1.11 milestone on Jan 8, 2024
added and removed
type:bugA broken experience
on Jan 8, 2024

55 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

CsharpPull requests that update .net codeWIPenhancementNew feature or request

Type

No type

Projects

Status

New📃

Relationships

None yet

    Participants

    @0xced@petriashev@remiX-@Suchiman@baywet

    Issue actions

      Kiota should unconditionally generate C# classes with nullable reference types · Issue #3944 · microsoft/kiota