umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.42k stars 2.67k forks source link

MaybeNull attribute should not be generated on mandatory fields #15628

Open josefkvist opened 8 months ago

josefkvist commented 8 months ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.0.3

Bug summary

I find it wrong the [return: global::System.Diagnostics.CodeAnalysis.MaybeNull] attribute is set on properties that are mandatory. If this is meant to be, I would like to know why. If not, I would gladly like to help to fix this.

Specifics

No response

Steps to reproduce

  1. Create a new type of some sort
  2. Select a textbox, give it a name and make it mandatory
  3. Generate models

Expected result / actual result

The maybenull attribute should not be on the property

github-actions[bot] commented 8 months ago

Hi there @josefkvist!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

abjerner commented 8 months ago

It's probably worth arguing whether the attributes for nullability should always be 100% correct, or just be somewhat correct.

You could have a scenario where a property isn't initially mandatory, then a lot of content is created, and then the property is then afterwards made mandatory. In this case you could still have properties that may be null even though the property is now mandatory. So I think the adding the attribute is correct.

elit0451 commented 7 months ago

Hi both 👋

And thanks for reaching out @josefkvist!

We needed the MaybeNull attribute because the Models Builder does not fully respect NullableReferenceTypes. Ideally, we would like to fix this:

If the property holds a string value, for example, then:

And so on 🙂 I will mark the task as up for grabs

github-actions[bot] commented 7 months ago

Hi @josefkvist,

We're writing to let you know that we would love some help with this issue. We feel that this issue is ideal to flag for a community member to work on it. Once flagged here, folk looking for issues to work on will know to look at yours. Of course, please feel free work on this yourself ;-). If there are any changes to this status, we'll be sure to let you know.

For more information about issues and states, have a look at this blog post.

Thanks muchly, from your friendly Umbraco GitHub bot :-)

josefkvist commented 7 months ago

Hi @elit0451,

I have started to look at this now. I have a couple of questions about what path I should choose.

  1. Nullable properties result in a warning in generated files, see this. If you go by the #nullable enable, I'm not sure how that will impact older versions and .NET-versions. \ The other option is to remove the MaybeNull attribute on the properties that is mandatory. Maybe this is the way to go?\ Or maybe we could add a opt-in-flag to the modelbuilder and if it is true, we use #nullable enable?
  2. Another note is that no matter what type of property, the same logic is applied, should I implement it on all types then?
  3. How am I supposed to test this? I can't find anything in the contribution-docs about what the requirements are.
bjarnef commented 7 months ago

Also see this discussion about how nullable properties should be handle in Umbraco (with/without ModelsBuilder): https://github.com/umbraco/Umbraco-CMS/discussions/13816

elit0451 commented 7 months ago

Hey again @josefkvist, and thanks for being patient with my reply 🙌 (busy days 🙈)

Here are the answers that I got from my expert colleague in this area:

  1. It will be breaking for those having nullability enabled, and thus should only be fixed in a major;
  2. Yes all types(properties?) are inherently nullable if they aren't mandatory;
  3. Testing is a hard one so not sure, maybe we have some ModelsBuilder generator tests you can have inspiration from?

But before you proceed, I would encourage you to join the discussion @bjarnef linked (thanks for that Bjarne 💪) as it is definitely the right place to clarify such unknowns 🙂