Skip to content

Commit

Permalink
update document spec with refinement cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl committed Jan 17, 2025
1 parent d53e5de commit 081bb3e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,77 @@ class DocumentSpec() extends FunSuite {
)
}

test(
"Required refined field with null default"
) {
case class Test()
object Test extends ShapeTag.Companion[Test] {
def id: ShapeId = ShapeId("test", "Test")
def schema: Schema[Test] = Schema.constant(Test())
}
case class Foo(str: String)
case class Bar(foo: Foo)
implicit val provider: RefinementProvider[Test, String, Foo] =
Refinement.drivenBy[Test](str => Right(Foo.apply(str)), _.str)
val fieldSchema: smithy4s.schema.Field[Bar, Foo] =
Schema.string
.refined[Foo](
Test()
)
.required[Bar]("foo", _.foo)
.addHints(smithy.api.Default(Document.DNull))
implicit val schema: Schema[Bar] =
Schema.struct[Bar](fieldSchema)(Bar.apply)

expect.same(
Document.decode[Bar](
Document.DObject(Map("foo" -> Document.fromString("test")))
),
Right(Bar(Foo("test")))
)
expect.same(
Document.decode[Bar](Document.DObject(Map.empty)),
// Empty string here because null default is implied to be empty string
// for a non-nullable string field
Right(Bar(Foo("")))
)
}

test(
"Nullable required refined field with null default"
) {
case class Test()
object Test extends ShapeTag.Companion[Test] {
def id: ShapeId = ShapeId("test", "Test")
def schema: Schema[Test] = Schema.constant(Test())
}
case class Foo(str: String)
case class Bar(foo: Nullable[Foo])
implicit val provider: RefinementProvider[Test, String, Foo] =
Refinement.drivenBy[Test](str => Right(Foo.apply(str)), _.str)
val fieldSchema: smithy4s.schema.Field[Bar, Nullable[Foo]] =
Schema.string
.refined[Foo](
Test()
)
.nullable
.required[Bar]("foo", _.foo)
.addHints(smithy.api.Default(Document.DNull))
implicit val schema: Schema[Bar] =
Schema.struct[Bar](fieldSchema)(Bar.apply)

expect.same(
Document.decode[Bar](
Document.DObject(Map("foo" -> Document.fromString("test")))
),
Right(Bar(Nullable.value(Foo("test"))))
)
expect.same(
Document.decode[Bar](Document.DObject(Map.empty)),
Right(Bar(Nullable.Null))
)
}

private def inside[A, B](
a: A
)(assertPF: PartialFunction[A, Unit])(implicit loc: munit.Location) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ final class DefaultValueSpec extends FunSuite {
test("refined") {
val b: Schema[Int] =
Schema.int.refined(smithy.api.Range(None, Option(BigDecimal(1))))
testCaseOpt(b, None)
testCaseOpt(b, Some(0))
}

test("recursive") {
Expand Down

0 comments on commit 081bb3e

Please sign in to comment.