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
Add Kotlin alternatives to Java samples #29499
Conversation
@p7nov Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@p7nov Thank you for signing the Contributor License Agreement! |
kotlinOptions.setAllWarningsAsErrors(true); | ||
kotlinOptions.setApiVersion("1.6"); | ||
kotlinOptions.setLanguageVersion("1.6"); | ||
kotlinOptions.setAllWarningsAsErrors(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty to switch this to false
because there are warnings that are meaningless in sample code (such as parameter is never used
in functions with empty bodies) and they break the build.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do this specifically for the docs project, leaving warnings as errors by convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returned this setting to true
; set to false
in build.gradle
of spring-boot-docs.
@WebMvcTest(UserVehicleController::class) | ||
class MyControllerTests( | ||
@Autowired val mvc: MockMvc, | ||
@MockBean val userVehicleService: UserVehicleService) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, there is no ParameterResolver for parameters annotated with MockBean (which, BTW annotates the backing field, and not the parameter). So, unless I missed something, this will fail at runtime. Instead, userVehicleService
should be a lateinit var
.
Sorry for just leaving this comment: I stumbled on this almost by accident and felt the need to signal the issue, but I applaud your huge effort to translate all those examples in Kotlin. 👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed this case in three examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I wasn't clear enough. The MockBean annotation is needed, but it needs to annotate the lateinit var:
@MockBean
lateinit var userVehicleService: UserVehicleService
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, fixed.
Thank you very much for this contribution. There is some related work that needs to happen before we can merge this. Our asciidoctor backend automatically removes imports, package statement and copyright headers but that doesn't work for Kotlin source code. I've created spring-io/spring-asciidoctor-backends#55 |
Thanks very much for this amazing PR @p7nov! I've merged it to I made a few changes to our asciidoctor backend to support Kotlin and to also automate the discovery of Kotlin source files. I've also tweaked a few things in this polish commit. I hope the updates are OK. The new docs should appear here once CI has completed a build. Thanks again! |
In this PR, I'm adding the Kotlin alternatives to Java samples in the Spring Boot reference. They are mostly translated with the IntelliJ IDEA built-in J2K converter and later adjusted manually for a more Kotlin-idiomatic look.
Feel free to change the new samples as you see best or ask me to do this.
Regards, Pavel
Kotlin Technical Writer