From 4d0fac5df0c5b12c86ee8174b1b03c0227e9b676 Mon Sep 17 00:00:00 2001 From: sdeleuze Date: Mon, 12 Feb 2018 14:53:45 +0100 Subject: [PATCH 1/2] Add jackson-module-kotlin dependency when appropriate This commit introduces a "json" facet designed to identify spring-boot-starter-json transitive dependency, and adds jackson-module-kotlin dependency if any dependency with that facet is included when Kotlin language is used. See gh-600 --- .../JacksonKotlinRequestPostProcessor.java | 50 ++++++++++++++++ .../src/main/resources/application.yml | 7 +++ ...acksonKotlinRequestPostProcessorTests.java | 60 +++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java create mode 100644 initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java diff --git a/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java b/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java new file mode 100644 index 0000000000..f0535ee748 --- /dev/null +++ b/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.service.extension; + +import io.spring.initializr.generator.ProjectRequest; +import io.spring.initializr.generator.ProjectRequestPostProcessor; +import io.spring.initializr.metadata.Dependency; +import io.spring.initializr.metadata.InitializrMetadata; + +import org.springframework.stereotype.Component; + +/** + * A {@link ProjectRequestPostProcessor} that automatically adds "jackson-module-kotlin" + * when Kotlin is used and a dependency has the "json" facet (meaning that it has + * "spring-boot-starter-json" transitive dependency). + * + * @author Sebastien Deleuze + */ +@Component +class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor { + + private final Dependency jacksonModuleKotlin; + + public JacksonKotlinRequestPostProcessor() { + this.jacksonModuleKotlin = Dependency.withId( + "jackson-module-kotlin", "com.fasterxml.jackson.module", "jackson-module-kotlin"); + } + + @Override + public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) { + if (request.getFacets().contains("json") && "kotlin".equals(request.getLanguage())) { + request.getResolvedDependencies().add(this.jacksonModuleKotlin); + } + } + +} diff --git a/initializr-service/src/main/resources/application.yml b/initializr-service/src/main/resources/application.yml index 5870ca99be..8360f3d12f 100644 --- a/initializr-service/src/main/resources/application.yml +++ b/initializr-service/src/main/resources/application.yml @@ -284,6 +284,7 @@ initializr: weight: 100 facets: - web + - json links: - rel: guide href: https://spring.io/guides/gs/rest-service/ @@ -301,9 +302,13 @@ initializr: versionRange: 2.0.0.M1 description: Reactive web development with Netty and Spring WebFlux weight: 90 + facets: + - json - name: Rest Repositories id: data-rest weight: 10 + facets: + - json description: Exposing Spring Data repositories over REST via spring-data-rest-webmvc links: - rel: guide @@ -353,6 +358,8 @@ initializr: - name: Jersey (JAX-RS) id: jersey description: RESTful Web Services framework with support of JAX-RS + facets: + - json versionRange: 1.2.0.RELEASE links: - rel: reference diff --git a/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java b/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java new file mode 100644 index 0000000000..f6741fb7b7 --- /dev/null +++ b/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java @@ -0,0 +1,60 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.service.extension; + +import io.spring.initializr.generator.ProjectRequest; +import io.spring.initializr.metadata.Dependency; +import org.junit.Test; + +/** + * Tests for {@link JacksonKotlinRequestPostProcessor}. + * + * @author Sebastien Deleuze + */ +public class JacksonKotlinRequestPostProcessorTests + extends AbstractRequestPostProcessorTests { + + @Test + public void jacksonModuleKotlinIsAdded() { + ProjectRequest request = createProjectRequest("webflux"); + request.setBootVersion("2.0.0.M2"); + request.setLanguage("kotlin"); + Dependency jacksonKotlinModuleTest = Dependency.withId( + "jackson-module-kotlin", "com.fasterxml.jackson.module", "jackson-module-kotlin"); + generateMavenPom(request) + .hasDependency(jacksonKotlinModuleTest) + .hasDependenciesCount(6); + } + + @Test + public void jacksonModuleKotlinIsNotAddedWithoutKotlin() { + ProjectRequest request = createProjectRequest("webflux"); + request.setBootVersion("2.0.0.M2"); + generateMavenPom(request) + .hasDependenciesCount(3); + } + + @Test + public void jacksonModuleKotlinIsNotAddedWithoutJsonFacet() { + ProjectRequest request = createProjectRequest("batch"); + request.setBootVersion("2.0.0.M2"); + request.setLanguage("kotlin"); + generateMavenPom(request) + .hasDependenciesCount(5); + } + +} From 15fb6eb1c21b14f3c89fc856e52f664a7ed52691 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 12 Feb 2018 15:14:25 +0100 Subject: [PATCH 2/2] Polish "Add jackson-module-kotlin dependency when appropriate" Closes gh-600 --- .../JacksonKotlinRequestPostProcessor.java | 13 ++++----- ...acksonKotlinRequestPostProcessorTests.java | 27 +++++++++++++++---- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java b/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java index f0535ee748..e6ff72b98d 100644 --- a/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java +++ b/initializr-service/src/main/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessor.java @@ -25,8 +25,7 @@ /** * A {@link ProjectRequestPostProcessor} that automatically adds "jackson-module-kotlin" - * when Kotlin is used and a dependency has the "json" facet (meaning that it has - * "spring-boot-starter-json" transitive dependency). + * when Kotlin is used and a dependency has the "json" facet. * * @author Sebastien Deleuze */ @@ -36,13 +35,15 @@ class JacksonKotlinRequestPostProcessor implements ProjectRequestPostProcessor { private final Dependency jacksonModuleKotlin; public JacksonKotlinRequestPostProcessor() { - this.jacksonModuleKotlin = Dependency.withId( - "jackson-module-kotlin", "com.fasterxml.jackson.module", "jackson-module-kotlin"); + this.jacksonModuleKotlin = Dependency.withId("jackson-module-kotlin", + "com.fasterxml.jackson.module", "jackson-module-kotlin"); } @Override - public void postProcessAfterResolution(ProjectRequest request, InitializrMetadata metadata) { - if (request.getFacets().contains("json") && "kotlin".equals(request.getLanguage())) { + public void postProcessAfterResolution(ProjectRequest request, + InitializrMetadata metadata) { + if (request.getFacets().contains("json") + && "kotlin".equals(request.getLanguage())) { request.getResolvedDependencies().add(this.jacksonModuleKotlin); } } diff --git a/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java b/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java index f6741fb7b7..042c996907 100644 --- a/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java +++ b/initializr-service/src/test/java/io/spring/initializr/service/extension/JacksonKotlinRequestPostProcessorTests.java @@ -24,19 +24,29 @@ * Tests for {@link JacksonKotlinRequestPostProcessor}. * * @author Sebastien Deleuze + * @author Stephane Nicoll */ public class JacksonKotlinRequestPostProcessorTests extends AbstractRequestPostProcessorTests { + static final Dependency JACKSON_KOTLIN = Dependency.withId("jackson-module-kotlin", + "com.fasterxml.jackson.module", "jackson-module-kotlin"); + + static final Dependency REACTOR_TEST = Dependency.create( + "io.projectreactor", "reactor-test", null, Dependency.SCOPE_TEST); + @Test public void jacksonModuleKotlinIsAdded() { ProjectRequest request = createProjectRequest("webflux"); request.setBootVersion("2.0.0.M2"); request.setLanguage("kotlin"); - Dependency jacksonKotlinModuleTest = Dependency.withId( - "jackson-module-kotlin", "com.fasterxml.jackson.module", "jackson-module-kotlin"); generateMavenPom(request) - .hasDependency(jacksonKotlinModuleTest) + .hasSpringBootStarterDependency("webflux") + .hasDependency(JACKSON_KOTLIN) + .hasSpringBootStarterTest() + .hasDependency(REACTOR_TEST) + .hasDependency("org.jetbrains.kotlin", "kotlin-reflect") + .hasDependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk8") .hasDependenciesCount(6); } @@ -45,16 +55,23 @@ public void jacksonModuleKotlinIsNotAddedWithoutKotlin() { ProjectRequest request = createProjectRequest("webflux"); request.setBootVersion("2.0.0.M2"); generateMavenPom(request) + .hasSpringBootStarterDependency("webflux") + .hasSpringBootStarterTest() + .hasDependency(REACTOR_TEST) .hasDependenciesCount(3); } @Test public void jacksonModuleKotlinIsNotAddedWithoutJsonFacet() { - ProjectRequest request = createProjectRequest("batch"); + ProjectRequest request = createProjectRequest("actuator"); request.setBootVersion("2.0.0.M2"); request.setLanguage("kotlin"); generateMavenPom(request) - .hasDependenciesCount(5); + .hasSpringBootStarterDependency("actuator") + .hasSpringBootStarterTest() + .hasDependency("org.jetbrains.kotlin", "kotlin-reflect") + .hasDependency("org.jetbrains.kotlin", "kotlin-stdlib-jdk8") + .hasDependenciesCount(4); } }