I have a very weird situation which I am finding impossible to resolve, so any guidance would be much appreciated.

I am retrieving the results of a VAT Number Search from HMRC via their API, and it is being returned as valid JSON. However, one of the properties and its own properties are not being set when I do it in the live application but if I debug the application and copy the returned JSON value over to a unit test, it deserialises fully - yet the code and the value are identical in both scenarios. I have been doing this sort of thing for ages with other parts of the API, it is only this result that has the issue.

I am using fastJSON to deserialise the returned JSON to a simple POCO object:

public class VatNumberValidationResult
{

    #region Properties

    // ReSharper disable InconsistentNaming
    public string consultationNumber { get; set; }
    public DateTime? processingDate { get; set; }
    public string requester { get; set; }
    public VatTarget target { get; set; }
    // ReSharper restore InconsistentNaming

    #endregion

    #region Constructors

    public VatNumberValidationResult()
    {
        Initialise();
    }

    #endregion

    #region Methods

    private void Initialise()
    {
        consultationNumber = string.Empty;
        processingDate = null;
        requester = string.Empty;
        target = new VatTarget();
    }

    #endregion

}

public class VatTarget
{

    #region Properties

    // ReSharper disable InconsistentNaming
    public string name { get; set; }
    public string vatNumber { get; set; }
    public VatValidationAddress address  { get; set; }
    // ReSharper restore InconsistentNaming

    #endregion

    #region Constructors

    public VatTarget()
    {
        Initialise();
    }

    #endregion

    #region Methods

    private void Initialise()
    {
        name = string.Empty;
        vatNumber = string.Empty;
        address = new VatValidationAddress();
    }

    #endregion

}

public class VatValidationAddress
{

    #region Properties

    // ReSharper disable InconsistentNaming
    public string countryCode { get; set; }
    public string line1 { get; set; }
    public string line2{ get; set; }
    public string line3{ get; set; }
    public string postcode { get; set; }
    // ReSharper restore InconsistentNaming

    #endregion

    #region Constructors

    public VatValidationAddress()
    {
        Initialise();
    }

    #endregion

    #region Methods

    private void Initialise()
    {
        countryCode = string.Empty;
        line1 = string.Empty;
        line2 = string.Empty;
        line3 = string.Empty;
        postcode = string.Empty;
    }

    #endregion

}

Here is an example of the Unit Test code:

    [TestMethod]
    public void ToObject_ValidHmrcJson2_ReturnsValidObject()
    {
        var payload = "{\"target\":{\"name\":\"EVERGREEN LIMITED\",\"vatNumber\":\"123926622\",\"address\":{\"line1\":\"BIG HOUSE\",\"line2\":\"160 EVERGREEN ROAD\",\"line3\":\"LONDON\",\"postcode\":\"NW1 2XX\",\"countryCode\":\"GB\"}},\"requester\":\"806970609\",\"consultationNumber\":\"VMn-pts-mWB\",\"processingDate\":\"2020-12-08T08:18:36.546Z[Europe/London]\"}";
        var result = fastJSON.JSON.ToObject<VatNumberValidationResult>(payload);
        Assert.IsNotNull(result);
    }

This passes every time, regardless of the data being passed i.e. I have tried with a huge number of different companies and the correct information is returned every time.

Here is the live code, which is effectively identical:

    .....

    var payLoad = postResult.Payload;
    var fastJsonResult = fastJSON.JSON.ToObject<VatNumberValidationResult>(payLoad);

    .....

The value of the payload is the JSON copied and transferred to the Unit Test. The result of running this effectively identical code is that the target property is left empty:

    result = {Synergy.MtdVatModule.Model.VatNumberValidationResult}
        consultationNumber = "VMn-pts-mWB"
        processingDate = {08/12/2020 08:18:36}
        requester = "806970609"
        target = {Synergy.MtdVatModule.Model.VatTarget}
            address = {Synergy.MtdVatModule.Model.VatValidationAddress}
                countryCode = ""
                line1 = ""
                line2 = ""
                line3 = ""
                postcode = ""
            name = ""
            vatNumber = ""
            

whereas when it is done as part of a unit test it works perfectly every time:

    result = {Synergy.MtdVatModule.Model.VatNumberValidationResult}
        consultationNumber = "VMn-pts-mWB"
        processingDate = {08/12/2020 08:18:36}
        requester = "806970609"
        target = {Synergy.MtdVatModule.Model.VatTarget}
            address = {Synergy.MtdVatModule.Model.VatValidationAddress}
                countryCode = "GB"
                line1 = "BIG HOUSE"
                line2 = "160 EVERGREEN ROAD"
                line3 = "LONDON"
                postcode = "NW1 2XX"
            name = "EVERGREEN LIMITED"
            vatNumber = "123926622"

            

The only thing I can think of is that the saving of the JSON to text before processing is what is making the difference, but since it is already a valid JSON string I cannot see how that can be it. Copying the string to the payload variable as above makes no difference when compared to making the same call and passing postResult.Payload...

Can anyone provide any insight into this at all?

1

There are 1 answers

0
oldcoder On

@Fildor pointed me in the right direction with one of his comments where he said that if identical inputs produce different results the lib must be broken. In fact, the problem was with the Nuget packages in use - although they had been automatically updated they were at different versions. I updated both to the latest version, and that resolved the problem in the main application.

It would seem that there was a bug introduced in a more recent version of the lib which was fixed in the latest version. When both were updated to the newest version both returned the correct result.