question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`PatchWithOptions` can't specify removal of optional fields

See original GitHub issue

Given a case class with an optional field:

case class Foo(foo: Option[Int])

When deriving a patch for that class:

Decoder[Foo => Foo]

The patch behavior is not correct:

val a = Foo(Some(22))
def patchIt(json: String) = parse(json).flatMap(_.as[Foo]).map(_(a))

patchIt("{}") // Foo(Some(22)) - expected
patchIt("""{"foo": 33}""") // Foo(Some(33)) - expected
patchIt("""{"foo": null}""") // Foo(Some(22)) - should be Foo(None)

The behavior I described above is necessary in order to ever have the ability to remove fields via patching.

It would probably be as simple as defining an explicit decoder for Option[Option[T]] which does what’s described above - if the field isn’t present, it yields None; but if it is present and explicitly null, it yields Some(None). I think that could be considered correct in the general case of Option[Option[T]], not just for patch purposes – though I can’t think of a reason you would use that type outside of the patch use case.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jeremyrsmithcommented, Jul 21, 2016

@Tvaroh what you’re seeing isn’t related to this issue. The null values in JSON representation arise from a property of the Printer, which is configurable. The default printers have dropNullKeys set to false, but you can create your own printer with dropNullKeys set to true, and use that to output the JSON:

//before
myJson.noSpaces  //includes nulls

//after
myJson.pretty(Printer.noSpaces.copy(dropNullKeys = true)) // nulls are not included
1reaction
bergmarkcommented, Mar 7, 2017

Thanks @jeremyrsmith, this is useful to me.

But note that using a custom printer makes the assumption that you want to treat all null keys equally. This is something I think should be decided on the instance level instead.

e.g. given C(a = X(None), b = Y(None)) we by default get{ "a" : { "x" : null }, "b" : { "y": null } }. It might be necessary to turn this into { "a" : {}, "b" : { y : null } } and this decision should IMO be made in the instance declarations of X and Y. This is possible using aeson.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hide empty details containers [#2409639] | Drupal.org
We decided to fix the UI problem by using the available option on the details container. Removing all fieldset logic is out of...
Read more >
INDEX_LOCATION_ATTRIBUTE...
The INDEX_LOCATION_ATTRIBUTE_NAME classpath attribute can be used to explicitly specify the location for the index file. However, this is currently used ...
Read more >
[Patch] Terrible post effects must be disableable (lense flares ...
So how about REMOVING THESE UGLY EFFECTS or at least giving us FINE and DETAILED controls over each of such effect for performance...
Read more >
MySQL Error 1093 - Can't specify target table for update in ...
Upvoted this answer because I had to delete items and could not get info from another table, had to subquery from same table....
Read more >
io.circe.Decoder Scala Example - ProgramCreek.com
getOrElse(name, throw new RuntimeException(s"Unable to find analyzer by name: ... final case class LanguageConfiguration(extensions: Option[Set[String]]) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found