Skip to content

Fix #4082: Remove name() method generation for unnamed Smithy enums #4083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,27 @@ class PythonConstrainedEnum(
}

private fun pyEnumName(context: EnumGeneratorContext): Writable =
writable {
rustBlock(
"""
##[getter]
pub fn name(&self) -> &str
""",
) {
rustBlock("match self") {
context.sortedMembers.forEach { member ->
val memberName = member.name()?.name
rust("""${context.enumName}::$memberName => ${memberName?.dq()},""")
// Only named enums have a `name` property. Do not generate `fn name` for
// unnamed enums.
if (context.enumTrait.hasNames()) {
writable {
rustBlock(
"""
##[getter]
pub fn name(&self) -> &str
""",
) {
rustBlock("match self") {
context.sortedMembers.forEach { member ->
val memberName = member.name()?.name
check(memberName != null) { "${context.enumTrait} cannot have null members" }
rust("""${context.enumName}::$memberName => ${memberName?.dq()},""")
}
}
}
}
} else {
writable {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import software.amazon.smithy.rust.codegen.server.python.smithy.testutil.generat
import kotlin.io.path.appendText

internal class PythonServerTypesTest {
@Test
fun `document type`() {
val model =
"""
private fun getModel(
structures: String,
vararg errors: String = emptyArray(),
) = """
namespace test

use aws.protocols#restJson1
use smithy.framework#ValidationException

@restJson1
service Service {
Expand All @@ -36,16 +37,25 @@ internal class PythonServerTypesTest {
operation Echo {
input: EchoInput,
output: EchoOutput,
${if (errors.isNotEmpty()) "errors: [${errors.joinToString(", ")}]," else ""}
}

$structures
""".asSmithyModel()

@Test
fun `document type`() {
val model =
getModel(
"""
structure EchoInput {
value: Document,
}

structure EchoOutput {
value: Document,
}
""".asSmithyModel()
""",
)

val (pluginCtx, testDir) = generatePythonServerPluginContext(model)
executePythonServerCodegenVisitor(pluginCtx)
Expand Down Expand Up @@ -151,26 +161,8 @@ internal class PythonServerTypesTest {
@Test
fun `timestamp type`() {
val model =
"""
namespace test

use aws.protocols#restJson1
use smithy.framework#ValidationException

@restJson1
service Service {
operations: [
Echo,
],
}

@http(method: "POST", uri: "/echo")
operation Echo {
input: EchoInput,
output: EchoOutput,
errors: [ValidationException],
}

getModel(
"""
structure EchoInput {
@required
value: Timestamp,
Expand All @@ -182,7 +174,9 @@ internal class PythonServerTypesTest {
value: Timestamp,
opt_value: Timestamp,
}
""".asSmithyModel()
""",
"ValidationException",
)

val (pluginCtx, testDir) = generatePythonServerPluginContext(model)
executePythonServerCodegenVisitor(pluginCtx)
Expand Down Expand Up @@ -240,4 +234,50 @@ internal class PythonServerTypesTest {

cargoTest(testDir)
}

@Test
fun `unnamed enum type`() {
val model =
getModel(
"""
structure EchoInput {
@required
unnamedValue: Choice,
unnamedOptValue: Choice,
@required
namedValue: Choice,
namedOptValue: Choice,
}

structure EchoOutput {
@required
unnamedValue: Choice,
unnamedOptValue: Choice,
@required
namedValue: Choice,
namedOptValue: Choice,
}

@enum([
{
value: "t2.nano",
},
{
value: "t2.micro",
},
{
value: "m256.mega",
deprecated: true
}
])
string Choice
""",
"ValidationException",
)

val (pluginCtx, testDir) = generatePythonServerPluginContext(model)
executePythonServerCodegenVisitor(pluginCtx)

cargoTest(testDir)
}
}