Add Servicestack Reference - Nullable type information missing

Hello,

I’m adding a Servicestack-Reference via VisualStudio to my C#-Project. But the nullable-type (’?’) information is missing in the generated DTO - I guess it’s some simple setting, but I can’t find it.

cheers Michael

All Reference Types are nullable in .NET, DTOs do not emit nullable annotations, but all DTO properties are nullable by design which are instantiated with reflection from serialization.

My problem is, that the UI-Framework I’m using (DevExtreme) marks not-nullable fields as required, so I ended up in using my Service-Stack model directly and not via AddReference, for now thats ok, but maybe that would be a suggestion for an option to the Reference-Generator, especially since C# 8.0 Nullable types deserve more “explizit attention” :slight_smile:

cheers

Michael

I’ve added support for this in the latest v5.11.1 that’s now available on MyGet where you can generate nullable references for all but required types by opting in with:

CSharpGenerator.UseNullableAnnotations = true;

However because they’re just annotations they should be used in conjunction with a Validation rule to assert that API Requests match the state of your code-base, using either a .NotNull() Fluent Validation rule or [ValidateNotNull] or [ValidateNotEmpty] declarative validation attribute, e.g:

public class MyRequest
{
    [ValidateNotNull]
    public string A { get; set; }
    public string? B { get; set; }
}
1 Like

O.M.G. you are such a machine, Demis - best support on the planet :slight_smile:

1 Like

FYI made a slight tweak to limit this behavior to reference types so it doesn’t affect existing Value Types behavior if you refresh the latest v5.11.1.