Adaptivecards: Deserializing a card twice in .NET fails with ID collision errors

Created on 13 Jul 2019  路  10Comments  路  Source: microsoft/AdaptiveCards

Platform

What platform is your issue or question related to? (Delete other platforms).

  • .NET object model

Author or host

Someone deserializing cards (issue was reported by internal MS customer)

Version of SDK

NuGet 1.2.0

Issue

If you use JSON.NET to deserialize a card that's within another object and then immediately deserialize the same card again, a collision ID occurs... even though it's deserializing two separate instances.

It seems like the collision ID logic is using a shared static or something when the AdaptiveCard isn't the top-level thing being deserialized.

Repro code (install latest .NET 1.2.0 from NuGet)

public class cards
{
    public AdaptiveCard Card { get; set; }
}

private void Test()
{
    string cardsJson = @"{ ""card"": {
""type"": ""AdaptiveCard"",
""version"": ""1.0"",
""body"": [
{
    ""type"": ""Input.Text"",
    ""id"": ""myTextInput""
}
]
} }";

    cards fullcardresponse = JsonConvert.DeserializeObject<cards>(cardsJson);

    // Deserialize again, exception gets thrown here
    fullcardresponse = JsonConvert.DeserializeObject<cards>(cardsJson);
}
AdaptiveCards.AdaptiveSerializationException: 'Collision detected for id 'myTextInput''

Workaround

If you use the official AdaptiveCard.FromJson method, it works fine. This problem is limited to deserializing a JSON object that inside it contains an Adaptive Card. So workaround is that you could parse the initial object and then grab the card as a JObject and then parse that using the official parsing method

Bug Platform-.NET Triage-Investigate

All 10 comments

@paulcam206 mind taking a stab at this since you are the id collision guru :) thanks!

I have faced similar problem. I have read Card from JSON using AdaptiveCard.FromJson() method. I have there an AdapriveContainer which I should repeat undetermined number of times (based on object count read from DB). I was not able to find anything like Clone or CopyFrom method to clone my template AdaptiveContainer. So I have decided to Serialize and Deserialize object to create a new instance.
Code
var clonedContainer = JsonConvert.DeserializeObject<AdaptiveContainer>(JsonConvert.SerializeObject(contactContainer));
worked on version 1.0.0 but stopped on version 1.2.2

I have tried workaround above

var containerString = JsonConvert.SerializeObject(contactContainer);
var jObject = JObject.Parse(containerString);
var clonedContainer = jObject.ToObject<AdaptiveContainer>();

with no luck. Same issue with collision detected on first AdaptiveTextBlock element.

Ended with workaround:
Remove all ids from json template for now

This issue has been automatically marked as stale because it has not had any activity for 5 days.

we got around this problem by putting the FromJson function within a semaphore wait block as below


_semaphore.WaitOne();
            try
            {
                var jsonCard = AdaptiveCard.FromJson(json);
                return jsonCard.Card;
            }
            finally
            {
                _semaphore.Release();
            }

this function is in itself within a singleton class

Strange. I'm pretty sure I had a single thread there

Has someone found out the solution?

I am using AdaptiveCard 1.2.4 and can't reproduce this using the original issue. Was this fixed?

I've got this issue still occurring with the 2.1.0 Nuget package.
I have a List of AdaptiveElements that is deserialized from json with JSON.net that is pulled from a database, and then injected into a card, e.g.

public AdaptiveCard MakeCard(string dbJson){
    var elements = JsonConvert.DeserializeObject<List<AdaptiveElement>>(dbJson);
    var card = new AdaptiveCard("1.2");
    card.Body.AddRange(elements);
    // other stuff...
    return card;
}

It'll work the first time, and then on subsequent calls this will throw the "collision detected for id "foo"" exception when doing DeserializeObject().

Thanks for looking into this stale issue ericrrichards; Resetting staleness. @andrewleader FYI.

Any update or closure on this issue ? I am facing it. and every time it gives different error.

Was this page helpful?
0 / 5 - 0 ratings