Skip to content

Conversation

@ertembiyik
Copy link

Better support for optionals, arrays and dictionaries

Problem

The current ConvexMobile encoding implementation has limited support for common Swift collection types, leading to compilation errors when trying to use arrays and dictionaries with ConvexEncodable values.

Issue encountered:
When attempting to encode a [String] array, the compiler throws an error:

Generic struct 'Array' requires the types 'String' and '(any ConvexEncodable)?' be equivalent 

Example that fails with the current implementation:

let tools = ["hammer", "screwdriver", "wrench"] let args: [String: any ConvexEncodable] = [ "id": agentId, "tools": tools // ❌ Compilation error ]

The workaround required manual casting:

args["tools"] = tools.map { $0 as ConvexEncodable? } as [ConvexEncodable?]

Solution

This PR enhances the ConvexEncodable protocol support by adding proper extensions for:

  1. Generic Array support: Array<Element: ConvexEncodable> replaces the specific [ConvexEncodable?] extension
  2. Generic Dictionary support: Dictionary<String, ConvexEncodable> replaces the specific [String: ConvexEncodable?] extension
  3. Optional support: Optional<Wrapped: ConvexEncodable> provides clean null encoding
  4. Removed unneeded .sorted() call for better performance

After this PR

The problematic example now works seamlessly:

let tools = ["hammer", "screwdriver", "wrench"] let userData = ["name": "John", "email": "john@example.com"] let args: [String: any ConvexEncodable] = [ "id": agentId, "tools": tools, // ✅ Works directly "metadata": userData // ✅ Works directly ]

The changes are backward compatible.

This improvement makes ConvexMobile more intuitive to use with standard Swift collection types while maintaining the same encoding behavior.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Better support for optionals, arrays and dictionaries
@ertembiyik ertembiyik mentioned this pull request Sep 2, 2025
@nipunn1313
Copy link
Collaborator

Hi! Thanks for the PR. It may be a little while before we get around to reviewing this as mobile has been stable for a while, but appreciate that energy in the mobile swift area.

Recommend folks running into this to use the manual cast for now.

@dowski
Copy link
Contributor

dowski commented Oct 23, 2025

Hey thanks for this contribution! If you can add a few tests to cover the new behavior, I'll see about getting it merged sometime soon.

@ertembiyik
Copy link
Author

ertembiyik commented Oct 23, 2025

Hey thanks for this contribution! If you can add a few tests to cover the new behavior, I'll see about getting it merged sometime soon.

hey! sure i can add them soon. i wanted to discuss if you plan to support Encodable as part of the params in convex methods (query/mutation/action) since currently they use dict. i think it would be safer and more convenient for users to call those methods via Encodable instead of dict. curious to hear your thoughts on this one and should it be done in a separate pr or here (since convex may confirm Encodable to ConvexEncodable and expose it)

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

Labels

None yet

3 participants