piprate / json-gold

A JSON-LD processor for Go
Apache License 2.0
259 stars 30 forks source link

"Dereferencing a URL did not result in a valid JSON-LD context" for all given examples #37

Closed lanfeust21 closed 4 years ago

lanfeust21 commented 4 years ago
package main

import (
    "github.com/piprate/json-gold/ld"
    "log"
)

func main() {
    proc := ld.NewJsonLdProcessor()
    options := ld.NewJsonLdOptions("")

    // expanding remote document

    expanded, err := proc.Expand("http://json-ld.org/test-suite/tests/expand-0002-in.jsonld", options)
    if err != nil {
        log.Println("Error when expanding JSON-LD document:", err)
        return
    }

    ld.PrintDocument("JSON-LD expansion succeeded", expanded)

    // expanding in-memory document

    doc := map[string]interface{}{
        "@context":  "https://schema.org/Person",
        "@type":     "Person",
        "name":      "Jane Doe",
        "jobTitle":  "Professor",
        "telephone": "(425) 123-4567",
        "url":       "http://www.janedoe.com",
    }

    expanded, err = proc.Expand(doc, options)
    if err != nil {
        panic(err)
    }

    ld.PrintDocument("JSON-LD expansion succeeded", expanded)
}

panic: loading remote context failed: Dereferencing a URL did not result in a valid JSON-LD context: https://schema.org/Person

goroutine 1 [running]: main.main() /Users/denislamotte/Desktop/work/jsonld/jsonld.go:35 +0x578 exit status 2

kazarena commented 4 years ago

@lanfeust21, either "http://schema.org" or "https://schema.org" are correct contexts for schema.org. "https://schema.org/Person" is not (it's a type). Please refer to schema.org for valid examples of JSON-LD for Person.

lanfeust21 commented 4 years ago

Maybe but it's the example given by the code i did not change anything. The change proposed, did not change anything and still create the same error

kazarena commented 4 years ago

@lanfeust21 can you please provide a link to the example that uses

"@context":  "https://schema.org/Person",

?

You reported that the proposed changes don't work either. I think I found the underlying reason. See https://github.com/schemaorg/schemaorg/issues/2578#issuecomment-632227864 for more information. schema.org's content negotiation has changed in May 2020. Since then, multiple fixes were proposed by various JSON-LD implementations, including https://github.com/RDFLib/rdflib/pull/1125 and https://github.com/lanthaler/JsonLD/commit/f8dff42283d8fb256c0380fc78fe4e585002991e.

Back to the issue, https://schema.org now returns the following Link in the header:

link: </docs/jsonldcontext.jsonld>; rel="alternate"; type="application/ld+json"

Currently, this library only supports rel="http://www.w3.org/ns/json-ld#context" as per JSON-LD 1.1 spec.

Proposed solution

If you use the actual location of schema.org context:

"@context":  "https://schema.org/docs/jsonldcontext.jsonld",

The example should work. Please note that it is considered the best practice to cache context documents in production applications.

We will add support for rel="alternate" in the next release. I can't provide ETA just yet.

Thanks for reporting.

kazarena commented 4 years ago

The fix is in the master. It will be included in the next minor release.