Skip to content

Commit

Permalink
Merge pull request #671 from ctasada/ctasada/fix-kotlinx-collection
Browse files Browse the repository at this point in the history
(fix) Kotlin serializer for Collection
  • Loading branch information
ctasada authored Mar 29, 2024
2 parents 71ae935 + 6f091b5 commit a492b53
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(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 a492b53

Please sign in to comment.