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

Union member fix #186

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/codegen/src/smithy4s/codegen/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
val cn = caseName(altName)
block(s"object $cn")(
renderHintsVal(altHints),
s"val schema: $Schema_[$cn] = bijection(${tpe.schemaRef}, $cn(_), _.${uncapitalise(altName)})",
s"val schema: $Schema_[$cn] = bijection(${tpe.schemaRef}.withHints(hints), $cn(_), _.${uncapitalise(altName)})",
s"""val alt = schema.oneOf[$name]("$realName")"""
)
},
Expand Down
8 changes: 4 additions & 4 deletions modules/example/src/smithy4s/example/Foo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ object Foo extends smithy4s.ShapeTag.Companion[Foo] {

object IntCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[IntCase] = bijection(int, IntCase(_), _.int)
val schema: smithy4s.Schema[IntCase] = bijection(int.withHints(hints), IntCase(_), _.int)
val alt = schema.oneOf[Foo]("int")
}
object StrCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[StrCase] = bijection(string, StrCase(_), _.str)
val schema: smithy4s.Schema[StrCase] = bijection(string.withHints(hints), StrCase(_), _.str)
val alt = schema.oneOf[Foo]("str")
}
object BIntCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[BIntCase] = bijection(bigint, BIntCase(_), _.bInt)
val schema: smithy4s.Schema[BIntCase] = bijection(bigint.withHints(hints), BIntCase(_), _.bInt)
val alt = schema.oneOf[Foo]("bInt")
}
object BDecCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[BDecCase] = bijection(bigdecimal, BDecCase(_), _.bDec)
val schema: smithy4s.Schema[BDecCase] = bijection(bigdecimal.withHints(hints), BDecCase(_), _.bDec)
val alt = schema.oneOf[Foo]("bDec")
}

Expand Down
6 changes: 3 additions & 3 deletions modules/example/src/smithy4s/example/ObjectService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ object ObjectServiceGen extends smithy4s.Service[ObjectServiceGen, ObjectService

object ServerErrorCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[ServerErrorCase] = bijection(ServerError.schema, ServerErrorCase(_), _.serverError)
val schema: smithy4s.Schema[ServerErrorCase] = bijection(ServerError.schema.withHints(hints), ServerErrorCase(_), _.serverError)
val alt = schema.oneOf[PutObjectError]("ServerError")
}
object NoMoreSpaceCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[NoMoreSpaceCase] = bijection(NoMoreSpace.schema, NoMoreSpaceCase(_), _.noMoreSpace)
val schema: smithy4s.Schema[NoMoreSpaceCase] = bijection(NoMoreSpace.schema.withHints(hints), NoMoreSpaceCase(_), _.noMoreSpace)
val alt = schema.oneOf[PutObjectError]("NoMoreSpace")
}

Expand Down Expand Up @@ -146,7 +146,7 @@ object ObjectServiceGen extends smithy4s.Service[ObjectServiceGen, ObjectService

object ServerErrorCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[ServerErrorCase] = bijection(ServerError.schema, ServerErrorCase(_), _.serverError)
val schema: smithy4s.Schema[ServerErrorCase] = bijection(ServerError.schema.withHints(hints), ServerErrorCase(_), _.serverError)
val alt = schema.oneOf[GetObjectError]("ServerError")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ object ImportServiceGen extends smithy4s.Service[ImportServiceGen, ImportService

object NotFoundErrorCase {
val hints : smithy4s.Hints = smithy4s.Hints()
val schema: smithy4s.Schema[NotFoundErrorCase] = bijection(NotFoundError.schema, NotFoundErrorCase(_), _.notFoundError)
val schema: smithy4s.Schema[NotFoundErrorCase] = bijection(NotFoundError.schema.withHints(hints), NotFoundErrorCase(_), _.notFoundError)
val alt = schema.oneOf[ImportOperationError]("NotFoundError")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ import schematic.ByteArray
import smithy.api.JsonName
import smithy4s.http.PayloadError
import smithy4s.syntax._
import weaver._
import smithy4s.example.{
CheckedOrUnchecked,
Four,
One,
PayloadData,
TestBiggerUnion,
Three,
UntaggedUnion
}
import smithy4s.api.Discriminated
import weaver._

import scala.collection.immutable.ListMap
import smithy4s.example.PayloadData
import smithy4s.example.{TestBiggerUnion, One}
import smithy4s.example.{UntaggedUnion, Three, Four}
import scala.util.Try

object SchematicJCodecTests extends SimpleIOSuite {

Expand Down Expand Up @@ -162,6 +169,24 @@ object SchematicJCodecTests extends SimpleIOSuite {
expect(str == jsonStr)
}

pureTest("Valid union values are parsed successfuly") {
val jsonStr = """{"checked":"foo"}"""
val result = readFromString[CheckedOrUnchecked](jsonStr)
expect(result == CheckedOrUnchecked.CheckedCase("foo"))
}

pureTest("Invalid union values fails to parse") {
val jsonStr = """{"checked":"!@#"}"""
val result = Try(readFromString[CheckedOrUnchecked](jsonStr)).failed
expect(
result.get == PayloadError(
PayloadPath.fromString(".checked"),
"string",
"String '!@#' does not match pattern '^\\w+$'"
)
)
}

pureTest("Discriminated union gets encoded correctly") {
val jsonBaz = """{"type":"baz","str":"test"}"""
val jsonBin = """{"type":"binBin","binStr":"foo","int":2022}"""
Expand Down
7 changes: 7 additions & 0 deletions sampleSpecs/misc.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ structure BigStruct{
{value: "_"},
])
string EnumWithSymbols


union CheckedOrUnchecked {
@pattern("^\\w+$")
checked: String,
raw: String
}