Skip to content

Commit

Permalink
fix big struct schema rendering (#1433)
Browse files Browse the repository at this point in the history
* fix big struct schema rendering

* update changelog

* fix tests for bigstruct
  • Loading branch information
lewisjkl authored Mar 6, 2024
1 parent 816ac1e commit 3233108
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.18.12

* fix issue where schemas for fields of generated big structs (over 22 fields in size) would not be ordered correctly

# 0.18.11

* smithy4s Structure schemas are now retaining the original order of fields, as per the specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.ShapeTag
import smithy4s.schema.Schema.int
import smithy4s.schema.Schema.string
import smithy4s.schema.Schema.struct

final case class BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int)
final case class BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a24: Int, a23: Option[String] = None)

object BigStruct extends ShapeTag.Companion[BigStruct] {
val id: ShapeId = ShapeId("smithy4s.example", "BigStruct")

val hints: Hints = Hints.empty

// constructor using the original order from the spec
private def make(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int): BigStruct = BigStruct(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23)
private def make(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Option[String], a24: Int): BigStruct = BigStruct(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a24, a23)

implicit val schema: Schema[BigStruct] = struct.genericArity(
int.required[BigStruct]("a1", _.a1),
Expand All @@ -40,7 +41,8 @@ object BigStruct extends ShapeTag.Companion[BigStruct] {
int.required[BigStruct]("a20", _.a20),
int.required[BigStruct]("a21", _.a21),
int.required[BigStruct]("a22", _.a22),
int.required[BigStruct]("a23", _.a23),
string.optional[BigStruct]("a23", _.a23),
int.required[BigStruct]("a24", _.a24),
){
arr => make(
arr(0).asInstanceOf[Int],
Expand All @@ -65,7 +67,8 @@ object BigStruct extends ShapeTag.Companion[BigStruct] {
arr(19).asInstanceOf[Int],
arr(20).asInstanceOf[Int],
arr(21).asInstanceOf[Int],
arr(22).asInstanceOf[Int],
arr(22).asInstanceOf[Option[String]],
arr(23).asInstanceOf[Int],
)
}.withId(id).addHints(hints)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BigStructSmokeSpec() extends FunSuite {

test("Big structures do have a schema") {
val expected =
"structure BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int)"
"structure BigStruct(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Option[String], a24: Int)"
val schemaRepr =
smithy4s.example.BigStruct.schema.compile(SchemaDescriptionDetailed)
expect.same(schemaRepr, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ private[internals] class Renderer(compilationUnit: CompilationUnit) { self =>
.args(renderedFields)
.block(
line"arr => make".args(
fields.zipWithIndex.map { case (field, idx) =>
fields.sortBy(_.originalIndex).zipWithIndex.map { case (field, idx) =>
line"arr($idx).asInstanceOf[${Line.fieldType(field)}]"
}
)
Expand Down
3 changes: 2 additions & 1 deletion sampleSpecs/misc.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ structure BigStruct {
a21: Integer
@required
a22: Integer
a23: String
@required
a23: Integer
a24: Integer
}

@enum([
Expand Down

0 comments on commit 3233108

Please sign in to comment.