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.5k stars 2.69k forks source link

ModelsBuilder GetModelPropertyType fails when the "itemType" is "Element" #14694

Closed hfloyd closed 1 year ago

hfloyd commented 1 year ago

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

10.6.1

Bug summary

I have a Composition Document type, lets call it "CompMyData". It has some properties, and is on some Document types used for nodes.

I have some code like this, to get the exact alias of a composition property: var aliasMyProp = CompMyData.GetModelPropertyType(_publishedSnapshotAccessor, x => x.MyProperty).Alias;

but it throws an error when it runs:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'itemType')
   at Umbraco.Cms.Infrastructure.ModelsBuilder.PublishedModelUtility.GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor, PublishedItemType itemType, String alias)
   at MyNamespace.Models.CompMyData.GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor) ...

I am using Limbo ModelsBuilder for the model generation, and the code in question looks like this:

 public static IPublishedPropertyType GetModelPropertyType<TValue>(IPublishedSnapshotAccessor publishedSnapshotAccessor, Expression<Func<CompMyData, TValue>> selector)
     => PublishedModelUtility.GetModelPropertyType(GetModelContentType(publishedSnapshotAccessor), selector);

Specifics

The error occurs in Umbraco.Cms.Infrastructure.ModelsBuilder.PublishedModelUtility.GetModelContentType()

image

Steps to reproduce

Attempt to use GetModelPropertyType() on a Model for a Composition.

Expected result / actual result

I just wanted to get the property alias.

Resolution?

Should "Element" be added as an acceptable itemType?

github-actions[bot] commented 1 year ago

Hi there @hfloyd!

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:

Zeegaan commented 1 year ago

Hmm I cannot reproduce this sadly 😕

This is me on 10.6.1: image

hfloyd commented 1 year ago

Hi @Zeegaan , Thanks for looking into this. Do you see what the "itemType" is returning when you step into that method? When I run it, "itemType" = "Element" - which isn't included in the switch statement...

Zeegaan commented 1 year ago

Yea apparently I just forgot to re-build my models after switching to the Limbo modelsbuilder 🙈 We had a talk and agreed this is a bug, and have made a PR implementing your suggestion @hfloyd 🙌 Fixed in https://github.com/umbraco/Umbraco-CMS/pull/14776

abjerner commented 1 year ago

Awesome 👍

I hadn't even spotted this. I tried to do the same as Umbraco's models builder for my package, so not sure if something has changed in Umbraco since then, or I just ended up doing this part slightly different.

hfloyd commented 1 year ago

Great news, @Zeegaan ! Thanks for checking again.