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

fix(Util): flatten ignoring certain fields #7773

Merged
merged 4 commits into from
Apr 25, 2022
Merged

Conversation

newracket
Copy link
Contributor

@newracket newracket commented Apr 11, 2022

Please describe the changes this PR makes and why it should be merged:
Fixes #7712

Improves clarity of toJSON functions throughout. Currently, some fields are ignored in toJSON if the value tied to that field is an object (as pointed out by issue #7712). This change fixes issue #7712 by making it so fields tied to an object don't get ignored.

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

@newracket
Copy link
Contributor Author

This is what Message.toJSON() looks like before I made the change.

{
  "channelId": "723654712410374194",
  "guildId": "605969074178228224",
  "id": "963209574242979900",
  "createdTimestamp": 1649717458068,
  "type": 0,
  "system": false,
  "content": "",
  "authorId": "605975599609020437",
  "pinned": false,
  "tts": false,
  "nonce": null,
  "embeds": [
    {}
  ],
  "components": [],
  "attachments": [],
  "stickers": [],
  "editedTimestamp": null,
  "webhookId": null,
  "groupActivityApplicationId": null,
  "applicationId": null,
  "activity": null,
  "flags": 0,
  "reference": null,
  "interaction": null,
  "cleanContent": ""
}

This is what it looks like with the change. Both mentions and embeds were omitted previously, but with the change, they're both now included

{
  "channelId": "723654712410374194",
  "guildId": "605969074178228224",
  "id": "963209574242979900",
  "createdTimestamp": 1649717458068,
  "type": 0,
  "system": false,
  "content": "",
  "authorId": "605975599609020437",
  "pinned": false,
  "tts": false,
  "nonce": null,
  "embeds": [
    {
      "data": {
        "type": "rich",
        "author": {
          "name": "hey"
        }
      }
    }
  ],
  "components": [],
  "attachments": [],
  "stickers": [],
  "editedTimestamp": null,
  "mentions": {
    "everyone": false,
    "users": [],
    "roles": [],
    "crosspostedChannels": [],
    "repliedUser": null
  },
  "webhookId": null,
  "groupActivityApplicationId": null,
  "applicationId": null,
  "activity": null,
  "flags": 0,
  "reference": null,
  "interaction": null,
  "cleanContent": ""
}

@iCrawl iCrawl added this to the discord.js v14 milestone Apr 17, 2022
@kyranet
Copy link
Member

kyranet commented Apr 21, 2022

One of my concerns here is... shouldn't we try to use .toJSON where possible?

With the switch to raw data, we may have nested data, which is what happens here in your sample:

  "embeds": [
    {
+     "data": {
        "type": "rich",
        "author": {
          "name": "hey"
        }
+     }
    }
  ],

If .toJSON were to be preferred and bypass the JSON serialization from the utility, data would get dropped, and the data would be "flat".

@newracket
Copy link
Contributor Author

@kyranet I made a second commit making it try to use .toJSON if it exists (both when the element's an object and when it's an array). The same Message.toJSON() from my previous commit now looks like this.

{
  "channelId": "723654712410374194",
  "guildId": "605969074178228224",
  "id": "963209574242979900",
  "createdTimestamp": 1649717458068,
  "type": 0,
  "system": false,
  "content": "",
  "authorId": "605975599609020437",
  "pinned": false,
  "tts": false,
  "nonce": null,
  "embeds": [
    {
      "type": "rich",
      "author": {
        "name": "hey"
      }
    }
  ],
  "components": [],
  "attachments": [],
  "stickers": [],
  "editedTimestamp": null,
  "mentions": {
    "everyone": false,
    "users": [],
    "roles": [],
    "crosspostedChannels": [],
    "repliedUser": null,
    "members": [],
    "channels": []
  },
  "webhookId": null,
  "groupActivityApplicationId": null,
  "applicationId": null,
  "activity": null,
  "flags": 0,
  "reference": null,
  "interaction": null,
  "cleanContent": ""
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Message#toJSON() issue
6 participants