Giter Site home page Giter Site logo

Comments (9)

dotMorten avatar dotMorten commented on July 2, 2024

Could you share an example that demonstrates this problem?

from microsoft.sqlserver.types.

masonwheeler avatar masonwheeler commented on July 2, 2024

Here's a very simple case:

using Microsoft.SqlServer.Types;
using System;
using System.IO;

namespace SqlHierarchy.Repro
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream, System.Text.Encoding.UTF8, true);
            var hierarchy = SqlHierarchyId.Parse(new System.Data.SqlTypes.SqlString("/1/"));
            Console.WriteLine(hierarchy);
            hierarchy.Write(writer);
            for (int i = 0; i < 10; i++)
            {
                writer.Write(i);
            }
            writer.Close();
            stream.Position = 0;
            var reader = new BinaryReader(stream);
            hierarchy = new SqlHierarchyId();
            hierarchy.Read(reader);
            Console.WriteLine(hierarchy);
        }
    }
}

Expected: This should print /1/ twice.

Observed: Prints /1/ once, then crashes with:

Microsoft.SqlServer.Types.HierarchyIdException: '24000: SqlHierarchyId operation failed because HierarchyId object was constructed from an invalid binary string. '

from microsoft.sqlserver.types.

dotMorten avatar dotMorten commented on July 2, 2024

@masonwheeler Thanks this is awesome. I tried this with the official .NET Framework implementation and it gives a very similar exception, so from what I can tell, this is "as designed".
Only thing I can think of doing here is slightly tweaking the error message to match.

Unit test used to compare behavior:

        [TestMethod]
        [WorkItem(64)]
        public void ReadFromContinuousStream()
        {
            var stream = new MemoryStream();
            var writer = new BinaryWriter(stream, System.Text.Encoding.UTF8, true);
            var hierarchy = SqlHierarchyId.Parse(new System.Data.SqlTypes.SqlString("/1/"));
            hierarchy.Write(writer);
            //Add extra bytes after
            for (int i = 0; i < 10; i++)
            {
                writer.Write(i);
            }
            writer.Close();
            stream.Position = 0;
            var reader = new BinaryReader(stream);
            var hierarchy2 = new SqlHierarchyId();
            hierarchy2.Read(reader);
            Assert.AreEqual(hierarchy.ToString(), hierarchy2.ToString());
        }

from microsoft.sqlserver.types.

dotMorten avatar dotMorten commented on July 2, 2024

Error message thrown now matches .NET Framework implementation

from microsoft.sqlserver.types.

masonwheeler avatar masonwheeler commented on July 2, 2024

So where would I find the official repo to report this? It may be "as designed" but the design is completely broken.

from microsoft.sqlserver.types.

dotMorten avatar dotMorten commented on July 2, 2024

If you clone this repo and run the unit test, it'll repeat the tests for both this implementation as well as the official Microsoft .NET Framework implementation. AFAIK there is no official repo for that library, so I'm not sure where to report it. Having said that, I don't really see how this can be supported, since there doesn't appear to be any indicator in a binary stream that you've reached the end of a hierarchy object and should stop reading.

from microsoft.sqlserver.types.

masonwheeler avatar masonwheeler commented on July 2, 2024

Yeah, that's the entire point of my complaint, as I noted in the original post. This is inconsistent with the binary serialization format of the other types, which are length-prefixed. That's how you fix it: change the serialization format to something sane.

from microsoft.sqlserver.types.

dotMorten avatar dotMorten commented on July 2, 2024

You can try and get the sql server team to change a well-established format. I wish you the best of luck :-p

This is inconsistent with the binary serialization format of the other types

They are very different and serve different purposes, and AFAIK implemented by different teams.

from microsoft.sqlserver.types.

dotMorten avatar dotMorten commented on July 2, 2024

Perhaps when you write it, add \0 after each hierarchy and when reading it back in, use that as delimiter before feeding it into the reader? Would be simple to do to address your scenario.

from microsoft.sqlserver.types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.