Skip to content

Commit

Permalink
treat all members as clientOptional when container is marked with @input
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd committed Oct 2, 2023
1 parent 55594f3 commit 903ce1c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.*
import software.amazon.smithy.model.traits.ClientOptionalTrait
import software.amazon.smithy.model.traits.DefaultTrait
import software.amazon.smithy.model.traits.InputTrait
import software.amazon.smithy.model.traits.StreamingTrait
import java.util.logging.Logger

Expand Down Expand Up @@ -184,7 +185,10 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
.apply {
if (nullableIndex.isMemberNullable(shape, settings.api.nullabilityCheckMode)) nullable()

if (!shape.hasTrait<ClientOptionalTrait>()) { // @ClientOptional supersedes @default
// @clientOptional supersedes @default
val container = model.expectShape(shape.container)
val isClientOptional = shape.hasTrait<ClientOptionalTrait>() || container.hasTrait<InputTrait>()
if (!isClientOptional) {
shape.getTrait<DefaultTrait>()?.let {
defaultValue(it.getDefaultValue(targetShape))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ShapeValueGenerator(
ShapeType.STRUCTURE -> {
if (params.isNullNode) {
params.accept(nodeVisitor)
}else {
} else {
classDeclaration(writer, shape.asStructureShape().get()) {
params.accept(nodeVisitor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class SymbolProviderTest {
@input
structure MyStruct {
@required
@default(2)
quux: QuuxType
}
Expand Down
11 changes: 9 additions & 2 deletions tests/codegen/nullability-tests/model/nullability.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ service NullCheckService {
version: "1"
}

operation SayHello { input: TestInput }
operation SayHello {
input: TestInput
}

@input
structure TestInput {
nested: TestStruct
nested: TestStruct,

@required
@default("hammer")
tay: String
}

structure TestStruct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package smithy.kotlin.nullability

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

class DefaultValueTest {
@Test
fun testDefaultsClientMode() {
val actual = smithy.kotlin.nullability.client.model.SayHelloRequest.Builder()
.build()

// all members of structure marked with `@input` are implicitly `@clientOptional`
assertNull(actual.tay)
}

@Test
fun testDefaultsClientCarefulMode() {
val actual = smithy.kotlin.nullability.clientcareful.model.SayHelloRequest.Builder()
.build()

// all members of structure marked with `@input` are implicitly `@clientOptional`
assertNull(actual.tay)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package smithy.kotlin.nullability

import kotlin.test.*

/**
* Test nullability of generated types and error correction behavior
*/
class ErrorCorrectionTest {
@Test
fun testErrorCorrectionClientMode() {
Expand Down

0 comments on commit 903ce1c

Please sign in to comment.