Apparent Bug in _normalizeItem

When upgrading to select2 4.0.3 on NuGet, I came cross a problem where selected items all say “[object Object]”. I traced the problem down to this method: https://github.com/select2/select2/blob/master/src/js/select2/data/select.js#L246. It appears that you are checking if the passed-in data is an empty object, and if it is not, you re-assign it with a new one with id and text populated with the passed in data. Then, text and id are assigned as:

if (item.id != null) {
  item.id = item.id.toString();
}

I think the solution is to not name the item you are initializing with the item passed in as a parameter, so the data isn’t overwritten. Then, it should just work. I’m prepared to submit a pull-request for this, should you want it.

For a further example, in case the above wasn’t clear enough, suppose item looks like this:

{
    "id": "id",
    "text": "text"
}

You get an object that looks like this:

{
    "id": {
        "id": "id"
        "text": "text"
    },
    "text": {
        "id": "id"
        "text": "text"
    }
}

Then, when it tries to assign the text field, it gets assigned “[object Object]” because it calls item.id = item.id.toString();, and item is the same instance in the left- and right-hand sides of the expression.

This might have been fixed in a recent PR that we merged (https://github.com/select2/select2/pull/5093). Can you try pulling v4.0.6-rc.0 and see if it fixes the problem?

It is fixed now :slight_smile:. How soon do you expect 4.0.6 to be stable-released, and will it be posted on NuGet?

:raised_hands: sweet! I’m not really sure but, maybe a month? The more people who can volunteer to use and test the pre-releases, the sooner we can find any regressions and declare them to be stable :smile:

Cool. If you publish the pre-release on NuGet, I can continue working on the upgrade, and report anything else I find. It’s a standard at my work that we need to get stuff through NuGet when it is published there.

It looks like someone else (who I don’t know) is the owner of the repo on NuGet: https://www.nuget.org/profiles/splattne

Let me see if I can get in touch with them, so we don’t have to publish multiple competing packages.

Sweet :slight_smile:

All the stable releases are published on NuGet now :slight_smile: . Unfortunately, this bug isn’t fixed until 4.0.6, so please give them a ping to publish that one when it is released, and I’ll get it.