sequelize / sequelize/sequelize
attributes with custom getters always included in `toJSON()` output regardless of whether the instance was instantiated with data for that attribute.
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
What are you doing?
I'm running a model.findAll({ attributes: [/* ... some fields */] }) and them calling toJSON() on each returned instance. AFAICT the problem is not related to findAll() perse, it happens with any instance however it was instantiated (assuming the instantiation was done with data that includes only a subset of the available/defined model attributes)
What do you expect to happen?
Not to have any [real] properties in the plain object returned by toJSON() that are not mentioned in attributes given to the find*() method (I would imagine that getters for virtual properties would be included only if they did not specify a field dependency OR if all their specified field depdendencies were included in the attributes).
What is actually happening?
toJSON() always returns properties for any model attribute that has a custom getter regardless of whether that attribute has a corresponding property in the underlying dataValues property of the instance.
Comments ...
I think the offending code is here (basically the loop over this._hasCustomGetters does not take into account whether a corresponding key in this.dataValues):
The complication in the scenario is that some custom getters are virtual (and may or may not have dependencies defined on real attributes) ... whichh makes defining the desired behaviour more tricky.
I can imagine that you may decide to keep the functionality as it is a merely document that custom getters always cause properties to exist in the objects generated by toJSON() ... though personally I think it is a bug in the context of real attributes that have custom getters because the toJSON() output shows a property with a value of null or undefined because the data was not retrieved in the underlying query (but the actual value in the DB is possibly set to something).
Info
Sequelize version: master
Tested with master branch: yes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/model.js around the loop over _hasCustomGetters, then reproduce the reported findAll({ attributes: [...] }) case and inspect the instance dataValues before toJSON(). Define the agreed behavior for real custom-getter attributes and virtual getters with dependencies, then verify that toJSON() matches that behavior for partially selected attributes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100