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
fix: allow simple-json columns to hold non-object types (#5116) #5125
Conversation
wait, but why we should support a non-json value in a column that supposes to contain a json value? |
A valid use case of mine that gets broken by the regression is storing schemaless data as key-value pairs that support simple types as well as complex array/object types, like for an extensible configuration map (see basic example in #5116) |
Okay, so these changes were made by #4476 (issue #4440). I need feedback from @abhijeet1403 @vlapo @haydennyyy to conclude on a design decision around this feature. Do we want primitives to be handled by simple-json or not. Personally I'm not fan of this approach, but I found that Postgres's json type also does it. |
Note that #4476 introduced breaking changes that were not labelled as such and could break existing applications silently. I understand why some would want the behaviour introduced in that PR, but IMHO it should be made available as an opt-in, either using a new type (e.g. Also note that the problem outlined in #4440 could have been solved by ad-hoc defaults in the model and proper data validation prior to persisting data in the application, which are not really the responsibility of TypeORM. |
The reason for raising the PR was because for empty strings
Which is not the expected behavior and was breaking systems in case the JSON field was left empty/null. Its a behaviour of JS itself, checkout this stackoverflow post So the fix just adds checks to resolve values only if it is resolvable where the JSON field is empty in the database, and that it is being used to store JSON objects. For example the following test case was failing other wise,
If a flexible behavior is intended, wouldn't it be wise to use text as the data type and parse it to JSON as required. After all "simple-json" data type is just text with few utility functions to handle it as simple objects. In case it is still desirable to store non-object types the |
post.jsonField = ""; Well in that case, the value persisted in the database would be |
Regarding the empty column case you were talking about, However, silently replacing invalid JSON values with empty objects sounds rather dangerous and can lead to unwanted and unnoticed data loss. It is even more so for valid « unexpected » values. |
this sounds like a proper use case for reseting data in the field, but looks like it will be inappropriate use case. we can't normalize it to '""' because it will lead to bugs, and looks like we need to handle this case and throw an exception instead? I strongly don't like simple-json to be non-simple and confusing and thats exactly where we lead it to. |
Reseting a object is
What bugs specifically? I see the empty string
The simplest I can come-up with is that when you set a simple-json field to a value, it will return that same value when fetched from the database. Everything else is just magic and begging for bugs/confusion. It sounds to me that the ONLY real problem is the case where the database returns the empty string, which
With those semantics, you leave to the devs the responsibility to properly initialize the value, be consistent in the values they put inside the field, and validate their inputs. And you cannot do that in their stead without making assumptions about their models. This should be properly documented of course, but doesn't sound unreasonable at all |
@pleerock so, what do we do? |
I'm waiting to see other people feedback. Since many people already had some problems / fixes in this area maybe they can say something here. I don't want to merge it then get ( as usual:( )many negative feedback about functionality being constantly changed. |
I've been running into the same problem as the PR author, because using Making it opt-in (see: #5125 (comment)) sounds like a good idea imho. |
@pleerock I think let’s go with this PR. |
@svvac can you add tests and confirm if the test suite is passing ? |
@abhijeet1403 Can do but what's the plan? Get rid of object casting altogether? |
I think the proposed changes in this PR looks fine. |
@svvac If you're not updating this MR, should I go ahead, update this branch with tests and get these changes merged? |
…mpty object after a JSON parse failure
Closing this in favor of #6574 |
…mpty object after a JSON parse failure
…mpty object after a JSON parse failure
No description provided.