Skip to content

fix: Support all serverpod types in Map keys. - #1799

Merged
Isakdl merged 17 commits into
serverpod:mainfrom
Isakdl:toJsonDeep
Jan 24, 2024
Merged

Isakdl merged 17 commits into
serverpod:mainfrom
Isakdl:toJsonDeep

Conversation

@Isakdl

@Isakdl Isakdl commented Jan 22, 2024 •

Copy link
Copy Markdown
Collaborator

Changes

  • Moves serialization logic into extensions rather than handling them in the encoder. This change means that the toJson methods will generate a complete Map structure n-levels deep rather than just the top level.
  • Adds a big test suit for the generated toJson and allToJson methods.
  • Adds additional serialization/deserialization tests
  • Fixes broken serialization of map Keys that were not of type String or int, now any supported serverpod type should be handled by the serilization logic when used as a key in a Map.

Motivation: For upcoming changes to how we handle relational resolution we have to call resolve this Map for these keys. It makes the most sense to be consistent and making sure the entire tree is built the same way. This approach also aligns with how all other dart toJson implementations are done.

Closes: #1806

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

None

@Isakdl
Isakdl marked this pull request as draft January 22, 2024 16:29
@Isakdl
Isakdl force-pushed the toJsonDeep branch 2 times, most recently from 093cf0c to 3e6d8af Compare January 23, 2024 14:23
@Isakdl
Isakdl marked this pull request as ready for review January 23, 2024 14:41
@Isakdl
Isakdl requested a review from SandPod January 23, 2024 14:41
@jason-downs

Copy link
Copy Markdown

Yes, I was just about to file an issue/feature request for this - we should at least be able to have ints as Map keys. Only having Strings is way too limiting and there are a lot of use cases I currently have where I need int keys.

@jason-downs

Copy link
Copy Markdown

Also if we currently CAN have ints as Map keys as your PR description seems to suggest (?) then the documentation is definitely wrong as it says only non-nullable Strings are currently supported.

@Isakdl

Isakdl commented Jan 23, 2024

Copy link
Copy Markdown
Collaborator Author

You are correct! the documentation is wrong here. ints already work!

@jason-downs

jason-downs commented Jan 23, 2024 •

Copy link
Copy Markdown

Do enums that are serialized by name already work too?

Reason I'm wondering is that I'm currently encountering a bug with some serializable classes I have with Map fields, in one case it's with int keys, and in another its with enum (serialized byName) keys. When these maps are empty it causes the serverpod deserializer to mess up and try to typecast the Map and a List and crashes, becuase it seems behind the scenes these Maps are being represented as Lists (with items as Maps of "k":blah, "v":blah"), which seems to work when it's not empty but crashes when empty.

I thought this might just because I was using officially 'unsupported' Map key types here (ints and String enums) but if they are supported then I need to file a separate issue for this.

OK this is actually a lot of description, sorry for all this, I probably should just go ahead and file an issue instead of here.

(UPDATE: I created an issue for this: #1806 )

@Isakdl

Isakdl commented Jan 24, 2024 •

Copy link
Copy Markdown
Collaborator Author

@jason-downs Thank you for the issue!

Do enums that are serialized by name already work too?

They work but not as a key in a Map, until this PR is merged.

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Awesome work! Really happy to see all these tests being added and that we finally provide a proper toJson interface.

I have a couple of question.

It was my mistake to review this PR commit by commit. There are a couple of comments that then become outdated.

I still think it would be helpful in the future to squash together amending commits that fix issues in the same. PRs are a great way to preserve documentation around how changes are introduces and the motivation behind them. And bug fixing seldom add much value.

So making them as easy as possible to understand is a small effort that I believe helps future developers :)

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! 👍

@Isakdl
Isakdl merged commit 0d8ecea into serverpod:main Jan 24, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crashes with empty Map, causes type cast error

3 participants