pyld icon indicating copy to clipboard operation
pyld copied to clipboard

expand() does not prepend `@base` to @id-s

Open anatoly-scherbakov opened this issue 4 years ago • 2 comments

Problem

My PyLD version is 2.0.3 from pypi.

The complete code for this issue is published at https://gist.github.com/anatoly-scherbakov/84a539d2f862d1792244168a3b970b57

I am trying to expand() a JSON-LD document like this:

{
  "@context": {
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "label": "rdfs:label",
    "@vocab": "https://example.com/robotics/",
    "@base": "https://example.com/robotics/"
  },
  "@graph": [
    {
      "@id": "Robot",
      "@type": "rdfs:Class"
    },
    ...
  }
}

Expected output

I expect to see Robot converted to https://example.com/robotics/Robot.

That is how JSON-LD playground works — which can be verified by pasting the content of SERIALIZED_JSONLD_DOCUMENT constant into https://json-ld.org/playground/

Actual output

pyld.jsonld.expand() gives me

        {
            '@id': 'Robot',   # NOT EXPANDED
            '@type': ['http://www.w3.org/2000/01/rdf-schema#Class'],
        },
        ...

It only works if I'd provide base explicitly in the options:

    jsonld.expand(
        JSONLD_DOCUMENT,
        options={
            'base': 'https://example.com/robotics/',
        },
    )

Should it work this way? Thank you!

anatoly-scherbakov avatar Nov 29 '20 09:11 anatoly-scherbakov

My understanding of the spec (4.1.3 Base IRI) is the same as yours (and what the JSON-LD Playground does).

avillar avatar Jul 04 '23 12:07 avillar

I encountered the same issue while testing with PyLD version 2.0.4 and would like to confirm if this problem originates from the code implementation in PyLD.

During my tests, I expected the expand() method to expand the @base value before each @id value, but in reality, the @id values remain unchanged unless a base value is set in the options.

jsonld_doc = {
    "@context": {
        "name": "https://www.exmaple.com/name",
        "@base": "https://www.example.com/"
    },
    "@id": "c/123",
    "name": "alice"
}
pyld.jsonld.expand(jsonld_doc, options={'base': 'abc'})

Output (with base set)

[
   {
	"@id": "https://www.example.com/c/123",
	"https://www.exmaple.com/name": [
	   {
		"@value": "alice"
	   }
	]
   }
]

I have found that if any value is set for base in the options, the @id values are expanded as expected. However, if no base value is set, the @id values remain unchanged.

jsonld_doc = {
    "@context": {
        "name": "https://www.exmaple.com/name",
        "@base": "https://www.example.com/"
    },
    "@id": "c/123",
    "name": "alice"
}
pyld.jsonld.expand(jsonld_doc, options={'base': ''})

Output (without base set)

[
   {
	"@id": "c/123",
	"https://www.exmaple.com/name": [
	   {
		"@value": "alice"
	   }
	]
   }
]

I'm curious if others have encountered similar issues and whether this problem is a result of the code implementation in PyLD version 2.0.4.

Thank you!

aye-misan avatar Mar 24 '24 10:03 aye-misan