Skip to content

JsConfig.BeginScope Retains TreatEnumAsInteger for Future Scopes #10

@burrissc

Description

@burrissc

I provided two failing MSTest unit tests below to illustrate an issue I came across. When using JsConfig.BeginScope, the TreatEnumAsInteger property specifically is retaining its initial setting from the prior JsConfig scope. For example, if TreatEnumAsInteger is set as true for the first scope, it will also be true for the second scope (even when explicitly set to false) and vice versa. EmitCamelCaseNames and ExcludeTypeInfo, at minimum, use the settings within their scope as expected.

The NServiceKit.Text version is 1.0.10 from NuGet. My project is targeting .NET 4.5.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using NServiceKit.Text;

namespace NServiceKit.Text.Tests
{
    [TestClass]
    public class NServiceKitTextTest
    {
        public enum NServiceKitEnum
        {
            Value1,
            Value2,
            Value3
        }

        public class NServiceKitTestClass
        {
            public NServiceKitEnum SomeEnum { get; set; }
        }

        [TestMethod]
        public void NServiceKitTextTest_JsConfigScope_TreatEnumsAsIntegerIsUsedInFutureScopes()
        {
            string json1 = null;
            string json2 = null;

            var obj = new NServiceKitTestClass() { SomeEnum = NServiceKitEnum.Value2 };

            using (var config = JsConfig.BeginScope())
            {
                config.EmitCamelCaseNames = true;
                config.TreatEnumAsInteger = true;

                json1 = JsonSerializer.SerializeToString(obj);
            }

            using (var config = JsConfig.BeginScope())
            {
                config.EmitCamelCaseNames = false;
                config.TreatEnumAsInteger = false;

                json2 = JsonSerializer.SerializeToString(obj);
            }

            // becomes... "[ {\"someEnum\":1}, {\"SomeEnum\":1} ]"
            var combined = string.Format("[ {0}, {1} ]", json1, json2);

            Assert.AreEqual("{\"someEnum\":1}", json1, ignoreCase: false); // SUCCEEDS
            Assert.AreEqual("{\"someEnum\":\"Value2\"}", json2, ignoreCase: false); // FAILS!
        }

        [TestMethod]
        public void NServiceKitTextTest_JsConfigScopeInReverseOrder_TreatEnumsAsIntegerIsUsedInFutureScopes()
        {
            string json1 = null;
            string json2 = null;

            var obj = new NServiceKitTestClass() { SomeEnum = NServiceKitEnum.Value2 };

            using (var config = JsConfig.BeginScope())
            {
                config.EmitCamelCaseNames = false;
                config.TreatEnumAsInteger = false;

                json1 = JsonSerializer.SerializeToString(obj);
            }

            using (var config = JsConfig.BeginScope())
            {
                config.EmitCamelCaseNames = true;
                config.TreatEnumAsInteger = true;

                json2 = JsonSerializer.SerializeToString(obj);
            }

            // becomes... "[ {\"SomeEnum\":\"Value2\"}, {\"someEnum\":\"Value2\"} ]"
            var combined = string.Format("[ {0}, {1} ]", json1, json2);

            Assert.AreEqual("{\"SomeEnum\":\"Value2\"}", json1, ignoreCase: false); // SUCCEEDS
            Assert.AreEqual("{\"someEnum\":1}", json2, ignoreCase: false); // FAILS!
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions