Skip to content
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

Schema.RequiredMode.Required not working for spring mvc and kotlin #2360

Closed
olbpetersson opened this issue Aug 25, 2023 · 3 comments
Closed
Labels
invalid This doesn't seem right

Comments

@olbpetersson
Copy link
Contributor

olbpetersson commented Aug 25, 2023

Describe the bug

When building the same case as the springdoc-kotlin-test module does here in a spring boot 3 webMVC app, the openAPI-spec ends up with all the fields being reported as required.

To Reproduce
I've copied the resources from the test referenced above into an example app with a (failing) test which can be found here:
https://github.com/olbpetersson/springdoc-webmvc-test/blob/main/src/test/kotlin/com/example/demo/DemoApplicationTests.kt

and can be executed via ./gradlew bootRun & ./gradlew test

Expected behavior
The fields annotated with @Schema.RequiredMode.NOT_REQUIRED should not end up in the required list. The openAPI-spec should be the same as the one in app9.json referenced above

Screenshots

Additional context

@bnasslahsen
Copy link
Collaborator

@olbpetersson,

Tests are following the test cases described here:

If you have @Schema.RequiredMode.NOT_REQUIRED explicitly defined, you wil not end up with required.

@bnasslahsen bnasslahsen added the invalid This doesn't seem right label Oct 15, 2023
@olbpetersson
Copy link
Contributor Author

olbpetersson commented Oct 27, 2023

@bnasslahsen what you are writing is what I would expect, but when I run the test code that I provided that doesn't seem to be the case. As you can see in my test class, I am using @field:Schema.RequiredMod.NOT_REQUIRED on e.g.

	@field:Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
	val noRequiredNoNullableNoDefault2: String,

but it still shows up as required in the schema (see my test)
image
.

There seems to be a discrepancy between webflux and webmvc

@bnasslahsen bnasslahsen reopened this Oct 27, 2023
@egrimstad
Copy link

egrimstad commented Nov 15, 2023

Hi. Just hit this problem when migrating to springdoc from springfox, on Kotlin fields that are not nullable but have a default value:

@field:Schema(requiredMode = Schema.RequiredMode.NOT_REQUIRED)
val foo: String = "default"

This still gets marked as required. Are there any workarounds?

EDIT:
Okay, so I did some more debugging, and figured out that one property that was not set in your reproduction, @olbpetersson, was spring.main.lazy-initialization=true. I enabled that property and now your test runs green. I can't really say why that is the case though. Maybe @bnasslahsen has a good explanation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants