-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add omitNullValues -parameter to context; With omitNullValues you can…
… omit key-value pairs of the JSON input that have a value of 'null'. This fixes a bug, where Scala traits could not be deserialized properly, if some of the case classes of the trait used an Option[T] -type and the input included key-value pairs, that had the value of 'null'.
- Loading branch information
Showing
6 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/scala/fi/oph/scalaschema/OmitNullFromInputTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package fi.oph.scalaschema | ||
|
||
import org.scalatest.freespec.AnyFreeSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
trait SomeTrait { | ||
} | ||
|
||
case class SomeTraitBranch1(first: String, second: Option[String] = None, third: Option[String] = None) extends SomeTrait { | ||
} | ||
|
||
case class SomeTraitBranch2(second: String, third: Option[String] = None) extends SomeTrait { | ||
} | ||
|
||
case class SomeTraitBranch3(third: String) extends SomeTrait { | ||
} | ||
|
||
|
||
|
||
|
||
class OmitNullFromInputTest extends AnyFreeSpec with Matchers { | ||
"OmitNullFromInput" - { | ||
"When JSON contains a null value" - { | ||
implicit val context = ExtractionContext(SchemaFactory.default, omitNullFromInput = true) | ||
"Should extract case class SomeTraitBranch1 correctly" in { | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": "Example first"}""") should equal(Right(SomeTraitBranch1("Example first"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": "Example first", "second": null}""") should equal(Right(SomeTraitBranch1("Example first"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": "Example first", "second": null, "third": null}""") should equal(Right(SomeTraitBranch1("Example first"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": "Example first", "second": "Example second", "third": "Example third"}""") should equal(Right(SomeTraitBranch1("Example first",Some("Example second"),Some("Example third")))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": "Example first", "third": null}""") should equal(Right(SomeTraitBranch1("Example first"))) | ||
} | ||
"Should extract case class SomeTraitBranch2 correctly" in { | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": null, "second": "Example second", "third": null}""") should equal(Right(SomeTraitBranch2("Example second"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"second": "Example second", "third": null}""") should equal(Right(SomeTraitBranch2("Example second"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": null, "second": "Example second"}""") should equal(Right(SomeTraitBranch2("Example second"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"second": "Example second"}""") should equal(Right(SomeTraitBranch2("Example second"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"second": "Example second", "third": "Example third"}""") should equal(Right(SomeTraitBranch2("Example second", Some("Example third")))) | ||
} | ||
"Should extract case class SomeTraitBranch3 correctly" in { | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": null, "second": null, "third": "Example third"}""") should equal(Right(SomeTraitBranch3("Example third"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"first": null, "third": "Example third"}""") should equal(Right(SomeTraitBranch3("Example third"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"second": null, "third": "Example third"}""") should equal(Right(SomeTraitBranch3("Example third"))) | ||
SchemaValidatingExtractor.extract[SomeTrait]("""{"third": "Example third"}""") should equal(Right(SomeTraitBranch3("Example third"))) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters