-
Notifications
You must be signed in to change notification settings - Fork 7
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
Serializing Descrimination Union Not Selecting Correct Member #101
Comments
Thanks for the report. Could you share your version and the
This is the same as a literal shorthand in TS API. This needs to be a function because the ReScript lang type system doesn't allow overloads.
ReScript Schema does it automatically 🙂 It Looks like, for some reason, the |
As for the version, I was on 8.3.0, but I just upgraded to 8.4.0 (latest) to see if that would resolve the issue. Setting aside the API differences, the behavior in this case appears to be exactly the same between 8.3 and 8.4. |
OK. New information. If I change the 'response' field from a Union to S.string, all the cases work correctly. So, it appears that having a discriminated Union with a field which is also a Union seems to be causing some issue. Notably, it is this field that is being left out when the object is serialized. I checked with other non-primitive schema properties and they don't seem to trigger the problem. |
Super weird. I'm currently refactoring this code for V9. It's going to be more robust and performant. It's not ready yet, but I can probably cherry-pick some changes and release I'll keep you updated. |
Also, there's a chance that |
Yeah, I reproduced the issue. Unfortunately, the cherry-picking is not possible as I initially thought. For now, I set |
Published https://github.com/DZakh/rescript-schema/releases/tag/v9.0.0-rc.1 with a fix. It still requires to write a change log and update documentation, but the code isn't going to be changed until the official v9 release. |
I have a schema which defines a number of different messages to be used within a protocol. There are individual schema for each message type and a union used by the protocol which includes all message types in the protocol. All of the message schema have a discriminator field (in this case 'type') which indicates the type of message, and by extension the correct/matching schema.
When parsing, this works fine. However, when serializing the wrong schema seems to be selected for the serialization and whether the correct or incorrect schema is selected seems to depend on the ordering in the union.
In the Rescript API, there appears to be a special 'tag' construct that allows specifying the discriminator. Other libraries also allow specifying the discriminator for these types of unions. It seems like it would be both more reliable and more performant to leverage an explicit discriminator when available.
The text was updated successfully, but these errors were encountered: