OpenAPITools / OpenAPITools/openapi-generator

[REQ] [C#] [Csharp] Support `Nullable``enable` for RestSharp client when `nullableReferenceTypes` enabled

Open
#18,341 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

When nullableReferenceTypes is set to true, the default RestSharp client only supports the least strict nullable context, annotations:

https://github.com/OpenAPITools/openapi-generator/blob/453facc81c322155a7309738352480281bd325ea/modules/openapi-generator/src/main/resources/csharp-functions/Project.csproj.mustache#L12

As described here:

the annotations level is not very strict and cannot perform the actual nullability compile checks.

If a user enables <Nullable>enable</Nullable> in the .csproj, the generated OpenAPI client will throw a lot of compile time warnings due to generated Model code that looks like:

MyModel myModel = default

This is an error in nullable context, because MyModel is assigned a default value of null, but is not marked MyModel?.

Also, any external uses of the API from nullable contexts that pass in a type such as string? will generate a compile warning because the OpenAPI client code will mark the field as string even though it can be null.

Describe the solution you'd like

An option so that a user can select RestSharp client and enable <Nullable>enable</Nullable> in the generated .csproj and have the client be valid. New clients such as generichost are not an option to some users who are targeting .NET Framework (but also still using a new C# language version, such as C# 12).

Likely, much of the Model could should use ? types when nullableReferenceTypes is set true. Currently it does not.

Describe alternatives you've considered

Allow generichost client to support .NET Framework 4.8, which seems to already support Nullable enable:

https://github.com/OpenAPITools/openapi-generator/blob/453facc81c322155a7309738352480281bd325ea/samples/client/petstore/csharp/generichost/net8/AnyOf/src/Org.OpenAPITools/Org.OpenAPITools.csproj#L20

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/csharp-functions/Project.csproj.mustache and inspect the generated C# Model code referenced in the issue. Generate a RestSharp client with nullableReferenceTypes enabled, then compile it with enable and check that nullable warnings are addressed. Done means the generated client is valid in the nullable context, including external nullable values.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.