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: Fixes to the Kotlinx Module #680

Merged
merged 1 commit into from
Apr 1, 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 @@ -25,13 +25,14 @@ class KotlinxSerializationModelConverter(private val useFqn: Boolean = false) :
annotatedType: AnnotatedType, context: ModelConverterContext, chain: Iterator<ModelConverter>
): Schema<*>? {
if (annotatedType.type is Class<*>) {
val kClass = (annotatedType.type as Class<*>).kotlin
val jClass = annotatedType.type as Class<*>
val kClass = jClass.kotlin
val isKotlinSerializable = kClass.findAnnotation<Serializable>() != null

if (isKotlinSerializable) {
val schema = ObjectSchema()
schema.nullable = false
schema.name = kClass.simpleName
schema.name = getClassName(jClass)

kClass.memberProperties.forEach { property ->
val propertySchema = getPropertySchema(property, context)
Expand Down Expand Up @@ -94,8 +95,10 @@ class KotlinxSerializationModelConverter(private val useFqn: Boolean = false) :
private fun resolveRefSchema(type: Type, context: ModelConverterContext): Schema<*> {
val typeSchema = context.resolve(AnnotatedType(type))
if (typeSchema.type == "object") {
val name = getClassName(type as Class<*>)
context.defineModel(name, typeSchema)
val typeClass = type as Class<*>
val name = getClassName(typeClass)
// Deletes any previously defined model with the simpleName
context.defineModel(name, typeSchema, type, typeClass.simpleName)
return Schema<Any>().`$ref`(RefUtils.constructRef(name))
}
return typeSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void testClassWithNestedProperty() {
assertThat(nestedClass.getType()).isNull();
assertThat(nestedClass.get$ref()).isEqualTo("#/components/schemas/ClassWithNestedProperty$NestedClass");

final Schema<?> nestedModel = result.get(ClassWithNestedProperty.NestedClass.class.getSimpleName());
final Schema<?> nestedModel = result.get("ClassWithNestedProperty$NestedClass");
assertThat(nestedModel).isNotNull();
assertThat(nestedModel.getType()).isEqualTo("object");
assertThat(nestedModel.getProperties()).hasSize(3);
Expand All @@ -144,8 +144,7 @@ void serializeKotlin() {
converters.addConverter(modelConverter);

var media = converters.readAll(new AnnotatedType(SampleEvent.class));
// FIXME: The NestedClass is duplicated
assertThat(media).hasSize(3);
assertThat(media).hasSize(2);
final Schema<?> model = media.get(SampleEvent.class.getSimpleName());
assertThat(model).isNotNull();
assertThat(model.getType()).isEqualTo("object");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
{
"NestedClass" : {
"type" : "object",
"properties" : {
"color" : {
"type" : "string",
"enum" : [ "RED", "GREEN", "BLUE" ]
},
"id" : {
"type" : "integer",
"format" : "int32"
},
"name" : {
"type" : "string"
}
},
"required" : [ "color", "id", "name" ]
},
"SampleEvent" : {
"io.github.springwolf.addons.kotlinx_serialization_model_converter.converter.SampleEvent" : {
"type" : "object",
"properties" : {
"boolean_field" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
{
"NestedClass" : {
"type" : "object",
"properties" : {
"color" : {
"type" : "string",
"enum" : [ "RED", "GREEN", "BLUE" ]
},
"id" : {
"type" : "integer",
"format" : "int32"
},
"name" : {
"type" : "string"
}
},
"required" : [ "color", "id", "name" ]
},
"SampleEvent" : {
"type" : "object",
"properties" : {
Expand Down