Skip to content

AddCategories using provided list as reference (unexpected behaviour) #1162

Description

@KondzioSSJ4

Issue Summary

AddCategories use provided list as a reference
so any further add's impact on first provided collection

Steps to Reproduce

  1. Create a SendGridMessage
  2. Create a static List with 1 category (or provide it via IOptions<> as normal ppl do)
  3. Add that list to message using AddCategories
  4. Add other category (using AddCategories or AddCategory)
  5. See that list from the static field is bigger
  6. See that list from point 2 is bigger
  7. Send email (success)
  8. Retry steps 1-6
  9. Send email (failed, because there are duplicated category,)

Code Snippet

Your AddCategories looks like:

public void AddCategories(List<string> categories)
        {
            if (Categories == null)
            {
                Categories = new List<string>();
                Categories = categories;
            }
            else
            {
                Categories.AddRange(categories);
            }
        }

but it should be more like:

public void AddCategories(List<string> categories)
        {
            if (Categories == null)
            {
                Categories = new HashSet<string>(categories); // First change type to HashSet to prevent duplications
            }
            else
            {
                foreach (var item in categories) // It need to be like that because this is no longer List but HashSet to prevent duplication
                      Categories.Add(categories);
            }
        }

or at least change to be like:

public void AddCategories(List<string> categories)
        {
            if (Categories == null)
            {
                Categories = categories.ToList();
            }
            else
            {
                 Categories.AddRange(categories);
            }
        }

Technical details:

  • sendgrid-csharp version: 9.25.2.0 (newest)
  • csharp version: .netstandard 2.0

Activity

  1. changed the title [-]AddCategories useing provided list as reference (unexpected behaviour)[/-] [+]AddCategories using provided list as reference (unexpected behaviour)[/+] on Feb 9, 2022
  2. JenniferMah commented on Feb 11, 2022

    @JenniferMah
    Contributor

    Hi @KondzioSSJ4! Thanks for bringing this to our attention. This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions