digitalbazaar / pyld

JSON-LD processor written in Python
https://json-ld.org/
Other
606 stars 131 forks source link

JSON-LD Value Objects #142

Open calummackervoy opened 3 years ago

calummackervoy commented 3 years ago

I'm working on a project which uses PyLD as a dependency. Reviewing the JSON-LD spec on value objects, it reads

The value associated with the @value key MUST be either a string, a number, true, false or null. If the value associated with the @type key is @json, the value MAY be either an array or an object.

Based on this I wrote a unit test

def test_save_field_with_object_value_object(self):
    invoice = Invoice.objects.create(title="title 3")
    post = {
            'http://test-server/owl/#invoice': {
                '@value': {'title': 'title', '@id': "https://test-server/{}{}/".format(Model.container_id(invoice), invoice.id)},
                '@type': '@json'
            }
        }
    response = self.client.post('/batchs/', data=json.dumps(post), content_type='application/ld+json')
    self.assertEqual(response.status_code, 201)

The endpoint uses jsonld.compact from PyLD, which in this test throws an error:

pyld.jsonld.JsonLdError: ('Could not expand input before compaction.',)
Type: jsonld.CompactError
Cause: ('Invalid JSON-LD syntax; "@value" value must not be an object or an array.',)

This is correct behaviour for the first part of the specification, but providing the '@type': '@json' should allow the value to be an array/object

Sorry that the example is so specific to my project.. let me know if I can improve the info given!