From 323310892454b920dcaa426dc7aa2f82ca7cb9cf Mon Sep 17 00:00:00 2001 From: Jeff Lewis Date: Wed, 6 Mar 2024 14:16:52 -0700 Subject: [PATCH] fix big struct schema rendering (#1433) * fix big struct schema rendering * update changelog * fix tests for bigstruct --- CHANGELOG.md | 4 ++++ .../src/generated/smithy4s/example/BigStruct.scala | 11 +++++++---- .../test/src/smithy4s/BigStructSmokeSpec.scala | 2 +- .../src/smithy4s/codegen/internals/Renderer.scala | 2 +- sampleSpecs/misc.smithy | 3 ++- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 709992bad..fec88bc91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/modules/bootstrapped/src/generated/smithy4s/example/BigStruct.scala b/modules/bootstrapped/src/generated/smithy4s/example/BigStruct.scala index 9b1da71ca..0379d33b8 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/BigStruct.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/BigStruct.scala @@ -5,9 +5,10 @@ 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") @@ -15,7 +16,7 @@ object BigStruct extends ShapeTag.Companion[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), @@ -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], @@ -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) } diff --git a/modules/bootstrapped/test/src/smithy4s/BigStructSmokeSpec.scala b/modules/bootstrapped/test/src/smithy4s/BigStructSmokeSpec.scala index 07d531fb6..09ca05ee3 100644 --- a/modules/bootstrapped/test/src/smithy4s/BigStructSmokeSpec.scala +++ b/modules/bootstrapped/test/src/smithy4s/BigStructSmokeSpec.scala @@ -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) diff --git a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala index 1d3e7de9a..c4d71aaec 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/Renderer.scala @@ -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)}]" } ) diff --git a/sampleSpecs/misc.smithy b/sampleSpecs/misc.smithy index 22df654ba..27f8f3f9d 100644 --- a/sampleSpecs/misc.smithy +++ b/sampleSpecs/misc.smithy @@ -53,8 +53,9 @@ structure BigStruct { a21: Integer @required a22: Integer + a23: String @required - a23: Integer + a24: Integer } @enum([