Skip to content

Commit

Permalink
Include field modifiers in collision avoidance (#1593)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz authored Sep 30, 2024
1 parent 00f6531 commit c8b5a04
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions modules/bootstrapped/src/generated/smithy4s/example/Username.scala
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 8 additions & 0 deletions sampleSpecs/defaults.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c8b5a04

Please sign in to comment.