Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Javascript] TypeError: Cannot read property 'isValid' of undefined #4767

Closed
scottlovegrove opened this issue Sep 11, 2020 · 4 comments · Fixed by #4791
Closed

[Javascript] TypeError: Cannot read property 'isValid' of undefined #4767

scottlovegrove opened this issue Sep 11, 2020 · 4 comments · Fixed by #4791
Assignees
Milestone

Comments

@scottlovegrove
Copy link
Contributor

Platform

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

  • JavaScript

Author or host

Author

If you're an author, who are you sending cards to?

Custom renderer, but not important for this bug

Version of SDK

2.3.0 of adaptivecards on npm

Details

If I try and return an adaptive card as part of an API response, and the adaptive card isn't the root item, I'm getting the following error:

TypeError: Cannot read property 'isValid' of undefined
    at Version.compareTo (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\serialization.ts:66:44)
    at AdaptiveCard.SerializableObject.internalToJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\serialization.ts:870:75)
    at AdaptiveCard.Container.internalToJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\card-elements.ts:5449:29)
    at AdaptiveCard.ContainerWithActions.internalToJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\card-elements.ts:6157:29)
    at AdaptiveCard.internalToJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\card-elements.ts:6424:29)
    at AdaptiveCard.SerializableObject.toJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\serialization.ts:913:18)
    at AdaptiveCard.CardElement.toJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\card-elements.ts:326:28)
    at AdaptiveCard.toJSON (D:\doist\adaptivecards-repro\node_modules\adaptivecards\src\card-elements.ts:6548:28)
    at JSON.stringify (<anonymous>)
    at stringify (D:\doist\adaptivecards-repro\node_modules\express\lib\response.js:1123:12)

I've managed to create a repro project with this in. Just run npm i && npm run start then point to http://localhost:4001/card to reproduce
adaptivecards-repro.zip

The key part to reproducing the problem is this:

res.send({
        card: myAdaptiveCard,
    });

What then happens is card is passed through as the context of toJSON which results in the above error when comparing versions.

@ghost ghost added this to the 20.09 milestone Sep 11, 2020
@ghost ghost added the Area-Inconsistency Bugs around renderer inconsistencies across different platforms label Sep 11, 2020
@ghost
Copy link

ghost commented Sep 11, 2020

please review this issue for target Milestone, Inconsistencies & Priority upon triage.

@golddove golddove added Triage-Approved for Fix and removed Area-Inconsistency Bugs around renderer inconsistencies across different platforms Triage-Needed labels Sep 18, 2020
@shalinijoshi19
Copy link
Member

@dclaux can you help take a look at this real quick ? Thanks

@dclaux
Copy link
Member

dclaux commented Sep 21, 2020

@scottlovegrove wow that is due to a toJSON() quirk I wasn't aware of, but which is documented on MDN (namely that toJSON() gets passed a parameter when it is invoked automatically by JSON.stringify() - via res.send() in this case). The AC version of toJSON() takes an optional parameter of type SerializationContext and assumes that it is of that specific type if not undefined, which in your example isn't the case - the parameter is the string "card".

I will fix, but in the meantime you can work around the problem by explicitly calling toJSON() on your card first:

    const adaptiveCard = new AdaptiveCard();
    adaptiveCard.version = new Version(1, 2);

    const textBlock = new TextBlock();
    textBlock.text = 'Sample';

    adaptiveCard.addItem(textBlock);

    let withThisItWillFail = {
        card: adaptiveCard
    }

    const serializedCard = adaptiveCard.toJSON();

    let withThisItWontFail = {
        card: serializedCard
    }

    JSON.stringify(withThisItWontFail);

@shalinijoshi19 shalinijoshi19 removed this from the 20.09 milestone Sep 21, 2020
@ghost ghost added the AdaptiveCards v20.09 label Sep 21, 2020
@shalinijoshi19 shalinijoshi19 added this to the 20.09 milestone Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants