Skip to content

Commit

Permalink
(fix) Kotlin serializer for Collection
Browse files Browse the repository at this point in the history
The new Kotlin Serialization converter was not
properly managing the `Collection` types.
  • Loading branch information
ctasada committed Mar 25, 2024
1 parent 71ae935 commit f78b272
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,14 @@ class KotlinxSerializationModelConverter(private val useFqn: Boolean = false) :
val propertyType = property.returnType.jvmErasure

when {
propertyType.isSubclassOf(List::class) -> {
propertyType.isSubclassOf(List::class) || propertyType.isSubclassOf(Collection::class) -> {
propertySchema = ArraySchema()
val value = (property.returnType.javaType as ParameterizedType).actualTypeArguments[0]
propertySchema.items = resolveRefSchema(value, context)
}

propertyType.isSubclassOf(Set::class) -> {
propertySchema = ArraySchema()
val value = (property.returnType.javaType as ParameterizedType).actualTypeArguments[0]
propertySchema.items = resolveRefSchema(value, context)
propertySchema.uniqueItems = true
if (propertyType.isSubclassOf(Set::class)) {
propertySchema.uniqueItems = true
}
}

propertyType.isSubclassOf(Map::class) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ void testClassWithListProperty() {
assertThat(listField.getItems()).isNotNull();
assertThat(listField.getItems().getType()).isEqualTo("string");
}

@Test
void testClassWithCollectionProperty() {
var result = modelConverters.readAll(new AnnotatedType(ClassWithCollectionProperty.class));
Schema<?> schema = result.get(ClassWithCollectionProperty.class.getSimpleName());

final Schema<?> listField = (Schema<?>) schema.getProperties().get("collection_field");
assertThat(listField).isNotNull();
assertThat(listField.getType()).isEqualTo("array");
assertThat(listField.getNullable()).isFalse();
assertThat(listField.getItems()).isNotNull();
assertThat(listField.getItems().getType()).isEqualTo("string");
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ data class ClassWithSetProperty(
val setField: Set<String>,
)

@Serializable
data class ClassWithCollectionProperty(
@SerialName("collection_field")
val setField: Collection<String>,
)

@Serializable
data class ClassWithEnumProperty(
@SerialName("enum_field")
Expand Down

0 comments on commit f78b272

Please sign in to comment.