SMILEY4 / ktor-swagger-ui

Kotlin Ktor plugin to generate OpenAPI and provide Swagger UI
Apache License 2.0
150 stars 25 forks source link

[Bug] Jackson sealed classes are no longer detected #73

Closed clragon closed 2 months ago

clragon commented 9 months ago

As of version 2.7.x, it seems that all my sealed classes annotated with Jackson are no longer detected.

Previously, the output for a given class was the following:

      "RemoteGithubProjectRequest-1" : {
        "type" : "object",
        "properties" : {
          "name" : {
            "type" : "string"
          },
          "owner" : {
            "type" : "string"
          },
          "repo" : {
            "type" : "string"
          },
          "type" : {
            "$ref" : "#/components/schemas/ProjectType"
          }
        }
      },
      "RemoteGithubProjectRequest-2" : {
        "$ref" : "#/components/schemas/RemoteGithubProjectRequest-1"
      },

(why every class is doubled I am unsure).

when upgrading to version 2.7.0 or later, I instead get:

      "ProjectRequest" : {
        "type" : "object"
      },

(which is the root of the sealed class).

The code behind this specific class is as follows:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "type"
)
@JsonSubTypes(
    JsonSubTypes.Type(value = RemoteGithubProjectRequest::class, name = "remote_github")
)
sealed interface ProjectRequest {
    val name: String
    val type: ProjectType
}

@JsonDeserialize(using = CaseInsensitiveEnumDeserializer::class)
@JsonSerialize(using = CaseInsensitiveEnumSerializer::class)
enum class ProjectType {
    REMOTE_GITHUB,
    GITHUB
}

data class RemoteGithubProjectRequest(
    override val name: String,
    val owner: String,
    val repo: String,
) : ProjectRequest {
    override val type = ProjectType.REMOTE_GITHUB
}

downgrading to version 2.6.0 restores the previous behaviour.

SMILEY4 commented 9 months ago

Hi, After just quickly looking into this, i think the issue is a result of a "JacksonModule"-addon being removed from the library that generates the json-schemas from classes to resolve https://github.com/SMILEY4/ktor-swagger-ui/issues/60. The problem was that fields starting with "is" were not included in schemas. "JacksonModule" was removed since it also seemed to remove these fields. Unfortunately, it was also responsible for resolving the inheritance/interfaces. I'll investigate further.

SMILEY4 commented 7 months ago

Hi, sorry for the very late response :/ I'm trying to find a proper future-proof solution but for now i only have a workaround - with the disadvantages described in my previous comment.

    EncodingData.DEFAULT_SCHEMA_GENERATOR = SchemaGenerator(
        EncodingData
            .schemaGeneratorConfigBuilder()
            .with(JacksonModule()) // add back JacksonModule
            .build()
    )

    install(SwaggerUI) { /*...*/ }
clragon commented 6 months ago

Thank you for your response. I've stopped using inheritance for my data classes (doesn't bode well) so I dont have this problem anymore. From my side, this issue is therefore done. Feel free to keep it open though.

SMILEY4 commented 2 months ago

closing this issue since it should be resolved with upcoming version 3