SwissDataScienceCenter / calamus

A JSON-LD Serialization Libary for Python
Apache License 2.0
29 stars 12 forks source link

cannot select the id field if it's BlankNodeId #22

Open vigsterkr opened 4 years ago

vigsterkr commented 4 years ago

say i have the following schema:

class A(JsonLDSchema):
   _id = fields.BlankNodeId()

class B(JsonLDSchema):
   _id = fields.BlankNodeId()
   value = fields.Nested(ML_SCHEMA.specifiedBy, A(only=("_id",)))

basically i only want to reference the A node.... this will basically yield in the following exception:

ValueError: Invalid fields for <ASchema(many=False)>: {'_id'}.

if i change ASchema::_id to fields.Id() it works, but even then it as well serializes the @type of the node, which i hoped that is not gonna be the case...

maybe it's related: when a field is BlankNodeId, then the @id is not serialized.

Panaetius commented 4 years ago

fields.Nested is meant to serialize nested nodes, so it'll always add @type.

If you just want an id reference between them, you can pass flattened=True to the schema, then you get a flat list like

[
{'@id': 'a', '@type': 'A`},
{'@id': 'b', '@type': 'B', 'value': {'@id': 'a'}
]

Not sure if that helps you.

I'll take a look at the BlankNodeId's (they're somewhat experimental right now).

We also discussed adding an IRIField before for an unrelated issue, that would do 'value':{'@id': '...'} serialization from/to string, but that's more for when you want to reference an external IRI/URI.

Can you give an example with Schema's, the serialization command used and what the desired Json-LD would be?.