-
-
Notifications
You must be signed in to change notification settings - Fork 132
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
feat(core): infer $class when obvious #577
Conversation
Signed-off-by: Matt Roberts <[email protected]>
Signed-off-by: Matt Roberts <[email protected]>
// class/subclass we can choose that instead | ||
const subclasses = classDeclaration.getAssignableClassDeclarations() | ||
.filter(clazz => !clazz.isAbstract()); | ||
if (subclasses.length === 1){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is risky imo because it opens up a scenario whereby deserialisation can break because you've simply added a new concept which extends an abstract type. This will be confusing to document and to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be much safer (and more work!) if this was explicit in the model, via a final
declaration. Properties, or arrays, of final types would never need $class when serialised or deserialised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could map final
to sealed
in C# and to final
in Java.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will also need to check the same logic applies to relationships.
--> IAddress address
vs. --> Address address
Closing in light of @dselman's comments. |
I agree with Dan's comment about deserialization. The approach I recommend is to simply treat an object as an instance of the declared field type if it's not annotated. This should be even simpler to implement than this PR (because you don't have to care about how many concrete assignable types there may be), more general, and would not break in the case Dan describes. |
Closes #542
This change allows us to validate and deserialize JSON objects where the root type is known, but where no $class properties for nested objects are provided. This will work for basic cases where there is only one possible concrete subtype.
For example, an object such as this will validate provided that we tell the validator what the root type is.
Author Checklist
--signoff
option of git commit.main
fromfork:branchname