Skip to content
Merged
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 @@ -106,14 +106,34 @@ import kotlinx.serialization.*
/**
* Returns a valid [{{classname}}] for [data], null otherwise.
*/
{{#jackson}}@JvmStatic
@JsonCreator
{{/jackson}}{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}fun decode(data: kotlin.Any?): {{classname}}? = data?.let {
{{^jackson}}
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}fun decode(data: kotlin.Any?): {{classname}}? = data?.let {
val normalizedData = "$it".lowercase()
values().firstOrNull { value ->
it == value || normalizedData == "$value".lowercase()
}
}
{{/jackson}}
{{#jackson}}
@JvmStatic
@JsonCreator
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}fun decode(data: kotlin.Any?): {{classname}}{{#isNullable}}?{{/isNullable}} {
if (data == null) {
{{#isNullable}}
return null
{{/isNullable}}
{{^isNullable}}
throw IllegalArgumentException("Value for {{classname}} cannot be null")
{{/isNullable}}
}
val normalizedData = "$data".lowercase()
return values().firstOrNull { value ->
data == value || normalizedData == "$value".lowercase()
}{{#isNullable}}{{/isNullable}}{{^isNullable}}{{#enumUnknownDefaultCase}}
?: {{#allowableValues}}{{#enumVars}}{{#-last}}{{&name}}{{/-last}}{{/enumVars}}{{/allowableValues}}{{/enumUnknownDefaultCase}}{{^enumUnknownDefaultCase}}
?: throw IllegalArgumentException("Unknown {{classname}} value: $data"){{/enumUnknownDefaultCase}}{{/isNullable}}
}
{{/jackson}}
}
}
{{#kotlinx_serialization}}{{#enumUnknownDefaultCase}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,61 @@ public void testJacksonEnumsUseJsonCreator() throws IOException {
TestUtils.assertFileContains(enumKt, "@JsonCreator");
}

@Test
public void testJacksonEnumsThrowForUnknownValue() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setLibrary("jvm-retrofit2")
.setAdditionalProperties(new HashMap<>() {{
put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");
put(CodegenConstants.MODEL_PACKAGE, "model");
}})
.setInputSpec("src/test/resources/3_0/kotlin/issue22534-kotlin-numeric-enum.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();

generator.opts(clientOptInput).generate();

final Path enumKt = Paths.get(output + "/src/main/kotlin/model/ExampleNumericEnum.kt");

// Verify that the decode function throws IllegalArgumentException for unknown values
TestUtils.assertFileContains(enumKt, "throw IllegalArgumentException(\"Unknown ExampleNumericEnum value: $data\")");
}

@Test
public void testJacksonEnumsWithUnknownDefaultCase() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("kotlin")
.setLibrary("jvm-retrofit2")
.setAdditionalProperties(new HashMap<>() {{
put(CodegenConstants.SERIALIZATION_LIBRARY, "jackson");
put(CodegenConstants.MODEL_PACKAGE, "model");
put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, "true");
}})
.setInputSpec("src/test/resources/3_0/kotlin/issue22534-kotlin-numeric-enum.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();

generator.opts(clientOptInput).generate();

final Path enumKt = Paths.get(output + "/src/main/kotlin/model/ExampleNumericEnum.kt");

// With enumUnknownDefaultCase=true, should return the default value instead of throwing
TestUtils.assertFileContains(enumKt, "@JsonEnumDefaultValue");
// Should NOT contain throw for unknown values when enumUnknownDefaultCase is enabled
TestUtils.assertFileNotContains(enumKt, "throw IllegalArgumentException(\"Unknown ExampleNumericEnum value");
}

@Test(description = "convert an empty model to object")
public void emptyModelKotlinxSerializationTest() throws IOException {
final Schema<?> schema = new ObjectSchema()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ enum class StringEnumRef(@get:JsonValue val value: kotlin.String) {
*/
@JvmStatic
@JsonCreator
fun decode(data: kotlin.Any?): StringEnumRef? = data?.let {
val normalizedData = "$it".lowercase()
values().firstOrNull { value ->
it == value || normalizedData == "$value".lowercase()
fun decode(data: kotlin.Any?): StringEnumRef {
if (data == null) {
throw IllegalArgumentException("Value for StringEnumRef cannot be null")
}
val normalizedData = "$data".lowercase()
return values().firstOrNull { value ->
data == value || normalizedData == "$value".lowercase()
}
?: unknown_default_open_api
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ enum class StringEnumRef(@get:JsonValue val value: kotlin.String) {
*/
@JvmStatic
@JsonCreator
fun decode(data: kotlin.Any?): StringEnumRef? = data?.let {
val normalizedData = "$it".lowercase()
values().firstOrNull { value ->
it == value || normalizedData == "$value".lowercase()
fun decode(data: kotlin.Any?): StringEnumRef {
if (data == null) {
throw IllegalArgumentException("Value for StringEnumRef cannot be null")
}
val normalizedData = "$data".lowercase()
return values().firstOrNull { value ->
data == value || normalizedData == "$value".lowercase()
}
?: unknown_default_open_api
}
}
}
Expand Down
Loading