Skip to content

Schema annotation not working in Kotlin super class (has workaround) #3010

Closed
@janjouketjalsma

Description

@janjouketjalsma

Describe the bug
When using Kotlin, annotating a field with @Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) does not work on the super class. It does however work on the "main" entity. This inconsistency could be addressed.

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using? 3.5
  • What modules and versions of springdoc-openapi are you using? 2.8.8
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
    Actual:
"required": [
          "bar"
          "id"
        ]

Expected:

"required": [
          "id"
        ]
  • Provide with a sample code (HelloController) or Test that reproduces the problem
// Any controller doing POST using inheritance in Kotlin. Example:

@MappedSuperclass
class AbstractEntity(
	@Column(name = "created_at")
	@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) // <-- ignored
	val bar: String = "barString",
)

@Entity
class ConcreteEntity(
	@Id
	var id: Long = 1,

	@Column(name = "updated_at")
	@Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) // <-- works
	val foo: String = "fooString",
): AbstractEntity()

Expected behavior

  • Fields annotated with @Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED) are not required in the generated OpenApi spec.

Workaround:
Add get: before the annotation in the super class to make the annotation explicitly attached to the getter. @get:Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions