jsonld.js icon indicating copy to clipboard operation
jsonld.js copied to clipboard

compactArrays over @type in version 1.0.2

Open Carpani opened this issue 7 years ago • 3 comments

Hello. In version 1.0.2, when I compact with compactArrays in false, works OK... except for @type. For example:

.compact(this.doc,this.ctx,{"compactArrays":false})
        .then(
          (comp)=>{
            let g=comp['@graph'];
// Here --> g[0] has  @type:"owl:Class"  

I think that type must be ["owl:class"]

I'm right?

Thanks. FDO.

Carpani avatar May 23 '18 22:05 Carpani

Did this work in 1.0.1? Can you provide short test case doc and context? I think the code is out of date with some of the tests related to this feature so some bugs might have slipped through. It also might be just preserving arrays, or lack of them, but not forcing everything to one. I'm forgetting what the correct behavior is.

davidlehn avatar May 24 '18 17:05 davidlehn

Heres an example. The code:

const jld=require('jsonld').promises;

const doc = [{
    "@id": "http://mh.uy/2018/4/Soporte#capturado",
    "@type": ["http://mh.uy/2018/4/Soporte#DataProperty","http://mh.uy/2018/4/Soporte#LocalProperty"],
    "http://www.w3.org/2000/01/rdf-schema#domain": [{
        "@id": "http://mh.uy/2018/4/Soporte#Datos"
    }]
}, {
        "@id": "http://mh.uy/2018/4/Soporte#corregido",
        "@type": ["http://mh.uy/2018/4/Soporte#DataProperty"],
        "http://www.w3.org/2000/01/rdf-schema#domain": [{
            "@id": "http://mh.uy/2018/4/Soporte#Datos"
        }]
    }];
const owlCtx = {
    'owl': 'http://www.w3.org/2002/07/owl#',
    'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
    'rdf': "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    'xsd': "http://www.w3.org/2001/XMLSchema#",
    'docCont': 'http://mh.uy/onto/2018/5/Contenido#',
    'doc': 'http://mh.uy/onto/2018/04/Documentos#',
    'ficha': 'http://mh.uy/onto/2017/7/FichaPersonal#',
    'supp': 'http://mh.uy/2018/4/Soporte#'
};

jld
    .compact(doc,owlCtx,{'compactArrays':false})
        .then((data)=>{
            console.log(JSON.stringify(data['@graph'],null,2));
        })
        .catch((err)=>{
            console.log(err.message,err.stack);
        });

The output:

[
  {
    "@id": "supp:capturado",
    "@type": [ "supp:DataProperty", "supp:LocalProperty"  ],
    "rdfs:domain": [
      { "@id": "supp:Datos" }
    ]
  },
  {  "@id": "supp:corregido",
    "@type": "supp:DataProperty",
    "rdfs:domain": [
      {   "@id": "supp:Datos"  }
    ]
  }
]

The jsonld document has two objects. The first object has an array of strings as @type with two types. The second has also an array but, only has one type. In the output, we can see that the @type in the first object still is an array, but in the second, is a string.

Today, I was in a quick reading of https://json-ld.org/spec/latest/json-ld-api/. In 9.2 section says:

compactArrays If set to true, the JSON-LD processor replaces arrays with just one element with that element during compaction. If set to false, all arrays will remain arrays even if they have just one element.

I don't see any exception about type... but I can be wrong. Or, perhaps, I don't understand something of the library. I think that in some previous version, this was working according to the specs.

That behaviour might be a bug?

Thanks. FDO.

Carpani avatar May 24 '18 22:05 Carpani

In the newest release compaction will cause each value to be an array if compactArrays is set to false. Stating from the docs this seems to be wrong, as it should only cause values to be an array if their context states them as such by @container: @set or @list. E.g.

_:b4_b0 <http://ex.org/points> "3"^^<http://www.w3.org/2001/XMLSchema#short> .

becomes to

{
  "@id": "_:b4_b0",
  "points": [ "3" ]
}

if this context is used:

{ 
  "points": {
    "@id": "http://ex.org/points", 
    "@type": "http://www.w3.org/2001/XMLSchema#short"
  } 
}

rmeissn avatar Apr 03 '19 08:04 rmeissn