diff --git a/CHANGELOG.md b/CHANGELOG.md index f563d45a0..886e386d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Thank you! # 0.18.25 +* Fixes a regression from 0.18.4 which incorrectly rendered default values for certain types (see [#1593](https://github.com/disneystreaming/smithy4s/pull/1593)) * Fixes an issue in which union members targetting Unit would fail to compile when used as traits (see [#1600](https://github.com/disneystreaming/smithy4s/pull/1600)). * Make the `transform` method in generated `*Gen` algebras final. This should make it possible to derive e.g. `FunctorK` instances in cats-tagless automatically (see [#1588](https://github.com/disneystreaming/smithy4s/pull/1588)). diff --git a/modules/bootstrapped/src/generated/smithy4s/example/DefaultNotCapitalized.scala b/modules/bootstrapped/src/generated/smithy4s/example/DefaultNotCapitalized.scala new file mode 100644 index 000000000..eb201f159 --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/DefaultNotCapitalized.scala @@ -0,0 +1,22 @@ +package smithy4s.example + +import smithy4s.Hints +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.ShapeTag +import smithy4s.schema.Schema.struct + +final case class DefaultNotCapitalized(name: Username = smithy4s.example.Username("hello")) + +object DefaultNotCapitalized extends ShapeTag.Companion[DefaultNotCapitalized] { + val id: ShapeId = ShapeId("smithy4s.example", "DefaultNotCapitalized") + + val hints: Hints = Hints.empty + + // constructor using the original order from the spec + private def make(name: Username): DefaultNotCapitalized = DefaultNotCapitalized(name) + + implicit val schema: Schema[DefaultNotCapitalized] = struct( + Username.schema.required[DefaultNotCapitalized]("name", _.name).addHints(smithy.api.Default(smithy4s.Document.fromString("hello"))), + )(make).withId(id).addHints(hints) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/Username.scala b/modules/bootstrapped/src/generated/smithy4s/example/Username.scala new file mode 100644 index 000000000..9c7fab14b --- /dev/null +++ b/modules/bootstrapped/src/generated/smithy4s/example/Username.scala @@ -0,0 +1,15 @@ +package smithy4s.example + +import smithy4s.Hints +import smithy4s.Newtype +import smithy4s.Schema +import smithy4s.ShapeId +import smithy4s.schema.Schema.bijection +import smithy4s.schema.Schema.string + +object Username extends Newtype[String] { + val id: ShapeId = ShapeId("smithy4s.example", "username") + val hints: Hints = Hints.empty + val underlyingSchema: Schema[String] = string.withId(id).addHints(hints) + implicit val schema: Schema[Username] = bijection(underlyingSchema, asBijection) +} diff --git a/modules/bootstrapped/src/generated/smithy4s/example/package.scala b/modules/bootstrapped/src/generated/smithy4s/example/package.scala index 568c337a0..b36f14d6a 100644 --- a/modules/bootstrapped/src/generated/smithy4s/example/package.scala +++ b/modules/bootstrapped/src/generated/smithy4s/example/package.scala @@ -118,6 +118,7 @@ package object example { type UVIndex = smithy4s.example.UVIndex.Type type UnicodeRegexString = smithy4s.example.UnicodeRegexString.Type type UnwrappedFancyList = smithy4s.example.UnwrappedFancyList.Type + type Username = smithy4s.example.Username.Type type ValidatedString = smithy4s.example.ValidatedString.Type } \ No newline at end of file diff --git a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala index ac0d50ac0..f6b6e2f87 100644 --- a/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala +++ b/modules/codegen/src/smithy4s/codegen/internals/CollisionAvoidance.scala @@ -154,15 +154,28 @@ private[internals] object CollisionAvoidance { private def modField(field: Field): Field = { Field( - protectKeyword(uncapitalise(field.name)), - field.name, - modType(field.tpe), - field.modifier, - field.originalIndex, - field.hints.map(modHint) + name = protectKeyword(uncapitalise(field.name)), + realName = field.name, + tpe = modType(field.tpe), + modifier = modModifier(field.modifier), + originalIndex = field.originalIndex, + hints = field.hints.map(modHint) ) } + private def modModifier(modifier: Field.Modifier): Field.Modifier = + Field.Modifier( + required = modifier.required, + nullable = modifier.nullable, + default = modifier.default.map(modFieldDefault) + ) + + private def modFieldDefault(default: Field.Default): Field.Default = + Field.Default( + node = default.node, + typedNode = default.typedNode.map(recursion.preprocess(modTypedNode)) + ) + private def modStreamingField( streamingField: StreamingField ): StreamingField = { diff --git a/sampleSpecs/defaults.smithy b/sampleSpecs/defaults.smithy index 9615d02ef..868024331 100644 --- a/sampleSpecs/defaults.smithy +++ b/sampleSpecs/defaults.smithy @@ -72,3 +72,11 @@ structure DefaultVariants { } structure DefaultInMixinUsageTest with [DefaultInMixinTest] {} + +// Regression test for https://github.com/disneystreaming/smithy4s/issues/1592 +structure DefaultNotCapitalized { + @required + name: username = "hello" +} + +string username