Skip to content

Commit

Permalink
fix: Fixes to the Kotlinx Module
Browse files Browse the repository at this point in the history
Fixed 2 bugs with the Kotlinx Module

* When using FQN, the parent classes where using the simple name instead of the fqn
* Nested classes where wrongly defined more than one time
  • Loading branch information
ctasada committed Apr 1, 2024
1 parent 708617a commit 929e0ef
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 42 deletions.
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

0 comments on commit 929e0ef

Please sign in to comment.