RehanSaeed / Schema.NET

Schema.org objects turned into strongly typed C# POCO classes for use in .NET. All classes can be serialized into JSON/JSON-LD and XML, typically used to represent structured data in the head section of html page.
MIT License
640 stars 80 forks source link

ListItem won't let me set the Item property #672

Closed AlizerUncaged closed 8 months ago

AlizerUncaged commented 9 months ago

Describe the bug

I have the code like below to create a BreadcrumbList schema image

my problem is that the ListItem Item property accepts a OneOrMany<IThing> type which if I do new OneOrMany<IThing>((IThing)new Uri(formattedUrl)) causes a System.InvalidCastException

Steps to reproduce

create a breadcrumblist object

Expected behaviour

It should allow string or a Uri in the Item field, Google seems to just accept a Uri in the item field, it will hurt SEO if we're not following Google image

Schema objects

https://schema.org/ListItem

Turnerj commented 8 months ago

Hi @AlizerUncaged - so the cast doesn't work because Uri is a .NET system type which doesn't implement the IThing interface. The root of your problem is the same thing I've mentioned here - what Google is showing in their example isn't following the schema.org standard. Our types are generated from the standard, not things Google decides to deviate from it.

We can see in the schema.org docs that item is only of type Thing.

image

Drilling down further into the item property docs itself, it shows a JSON-LD example of how you would set the URL:

<script type="application/ld+json">
{
 "@context": "https://schema.org",
 "@type": "BreadcrumbList",
 "itemListElement":
 [
  {
   "@type": "ListItem",
   "position": 1,
   "item":
   {
    "@id": "https://example.com/dresses",
    "name": "Dresses"
    }
  },
  {
   "@type": "ListItem",
  "position": 2,
  "item":
   {
     "@id": "https://example.com/dresses/real",
     "name": "Real Dresses"
   }
  }
 ]
}
</script>

And the screenshot of the Google documentation that you put in your comment on the other issue says that this is valid:

image

So all you need to do is use:

new ListItem
{
  Item = new Thing
  {
    Id = new Uri(formattedUrl + "_b")
  }
}

To be clear, I don't even disagree that item probably could take a Url as an alternative type but that isn't up to me - that is up to schema.org and unless you get them to change, we can't change.