SpringDoc-儿童课程没有添加到Swagger Spec Schema

发布于 2025-02-03 13:10:04 字数 2391 浏览 4 评论 0原文

我正在使用Spring-boot v2.6.7和Springdoc v1.6.8。以下是对象建模。我添加了一个端点,该端点将课程作为响应返回。我希望Swagger Spec会生成课程类型,而英语的孩子类型也有一个 itemport 字段字段,这是CourseItem的超级文字,并且有一个子类型,其中包括文件夹,课程,课程,课程,课程和测试。我希望Swagger规格包括所有子类型的适当架构定义。

package com.example.springdocdemo.domain

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import io.swagger.v3.oas.annotations.media.Schema
import java.util.UUID

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(
    value = [
        JsonSubTypes.Type(value = EnglishCourse::class, name = "EnglishCourse")
    ]
)
abstract class Course {
    abstract val id: UUID
    abstract val name: String
    abstract val code: String
}

class EnglishCourse(
    val items: List<CourseItem>,
    override val id: UUID,
    override val code: String,
    override val name: String,
) : Course()

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(
    value = [
        JsonSubTypes.Type(value = Folder::class, name = "FOLDER"),
        JsonSubTypes.Type(value = Lesson::class, name = "LESSON"),
        JsonSubTypes.Type(value = Test::class, name = "TEST")
    ]
)
@Schema(discriminatorProperty = "type", allOf = [Folder::class, Lesson::class, Test::class])
interface CourseItem {
    val type: CourseItemType
    val id: UUID
}

@Schema
class Folder(
    val items: List<CourseItem>,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem {
    fun items() = println(items)
}

class Lesson(
    val name: String,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem

class Test(
    val name: String,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem

enum class CourseItemType {
    Folder, Lesson, Test
}

下面是文件夹类型的架构定义,

"Folder": {
    "required": [
      "id",
      "type"
    ],
    "type": "object",
    "allOf": [
      {
        "$ref": "#/components/schemas/CourseItem"
      }
    ]
  },

它不包含字段ID和项目。有什么解决方案吗?该代码可以在此处找到: https://github.com/bhaveshssppringdoc-springdoc-sissue-springdoc-sissue

I'm using spring-boot v2.6.7 and springdoc v1.6.8. Below is the object modeling. I have added one endpoint which returns the Course as the response. I am expecting swagger spec to generate the types for the Course and the Child type which is EnglishCourse also there's an items field on EnglishCourse which is supertype of CourseItem and there are subtypes for it which are Folder, Lesson, and Test. I expect swagger spec to include a proper schema definition for all the subtypes.

package com.example.springdocdemo.domain

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import io.swagger.v3.oas.annotations.media.Schema
import java.util.UUID

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(
    value = [
        JsonSubTypes.Type(value = EnglishCourse::class, name = "EnglishCourse")
    ]
)
abstract class Course {
    abstract val id: UUID
    abstract val name: String
    abstract val code: String
}

class EnglishCourse(
    val items: List<CourseItem>,
    override val id: UUID,
    override val code: String,
    override val name: String,
) : Course()

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(
    value = [
        JsonSubTypes.Type(value = Folder::class, name = "FOLDER"),
        JsonSubTypes.Type(value = Lesson::class, name = "LESSON"),
        JsonSubTypes.Type(value = Test::class, name = "TEST")
    ]
)
@Schema(discriminatorProperty = "type", allOf = [Folder::class, Lesson::class, Test::class])
interface CourseItem {
    val type: CourseItemType
    val id: UUID
}

@Schema
class Folder(
    val items: List<CourseItem>,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem {
    fun items() = println(items)
}

class Lesson(
    val name: String,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem

class Test(
    val name: String,
    override val id: UUID,
    override val type: CourseItemType,
) : CourseItem

enum class CourseItemType {
    Folder, Lesson, Test
}

and the below is the schema definition for the Folder type

"Folder": {
    "required": [
      "id",
      "type"
    ],
    "type": "object",
    "allOf": [
      {
        "$ref": "#/components/schemas/CourseItem"
      }
    ]
  },

It doesn't include fields id and items. Is there any solution for this? The code can be found here: https://github.com/BhaveshSuvalaka/springdoc-issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2025-02-10 13:10:05

也许尝试

@Schema(subTypes = {EnglishCourse.class})

maybe try

@Schema(subTypes = {EnglishCourse.class})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文