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

Add Kotlin alternatives to Java samples #29499

Closed
wants to merge 30 commits into from

Conversation

p7nov
Copy link
Contributor

@p7nov p7nov commented Jan 20, 2022

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

@pivotal-cla
Copy link

@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.

@pivotal-cla
Copy link

@p7nov Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 20, 2022
kotlinOptions.setAllWarningsAsErrors(true);
kotlinOptions.setApiVersion("1.6");
kotlinOptions.setLanguageVersion("1.6");
kotlinOptions.setAllWarningsAsErrors(false);
Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@p7nov p7nov Jan 27, 2022

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.

@philwebb philwebb added the for: team-meeting An issue we'd like to discuss as a team to make progress label Jan 21, 2022
@snicoll snicoll changed the title docs: Add Kotlin alternatives to Java samples Add Kotlin alternatives to Java samples Jan 22, 2022
@WebMvcTest(UserVehicleController::class)
class MyControllerTests(
@Autowired val mvc: MockMvc,
@MockBean val userVehicleService: UserVehicleService) {
Copy link
Contributor

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. 👏

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, fixed.

@snicoll
Copy link
Member

snicoll commented Feb 1, 2022

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

@snicoll snicoll added the status: blocked An issue that's blocked on an external project change label Feb 1, 2022
@philwebb philwebb added type: enhancement A general enhancement type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged status: blocked An issue that's blocked on an external project change for: team-meeting An issue we'd like to discuss as a team to make progress type: enhancement A general enhancement labels Feb 2, 2022
@philwebb philwebb added this to the 2.7.x milestone Feb 2, 2022
@philwebb philwebb closed this in 4ab858a Feb 5, 2022
@philwebb
Copy link
Member

philwebb commented Feb 5, 2022

Thanks very much for this amazing PR @p7nov! I've merged it to 2.7.x and main.

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!

@philwebb philwebb modified the milestones: 2.7.x, 2.7.0-M2 Feb 5, 2022
philwebb added a commit that referenced this pull request Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants