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 for reading Some(Map.empty) #529

Merged
merged 3 commits into from
Sep 11, 2023
Merged

Conversation

martinw-ct
Copy link
Contributor

@martinw-ct martinw-ct commented Sep 4, 2023

A small failing test plus my suggested fix for what I think may be a bug in how it handles empty maps in an option. 🤔

Specifically {} in json when read into a Option[Map[String, _]] becomes None instead of Map.empty.

@martinw-ct martinw-ct changed the title Failing test demonstrating issue Failing test demonstrating issue with reading Some(Map.empty) Sep 4, 2023
@martinw-ct
Copy link
Contributor Author

martinw-ct commented Sep 11, 2023

A very simple fix is to remove JObject(Nil) from here

case JNothing | JNull | JObject(Nil) => validNone

But it looks very intentional that a {} be mapped to None in general (e.g. a case class with only optional fields) and I strongly suspect some callers have come to depend on this, so that probably isn't the way to go.

@martinw-ct martinw-ct marked this pull request as ready for review September 11, 2023 07:47
@martinw-ct martinw-ct changed the title Failing test demonstrating issue with reading Some(Map.empty) Fix for reading Some(Map.empty) Sep 11, 2023
@yanns
Copy link
Contributor

yanns commented Sep 11, 2023

A very simple fix is to remove JObject(Nil) from here

case JNothing | JNull | JObject(Nil) => validNone

But it looks very intentional that a {} be mapped to None in general (e.g. a case class with only optional fields) and I strongly suspect some callers have come to depend on this, so that probably isn't the way to go.

When we used json to map objects into mongo, we tried hard not to optimize the payload. For example, for an optional field, we removed all "field": null to save a payload without the "field" field.

I guess that JObject(Nil) is in the same vein? Instead of saving "field": {}, we tried to save a payload without the "field" field? (I'm not sure about this one)

We can also try to remove it, make some test release and check if all tests are passing. WDYT?

@yanns
Copy link
Contributor

yanns commented Sep 11, 2023

I don't understand why we treat Option[Map] differently from Option[List]. This does not make sense.

@martinw-ct
Copy link
Contributor Author

martinw-ct commented Sep 11, 2023

We can also try to remove it, make some test release and check if all tests are passing. WDYT?

I tried removing the JObject instead of the hacky specialized reader (because your comment made sense) and hit something very strange.

There are tests asserting that Option[Int] when None gets written out and read back as {} and they fell over. Fixing the tests were easy and I'm pretty sure that codepath was never hit on prod, just in some internal benchmarks and test code. Here is it in another branch 5f97030

Shall put that commit in this PR? I am now mildly concerned there is a Mongo record somewhere with {} instead of null though. 🤔

@yanns
Copy link
Contributor

yanns commented Sep 11, 2023

We can also try to remove it, make some test release and check if all tests are passing. WDYT?

I tried removing the JObject instead of the hacky specialized reader (because your comment made sense) and hit something very strange.

There are tests asserting that Option[Int] when None gets written out and read back as {} and they fell over. Fixing the tests were easy and I'm pretty sure that codepath was never hit on prod, just in some internal benchmarks and test code. Here is it in another branch 5f97030

Shall put that commit in this PR? I am now mildly concerned there is a Mongo record somewhere with {} instead of null though. 🤔

Yes, we should not have any null at the end. This PR with a specialized typeclass seems more safe.

@martinw-ct martinw-ct added this pull request to the merge queue Sep 11, 2023
Merged via the queue into main with commit e667059 Sep 11, 2023
7 checks passed
@martinw-ct martinw-ct deleted the cut-adhoc-flaky-discount-test branch September 11, 2023 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants