Skip to content

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

@KondzioSSJ4

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

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