Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable Reference types #1848

Closed
gaboe opened this issue Oct 2, 2020 · 5 comments
Closed

Nullable Reference types #1848

gaboe opened this issue Oct 2, 2020 · 5 comments

Comments

@gaboe
Copy link

gaboe commented Oct 2, 2020

Is there any effort to generate correct types for null reference types?

For example:

public class CommentDto
{
    public int CommentID { get; set; }

    public int BusinessCaseID { get; set; }

    public int SenderID { get; set; }

    public string Text { get; set; } = string.Empty;

    public UserDto? Sender { get; set; }
}

public class UserDto
{
    public UserDto()
    {
        Login = string.Empty;
        FirstName = string.Empty;
        LastName = string.Empty;
    }

    public int UserID { get; set; }

    public string Login { get; set; }

    public string? Phone { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime? LastSignInDate { get; set; }

    public string? AvatarUrl { get; set; }
}

will be generated as:

image

There are multiple problems in this generated code:

  1. Every C# string either nullable or non-nullable is generated as nullable
  2. Every object type either nullable or non-nullable is generated as non-nullable.

I saw some hacks/extensions like this #1487, but in my opinion, this is a pretty basic feature and should be implemented in this library.

Thank you for your answer.

@gaboe
Copy link
Author

gaboe commented Oct 12, 2020

Unfortunately, we have moved to nswag. It works correctly and migration have taken us only a few hours.

@domaindrivendev
Copy link
Owner

Thanks for the feedback, and sorry to see you go. For anyone else reading this thread, rest assured support for Nullable Reference Types is top of the list for Swashbuckle and something I hope to get out very soon.

@jcracknell
Copy link
Contributor

This actually warrants some careful consideration with regards to how this is implemented.

System.Text.Json currently blows up when attempting to deserialize a null struct, but permits a nulls for non-null reference types - despite the fact that default(T) is (arguably) a valid instance of a struct T, but not for reference type T (dotnet/runtime#1256).

Under the current behavior, there's an argument that all reference types in your serialization model are required to be nullable.

@Zero3
Copy link

Zero3 commented Nov 4, 2020

(I think this issue is a duplicate of #1686)

@domaindrivendev
Copy link
Owner

Closing because (as stated by @Zero3) this is a dup of #1686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants