dgm9704 / Xoxo

read, write, compare, convert XBRL reports
GNU Lesser General Public License v3.0
27 stars 9 forks source link

Bug with namespaces used in the value field of a segment explicit member #76

Closed TheSly1 closed 2 years ago

TheSly1 commented 2 years ago

If you use a segment value with a namespace which is not already used elsewhere (for example: dimension of some explicit member, fact, etc.), exception will be thrown.

string SchemaFilePath = "C:/Path/To/File.xsd";                                                                            
var instance = new Instance();                                                                                            
instance.SchemaReference = new SchemaReference("SCHEMA_TEST", SchemaFilepath);                                            
instance.TaxonomyVersion = "1.0";                                                                                         

instance.SetDimensionNamespace("TEST1", "http://www.some-url.com/xbrl/TEST1");                                            
instance.AddDomainNamespace("TEST2", "http://www.some-url.com/xbrl/TEST2");                                               
instance.AddDomainNamespace("TEST3", "http://www.some-url.com/xbrl/TEST_FACT");                                           

instance.Entity = new Entity("https://www.business-authority.org", "BusinessEntityID");                                   
instance.Period = new Period(DateTime.UtcNow);                                                                            

var segment = new Segment();                                                                                              
segment.AddExplicitMember("TEST1:TestString", "TEST1:TestValue"); // Ok ("TEST1" is used in the same segment as dimension)
segment.AddExplicitMember("TEST1:TestTestString", "TEST2:Test"); // Exception ("TEST2" is not used anywhere else but here)
segment.AddExplicitMember("TEST1:Testx3String", "TEST3:MyTest"); // Ok ("TEST3" is used in the fact name)                 

instance.AddFact(segment, "TEST3:FactTest", "", "2", "12345");                                                            

instance.ToFile("text.xml"); // Exception is thrown here 
dgm9704 commented 2 years ago

Thanks! I'll check it out.

TheSly1 commented 2 years ago

I managed to find the solution for myself. Here it is if it helps you:

In file: Instance.cs Function: GetXmlSerializerNamespaces Faulty code was this:

Contexts.
      Where(c => c.Entity.Segment != null).
      Select(c => c.Entity.Segment).
      SelectMany(s => s.ExplicitMembers).
      Select(m => m.Dimension.Namespace).
      ToList().ForEach(ns => namespaces.Add(ns));

Mine version:

Contexts.
        Where(c => c.Entity.Segment != null).
        Select(c => c.Entity.Segment).
        SelectMany(s => s.ExplicitMembers).
        Select(m => new { dimensionNamespace = m.Dimension.Namespace, valueNamespace = m.Value.Namespace }).
        ToList().
        ForEach(obj =>
        {
            if (!string.IsNullOrEmpty(obj.dimensionNamespace))
            {
                namespaces.Add(obj.dimensionNamespace);
            }

            if (!string.IsNullOrEmpty(obj.valueNamespace))
            {
                namespaces.Add(obj.valueNamespace);
            }
        });

Hope it helps :)

dgm9704 commented 2 years ago

Thank you, there is now version 1.2.2 (+corresponding nuget) with a fix for this + fix for outputting xbrldi namespace when using segments.