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

Enable nullability in generated projects #114

Open
NoTuxNoBux opened this issue May 25, 2023 · 1 comment
Open

Enable nullability in generated projects #114

NoTuxNoBux opened this issue May 25, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@NoTuxNoBux
Copy link

It would be nice if <Nullable>enable</Nullable> was automatically set in the generated csproj. Recent .NET versions are pushing hard to making this the default everywhere (the billion dollar mistake), and I noticed it only required minor changes to enable this in my project after the fact.

Some things to take into account if you do this, though:

  • Entity fields such as BaseEntity.CreatedBy need to become string? so nullability is now explicit, and EF will make these fields non-null otherwise now. This also applies to fields in general, but this is probably what you want (non-null database field for non-nullable C# property).
  • .NET will start validating non-nullable DTO fields as Required in the API automatically; omitting these fields in the request body will generate Bad Request errors, which is probably what you want and doesn't happen currently (e.g. omitting a field in PUT will have the same effect as explicitly setting it to null, which was misleading to me the first time and probably will be to other clients, too).
    • Similarly, FooParametersDto needs to make Filters and SortOrder a string? or they will always be required.
    • Ironically, adding [Required] attribute (not automatically added currently) is still useful or the OpenAPI specification won't mark the field as required despite .NET validating them at runtime.
  • Every DTO with non-nullable fields will spawn warnings about the fields potentially being null because there is no constructor and they can be fetched through get without setting them first through set. With .NET 7 you can do e.g. public required string FirstName { get; set; } to fix this if you don't want to spawn constructors everywhere.
@NoTuxNoBux NoTuxNoBux added the enhancement New feature or request label May 25, 2023
@pdevito3
Copy link
Owner

Yup this is on my list. Should be simple, just need to do it!

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

No branches or pull requests

2 participants