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

fix(model): Don't deserialize option values as Id's if option type is String #2190

Merged
merged 13 commits into from
Apr 23, 2023

Conversation

suneettipirneni
Copy link
Member

@suneettipirneni suneettipirneni commented Apr 3, 2023

Because of the way ValueEnvelope is declared it automatically puts a higher precedence on deserializing to an Id rather than a String.

This behavior is desired because currently, the deserialization of option values doesn't take the kind of the option JSON into account. So if an option actually is a snowflake type it would be serialized as such instead of becoming a String. This, however, does not account for situations where the string is a parseable Id, but is supposed to be represented as a String regardless.

As a result, this PR removes ValueEnvelope::Id. This means that means we initially deserialize any non-boolean or non-numeric values as Strings. Later on, when checking against the option kind we disambiguate the previously deserialized string and parse into the appropriate Id corresponding to the kind.

Closes #2178

@github-actions github-actions bot added c-model Affects the model crate t-fix Fixes a bug in the library labels Apr 3, 2023
@Erk-
Copy link
Member

Erk- commented Apr 4, 2023

Can you add a test for this case so it does not happen again in the future?

@vilgotf
Copy link
Member

vilgotf commented Apr 6, 2023

@suneettipirneni Sorry, it seems that my suggestion did not work after all. Testing locally with the latest commit resulted in the following error: invalid type: string "<SNOWFLAKE>", expected channel id. Intresting that serde-test does not result in the same code path as real world usage.

Otherwise this looks good.

@suneettipirneni
Copy link
Member Author

@suneettipirneni Sorry, it seems that my suggestion did not work after all. Testing locally with the latest commit resulted in the following error: invalid type: string "<SNOWFLAKE>", expected channel id. Intresting that serde-test does not result in the same code path as real world usage.

No worries, I've reverted the commit.

@MaxOhn
Copy link
Contributor

MaxOhn commented Apr 11, 2023

Now that things might get deserialized into an allocated String without having the need to, it might be a good time to consider serde_json::RawValue.
I'm unsure myself if it's sufficiently beneficial but I figured I throw it into the room at least.

The variant ValueEnvelope::String could hold a &'v RawValue or Box<RawValue> and then depending on what's needed one would parse the value accordingly at a later point.

This might be a topic for a subsequent new issue.

Edit: Scratch that, simd-json doesn't do well with RawValue 😅

Copy link
Member

@itohatweb itohatweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to #2190 (comment)

Copy link
Member

@itohatweb itohatweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a small nit-pick if you don't mind.

@vilgotf vilgotf enabled auto-merge April 23, 2023 19:31
@vilgotf vilgotf added this pull request to the merge queue Apr 23, 2023
Merged via the queue into twilight-rs:main with commit 7c69938 Apr 23, 2023
@suneettipirneni suneettipirneni deleted the fix/string-id-number-options branch April 23, 2023 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-model Affects the model crate t-fix Fixes a bug in the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String option trims prefixed zeros of integer input
5 participants