Skip to content
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

(fix) Kotlin serializer for Collection #671

Merged
merged 2 commits into from
Mar 29, 2024
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 @@ -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