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

Relative paths are removed from Product #391

Closed
dermotblairca opened this issue Feb 15, 2022 · 6 comments
Closed

Relative paths are removed from Product #391

dermotblairca opened this issue Feb 15, 2022 · 6 comments
Labels
bug Issues describing a bug or pull requests fixing a bug.

Comments

@dermotblairca
Copy link
Contributor

dermotblairca commented Feb 15, 2022

Describe the bug

When a Product has a relative path, for example as the "image" field, this is not returned in the Product object from SchemaSerializer.DeserializeObject.
Maybe this was intentional but it does seem to validate ok in the Schema.org validator. For example: https://validator.schema.org/#url=https%3A%2F%2Fwww.thedandys.ie%2Fproduct%2Fsodium-hypochlorite-25ltrs%3Fgclid%3DCj0KCQiAu62QBhC7ARIsALXijXTUddi2aWyH-XjZKJHIuzUSLbH6HiVW883I7sSdYEtORXVeeALdfwkaAnALEALw_wcB
And the schema.org documentation shows relative paths in the examples too (see the example in the JSON-LD tab): https://schema.org/image

Steps to reproduce

Run the following unit test in the ProductTest.cs file:

namespace Schema.NET.Test.Examples;

using System;
using Xunit;

// https://developers.google.com/search/docs/data-types/products
public class ProductTest
{
    [Fact]
    public void Deserializing_ProductJsonLd_ReturnsProduct2()
    {
        var productJson = "{\"@context\":\"http://www.schema.org\",\"@type\":\"product\",\"name\":\"SodiumHypochlorite/RedLabel25Ltrs\",\"image\":\"/site/uploads/sys_products/red-label-hypo.jpg\",\"offers\":{\"@type\":\"Offer\",\"price\":\"22.00\",\"availability\":\"http://schema.org/InStock\",\"priceCurrency\":\"EUR\"}}";
        var productObject = SchemaSerializer.DeserializeObject<Product>(productJson);
        var productBackToString = productObject?.ToString();
        var hasImagePath = productBackToString?.IndexOf("/site/uploads/sys_products/red-label-hypo.jpg", StringComparison.OrdinalIgnoreCase) > 0;
        Assert.True(hasImagePath);
    }
}

Expected behaviour

The image should be included in the Product object.

Schema objects

https://schema.org/Product (but I think this would affect any class derived from Thing)

@dermotblairca dermotblairca added the bug Issues describing a bug or pull requests fixing a bug. label Feb 15, 2022
@Turnerj
Copy link
Collaborator

Turnerj commented Feb 16, 2022

Thanks for raising this - we've seen this come up already in #343. It is a bit of a weird one to solve.

@dermotblairca
Copy link
Contributor Author

That's interesting, thanks for the link to the discussion @Turnerj

@RehanSaeed
Copy link
Owner

I think the solution as we discussed in #343 is to use UriKind.AbsoluteOrRelative. Although, if you're producing JSON-LD I'd still recommend using absolute URL's only.

@dermotblairca Would you like to raise a PR with the change to see if your problem is solved?

@dermotblairca
Copy link
Contributor Author

Hi @RehanSaeed Yes I have created a branch locally with the change (named dermotblairca/AllowRelativeUri) and have tested it on both absolute and relative URIs, by adding the following unit tests in ProductTest.cs (just locally, I didn't commit these to the branch).

[Fact]
public void Deserializing_ProductJsonLd_DeserializeRelativeUri()
{
    const string relativeUri = "/site/uploads/sys_products/red-label-hypo.jpg";
    var productJson = "{\"@context\":\"http://www.schema.org\",\"@type\":\"product\",\"name\":\"SodiumHypochlorite/RedLabel25Ltrs\",\"image\":\"" + relativeUri + "\",\"offers\":{\"@type\":\"Offer\",\"price\":\"22.00\",\"availability\":\"http://schema.org/InStock\",\"priceCurrency\":\"EUR\"}}";
    var product = SchemaSerializer.DeserializeObject<Product>(productJson);
    Assert.Equal(relativeUri, product?.Image.Value2.FirstOrDefault()?.ToString());
}

[Fact]
public void Deserializing_ProductJsonLd_DeserializeAbsoluteUri()
{
    const string absoluteUri = "https://www.thedandys.ie/site/uploads/sys_products/red-label-hypo.jpg";
    var productJson = "{\"@context\":\"http://www.schema.org\",\"@type\":\"product\",\"name\":\"SodiumHypochlorite/RedLabel25Ltrs\",\"image\":\"" + absoluteUri + "\",\"offers\":{\"@type\":\"Offer\",\"price\":\"22.00\",\"availability\":\"http://schema.org/InStock\",\"priceCurrency\":\"EUR\"}}";
    var product = SchemaSerializer.DeserializeObject<Product>(productJson);
    Assert.Equal(absoluteUri, product?.Image.Value2.FirstOrDefault()?.ToString());
}

I am trying to push my branch but I am getting the following message:
image
Do I need some type of permission or token? Thanks.

@dermotblairca
Copy link
Contributor Author

Thanks for your suggestion too about producing JSON-LD, that is good to know. I am actually using this package for the opposite use case though, deserializing JSON-LD to C# objects.

@dermotblairca
Copy link
Contributor Author

I have created a fork and a pull request from my fork @RehanSaeed #401

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug or pull requests fixing a bug.
Projects
None yet
Development

No branches or pull requests

3 participants