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

Not setting undefined values on read to required default values #531

Closed
mobsense opened this issue Jun 27, 2024 · 1 comment
Closed

Not setting undefined values on read to required default values #531

mobsense opened this issue Jun 27, 2024 · 1 comment
Labels
bug Something isn't working fixed Issue has been fixed in the repo

Comments

@mobsense
Copy link
Contributor

Looks like if you revise your schema and add a default value, then read old items that don't have that field defined .... the default values are not applied.

Currently default values are only being applied on create.

This quick patch addresses this:

diff --git a/src/Model.js b/src/Model.js
index ed1ce2c..68889ee 100644
--- a/src/Model.js
+++ b/src/Model.js
@@ -1134,7 +1134,7 @@ export class Model {
                 value = this.decrypt(value)
             }
             if (field.default !== undefined && value === undefined) {
-                value = field.default
+                rec[name] = field.default
             } else if (value === undefined) {

We'll test this out on production systems to ensure it doesn't break anything else.

@mobsense mobsense added the bug Something isn't working label Jun 27, 2024
@mobsense mobsense changed the title Not setting undefined values on read to required default valuesIssue title Not setting undefined values on read to required default values Jun 27, 2024
@mobsense
Copy link
Contributor Author

Here is a more complete patch that passes all unit tests:

This handles requests that use params.fields.

diff --git a/src/Model.js b/src/Model.js
index ed1ce2c..4f44ff2 100644
--- a/src/Model.js
+++ b/src/Model.js
@@ -1123,18 +1123,17 @@ export class Model {
                     let [att, sep, index] = field.encode
                     value = (raw[att] || '').split(sep)[index]
                 }
-                if (value === undefined) {
-                    continue
-                }
             }
-            if (sub) {
+            if (sub && value) {
                 value = value[sub]
             }
             if (field.crypt && params.decrypt !== false) {
                 value = this.decrypt(value)
             }
             if (field.default !== undefined && value === undefined) {
-                value = field.default
+                if (!params.fields || params.fields.indexOf(name) >= 0) {
+                    rec[name] = field.default
+                }
             } else if (value === undefined) {

@mobsense mobsense added the fixed Issue has been fixed in the repo label Jun 28, 2024
@mobsense mobsense closed this as completed Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has been fixed in the repo
Projects
None yet
Development

No branches or pull requests

1 participant