SMILEY4 / schema-kenerator

Analyze kotlin types, extract information and generate schemas
Apache License 2.0
9 stars 5 forks source link

Parsing generic type (ContextualSerializer) to `TypeId` #38

Open srhee91 opened 3 weeks ago

srhee91 commented 3 weeks ago

Issue found

When processing KotlinxSerialization, BaseTypeData for contexuals are generated with a TypeId as such:

image

code is here: https://github.com/SMILEY4/schema-kenerator/blob/b5c2d56ded43beaf47e4720af6758c3aaefc9201/schema-kenerator-serialization/src/main/kotlin/io/github/smiley4/schemakenerator/serialization/steps/KotlinxSerializationTypeProcessingStep.kt#L266


However, in order to compileReferencingRoot to map to SwaggerSchema, it tries to resolves the contextuals with a generated TypeId as such:

image

code is here: https://github.com/SMILEY4/schema-kenerator/blob/b5c2d56ded43beaf47e4720af6758c3aaefc9201/schema-kenerator-swagger/src/main/kotlin/io/github/smiley4/schemakenerator/swagger/steps/SwaggerSchemaCompileReferenceStep.kt#L50


As a result, the TypeId doesn't match and fails to reference from schemaList. code is here: https://github.com/SMILEY4/schema-kenerator/blob/b5c2d56ded43beaf47e4720af6758c3aaefc9201/schema-kenerator-swagger/src/main/kotlin/io/github/smiley4/schemakenerator/swagger/steps/SwaggerSchemaCompileReferenceStep.kt#L51-L52


Suggestion

First, with respect, I carefully request more explanation on the comment in order for me to understand its intent: // unique for each object since generic types cannot be respected in id And I wonder if the issue I brought up is a valid one, and I can fix it by using TypeId.parse() here: https://github.com/SMILEY4/schema-kenerator/blob/b5c2d56ded43beaf47e4720af6758c3aaefc9201/schema-kenerator-serialization/src/main/kotlin/io/github/smiley4/schemakenerator/serialization/steps/KotlinxSerializationTypeProcessingStep.kt#L266


I just saw other issues about generic type... Not sure how to solve it..

SMILEY4 commented 4 days ago

Hi,

As a result, the TypeId doesn't match and fails to reference from schemaList.

As far as i know, the ids should usually still match. However, I dont know how the models and serializers are set up, so im also not completly sure if this issue is connected to the line with the comment. We may need to dig a bit deeper into this perhaps.

Regarding the comment on getUniqueId: I write a comment before that i think explains the issue quite well_ https://github.com/SMILEY4/schema-kenerator/issues/37#issuecomment-2416326579. So getUniqueId checks if a type with the same name has already been encountered. If no, the name can just be used as the id as is. If the type is already known, it might be the same type or it might be the same class but with a different type parameter. Since i didnt find a way to get this information kotlinx, i decided to treat the types as different types by default in this case. I hope that kinda answers the question.