Skip to content

Commit

Permalink
update rules to use default values only when T not T?
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd committed Oct 3, 2023
1 parent 3b455aa commit 8b21600
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
val isNullable = nullableIndex.isMemberNullable(shape, settings.api.nullabilityCheckMode)
if (isNullable) nullable()

// only use @default if type is `T` (not `T?`) or marked `@required` (in which case it will be serialized anyway)
if (!isNullable || shape.isRequired) {
// only use @default if type is `T`
if (!isNullable) {
shape.getTrait<DefaultTrait>()?.let {
defaultValue(it.getDefaultValue(targetShape))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,28 +387,6 @@ class SymbolProviderTest {
assertEquals("null", memberSymbol.defaultValue())
}

@Test
fun `required @clientOptional with @default`() {
val model = """
structure MyStruct {
@required
@clientOptional
@default("Foo")
quux: QuuxType
}
string QuuxType
""".prependNamespaceAndService().toSmithyModel()

val provider: SymbolProvider = KotlinCodegenPlugin.createSymbolProvider(model)
val member = model.expectShape<MemberShape>("com.test#MyStruct\$quux")
val memberSymbol = provider.toSymbol(member)
assertEquals("kotlin", memberSymbol.namespace)
// still nullable due to `@clientOptional` but we use the default due to `@required`
assertTrue(memberSymbol.isNullable)
assertEquals("\"Foo\"", memberSymbol.defaultValue())
}

@Test
fun `@input with default`() {
val model = """
Expand All @@ -426,32 +404,10 @@ class SymbolProviderTest {
val memberSymbol = provider.toSymbol(member)
assertEquals("kotlin", memberSymbol.namespace)
assertTrue(memberSymbol.isNullable)
// @input makes every member implicitly @clientOptional
assertEquals("null", memberSymbol.defaultValue())
}

@Test
fun `@input with required`() {
val model = """
@input
structure MyStruct {
@required
@default(2)
quux: QuuxType
}
long QuuxType
""".prependNamespaceAndService().toSmithyModel()

val provider: SymbolProvider = KotlinCodegenPlugin.createSymbolProvider(model)
val member = model.expectShape<MemberShape>("com.test#MyStruct\$quux")
val memberSymbol = provider.toSymbol(member)
assertEquals("kotlin", memberSymbol.namespace)

// still nullable due to `@input` but we can use the default due to `@required`
assertTrue(memberSymbol.isNullable)
assertEquals("2L", memberSymbol.defaultValue())
}

@Test
fun `creates blobs`() {
val model = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package smithy.kotlin.nullability

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class DefaultValueTest {
Expand All @@ -15,9 +14,9 @@ class DefaultValueTest {
.build()

// all members of structure marked with `@input` are implicitly `@clientOptional`
// which means they are nullable. We ignore `@default` unless it's marked `@required`
// which means they are nullable.
assertNull(actual.tay)
assertEquals("ball", actual.lep)
assertNull(actual.lep)
}

@Test
Expand All @@ -26,8 +25,8 @@ class DefaultValueTest {
.build()

// all members of structure marked with `@input` are implicitly `@clientOptional`
// which means they are nullable. We ignore `@default` unless it's marked `@required`
// which means they are nullable.
assertNull(actual.tay)
assertEquals("ball", actual.lep)
assertNull(actual.lep)
}
}

0 comments on commit 8b21600

Please sign in to comment.