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

ThingConvert ignores the @Context #5

Open
juanparati opened this issue Feb 8, 2022 · 7 comments
Open

ThingConvert ignores the @Context #5

juanparati opened this issue Feb 8, 2022 · 7 comments

Comments

@juanparati
Copy link

It seems that ThingConvert ignores @context, so the library will fails to read schemas like the following ones:

{
   "@context":"https://schema.org",
   "@graph":[
      {
         "@type":"Organization",
         "@id":"https://example.org/#organization",
         "name":"Acme LTD",
         "url":"https://example.org/",
         "sameAs":[
            
         ]
      }
   ]
}

The reason is that ThingConverts expects a @type that starts with https://schema.org.

@jdrucey
Copy link

jdrucey commented Jun 9, 2022

Just started using this as it seems perfect for what we need, so thanks for the great work.

Flat schemas worked perfectly until I tried running on a site that uses @graph to include multiple schemas, where I get no results back.

Any input on this one? @graph seems to be commonly used by Yoast, a popular WordPress plugin, so these types of implementation seem ripe across the internet. This structure also validates fine in validator.schema.org so handling it in this library seems sensible.

@jdrucey
Copy link

jdrucey commented Jun 10, 2022

This is seemingly the same issue as @juanparati issue, in that the context is set at the top, so inside the @graph in my example, types are not prefixed with https://schema.org.

What was your solution in the end @juanparati?

@goellner
Copy link

Also running into this issue, would be great if anyone could share a solution to this

@goellner
Copy link

brick/schema can extract the data, if the context is set on a graph item:

{
   "@context":"https://schema.org",
   "@graph":[
      {
         "@context": "http://schema.org/"
         "@type":"Organization",
         "@id":"https://example.org/#organization",
         "name":"Acme LTD",
         "url":"https://example.org/",
         "sameAs":[
            
         ]
      }
   ]
}

older versions of the wp plugin yoast don't include this context, newer versions do. since we can't update external websites yoast version, it would be awesome, if brick could add support for this.

@BenMorel would you be able to help if we sponsor the project with a one time sponsorship?

@jhard
Copy link

jhard commented Jan 28, 2023

Seems like the easiest way is to add the @context in the @graph array so you end up with what @goellner mentioned.

Quickfix for me was to change brick/structured-data/src/Reader/JsonLdReader.php in the function readJson and change $data = $data->{'@graph'}; to

                $context = $data->{'@context'} ?? null;
                $data = $data->{'@graph'};
                if($context) {
                   foreach($data as $i => $item) {
                      if($context && !($item->{'@context'} ?? null)) {
                         $data[$i]->{'@context'} = $context;
                      }
                   }
                }

It will preserve existing @context attributes on the items and otherwise add the parent one if one exists.
I'm a total noob when it comes to schema.org so I don't know if they could potentially have nested @graph that would require additional treatment, but that shouldn't be too hard to add.

@BenMorel If you'd accept a pull request for brick/structured-data I could create on over there.

@goaround
Copy link

I run into the same issue with the Rank Math WordPress Plugin. There is also the @Schema on the @graph items missing
@jhard could you create a PR?

goaround added a commit to goaround/structured-data that referenced this issue Aug 18, 2023
@jhard
Copy link

jhard commented Aug 28, 2023

I run into the same issue with the Rank Math WordPress Plugin. There is also the @Schema on the @graph items missing @jhard could you create a PR?

Sorry, I've long moved on from this and it seems so has everyone else unfortunately. Feel free to submit the PR with the code yourself though :)

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

No branches or pull requests

5 participants