Skip to content

[1/6] api: Audit enums to handle unknown values where appropriate - #2339

Open
sm-sayedi wants to merge 11 commits into
zulip:mainfrom
sm-sayedi:1982-parse-enums-laxly
Open

[1/6] api: Audit enums to handle unknown values where appropriate#2339
sm-sayedi wants to merge 11 commits into
zulip:mainfrom
sm-sayedi:1982-parse-enums-laxly

Conversation

@sm-sayedi

@sm-sayedi sm-sayedi commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Towards #1982.

Goes through all the enums defined in lib/api (from top to bottom, both through the files and inside the files). Makes the enums accept unknown values or explains why we don't/can't accept them. For finding enum definitions inside a file, I used enum [^{]+\{ regex in VSCode "Find" feature (Cmd+F shortcut).

I have also taken the opportunity of going through all the API enums to use dot shorthands in their usages. To find all the usages of FooEnum, I used \bFooEnum(\.\w+) regex in VSCode "Search: Find in Files" feature (Shift+Cmd+F shortcut) with case-sensitivity on and used the capture group ($1) as the replacement.

@sm-sayedi
sm-sayedi force-pushed the 1982-parse-enums-laxly branch 3 times, most recently from 97db30e to e02550e Compare June 16, 2026 20:20
@sm-sayedi
sm-sayedi marked this pull request as ready for review June 16, 2026 20:37
@sm-sayedi sm-sayedi added the maintainer review PR ready for review by Zulip maintainers label Jun 16, 2026
@sm-sayedi
sm-sayedi requested a review from rajveermalviya June 16, 2026 20:37
@sm-sayedi
sm-sayedi force-pushed the 1982-parse-enums-laxly branch 2 times, most recently from 8b2e321 to 611b6b6 Compare July 18, 2026 04:19
@rajveermalviya

Copy link
Copy Markdown
Member

Thanks for working on this @sm-sayedi! This is a large PR (61 commits), I think it'd easier for review if this was divided in multiple PRs instead. So could you please divide it into N PRs, and if you want you can label each with "X/Y" (where X would be the N-th PR of the group, and Y would be the total number of PR in the group, Y could be just left as "N" if total number of PR is not clear.)

sm-sayedi added 11 commits July 28, 2026 00:35
We'll soon change Message.type to use MessageType instead of String.
This will make it compatible with the modern values "channel" and
"direct" when the server starts sending them in the future.

Having it next to Message seems more appropriate than its previous
location.
…omJson

And change MessageTypeConverter.fromJson to forward to MessageType.fromJson.

In future commits, we can use the compatibility logic independent
of MessageTypeConverter; simply by using MessageType.fromJson.
As of 2026-06, the server only sends "stream" type we already support.

This change is for accepting the modern "channel" type that the server
may start sending in the future.
This way, it can also accept "channel" and "direct" as the valid values
for the message type when the server starts sending them in the future.

As of 2026-06, the server only sends "stream" and "private" for message
types. Now, we map them to "channel" and "direct" in deserialization.
The modern type "channel" was added in server-9 (FL-248).
@sm-sayedi
sm-sayedi force-pushed the 1982-parse-enums-laxly branch from 611b6b6 to e6bad69 Compare July 27, 2026 20:26
@sm-sayedi sm-sayedi changed the title Audit API enums to handle unknown values where appropriate [1/6] api: Audit enums to handle unknown values where appropriate Jul 27, 2026
@sm-sayedi

Copy link
Copy Markdown
Collaborator Author

@rajveermalviya Done as requested, with a series of 6 PRs of around 10 commits each. PTAL.

@rajveermalviya rajveermalviya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @sm-sayedi! I've read through first 7 commits of this PR, comment below.

4983a7c dart [nfc]: Use dot shorthands for MessageType
80bed4b api [nfc]: Move MessageType next to Message
bb5b96a api [nfc]: Move MessageTypeConverter.fromJson logic to MessageType.fromJson
f854928 api: Replace stream with channel for MessageType
9c56c05 api: Change Message.type to MessageType, from String
1aa4aac api: Explain why MessageType cannot be unknown
945522d api: In sendMessage, use "channel" when supported

After reading through these commits, one PR separation feedback would be to include the enums that PR is migrating/auditing in the title or description. Or better yet, we could have one PR with related commits for each enum.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

api: In sendMessage, use "channel" when supported
…

This is nice to have, but let's move it into another PR, because this is unrelated to #1982 and this series of PRs is already pretty large.

Comment thread lib/api/model/model.dart
stream,
direct;

factory fromJson(String json) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Don't think we've used nameless constructors yet.

Suggested change
factory fromJson(String json) {
factory MessageType.fromJson(String json) {

Also without the name, it's easy to confuse it as a method.

Comment thread lib/api/model/events.dart
Comment on lines +1595 to +1596
// The server never actually sends "channel" here yet (it's "stream" instead),
// but we accept both forms for forward-compatibility.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's have a comment/dartdoc on MessageType explaining this. (For both channel and direct messages). Then we don't have to put this comment on every place MessageType is used.

Comment thread lib/api/model/model.dart
Comment on lines +1301 to 1303
if (json == 'stream') json = 'channel'; // TODO(server-future)
if (json == 'private') json = 'direct'; // TODO(server-future)
return $enumDecode(_$MessageTypeEnumMap, json);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Now that there are more than one cases, let's use switch pattern matching.

Comment thread lib/api/model/events.dart

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

api: Replace `stream` with `channel` for MessageType
…

This too is technically out of scope for #1982, but it's fine to have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer review PR ready for review by Zulip maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants