Skip to content

springdoc does not respect @field:Schema(enumAsRef = true) on a kotlin data class property #3016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
damiankaplon opened this issue Jun 4, 2025 · 1 comment
Labels
question Further information is requested

Comments

@damiankaplon
Copy link

Good morning, evening!

Having

@RestController
class HelloController {

	@GetMapping("/hello")
	fun hello() = Response(status = Response.Status.SUCCESS)
}

data class Response(@field:Schema(enumAsRef = true) val status: Status) {
	enum class Status {
		SUCCESS,
		ERROR
	}
}

generates response schema ⬇ :

"schemas":{"Response":{"type":"object","properties":{"status":{"type":"string","enum":["SUCCESS","ERROR"]}}}}

I expected ⬇ :

"schemas":{"Response":{"type":"object","properties":{"status":{"$ref":"#/components/schemas/Status"}}},"Status":{"type":"string","enum":["SUCCESS","ERROR"]}}}

It can be achieved annotating an enum class with Schema(enumAsRef = true) but I'd prefer to annotate a data class property instead) 🙏🏼

Here is a minimal, reproducible example

Versions:

                         <groupId>org.springframework.boot</groupId>
		        <artifactId>spring-boot-starter-parent</artifactId>
		        <version>3.5.0</version>

			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
			<version>2.8.0</version>

			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-starter-common</artifactId>
			<version>2.8.0</version>

	                 <groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-maven-plugin</artifactId>
			<version>1.4</version>
@bnasslahsen
Copy link
Collaborator

@damiankaplon,

You declare it this way:

data class Response(val status: Status) {
	@Schema(enumAsRef = true)
	enum class Status {
		SUCCESS,
		ERROR
	}
}

There are no other workarounds!

@bnasslahsen bnasslahsen added the question Further information is requested label Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants