ift-gftc / opentraceability

A repository for storing code for the open source traceability libraries developed by IFT.
MIT License
4 stars 2 forks source link

If an "@vocab" is included in the @context on a JSON-LD, causes errors converting to XML. #39

Open DuckScapePhilip opened 7 months ago

DuckScapePhilip commented 7 months ago

Problem

If an EPCIS Document / EPCIS Query Document has an "@vocab" in the JSON-LD:

{
     "@context": 
     [
          "https://gs1.github.io/EPCIS/epcis-context.jsonld",
          {
               "example": "http://ns.example.com/epcis/"
          },
          {
               "@vocab": "https://traceability-dialogue.org/epcis"
          }
     ],

Then if you convert it to a C# object, then map it back to XML, it will throw an error because we will try and add it as a namespace:

https://github.com/ift-gftc/opentraceability/blob/main/CSharp/OpenTraceability/Mappers/EPCIS/XML/EPCISDocumentBaseXMLMapper.cs

foreach (var ns in doc.Namespaces)
{
     if (ns.Value == Constants.CBVMDA_NAMESPACE
      || ns.Value == Constants.EPCISQUERY_1_NAMESPACE
      || ns.Value == Constants.EPCISQUERY_2_NAMESPACE
      || ns.Value == Constants.EPCIS_1_NAMESPACE
      || ns.Value == Constants.EPCIS_2_NAMESPACE)
     {
          continue;
     }
     else
     {
          xDoc.Root.Add(new XAttribute(Constants.XMLNS_XNAMESPACE + ns.Key, ns.Value));
     }
}

I think we should implement a "TryAdd" so that it will try and add it but ignore it if it fails to add it.

Tasks

  1. Update the XML mapper so that it handles this.
  2. Create a new test case to test having a JSON-LD with this attribute, map it into a C# object, then map it back to the XML format.