`PatchWithOptions` can't specify removal of optional fields
See original GitHub issueGiven 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:
- Created 7 years ago
- Reactions:2
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 havedropNullKeys
set tofalse
, but you can create your own printer withdropNullKeys
set totrue
, and use that to output the JSON: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 ofX
andY
. This is possible using aeson.