Feat: Add Spring Boot AutoConfiguration#260
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to add Spring Boot AutoConfiguration support so the library self-registers required beans when used in a Spring Boot app (per #61), reducing manual Jackson setup.
Changes:
- Adds optional Spring Boot AutoConfiguration dependencies (Boot 2.7.18) and Boot test support.
- Introduces
GithubClientAutoConfigurationand a correspondingApplicationContextRunnertest. - Adds an
AutoConfiguration.importsresource intended to register the auto-configuration.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
pom.xml |
Adds Spring Boot autoconfigure (+ processor) dependencies and Boot test dependency. |
src/main/java/com/spotify/github/v3/spring/GithubClientAutoConfiguration.java |
Adds a new Spring Boot auto-configuration class (currently wiring a GitHubClient bean). |
src/test/java/com/spotify/github/v3/spring/GithubClientAutoConfigurationTest.java |
Adds a Spring Boot context-runner test intended to validate module registration behavior. |
src/test/resources/META-INF/Spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports |
Adds an imports file intended for Boot’s auto-configuration discovery (currently under test resources and with incorrect casing). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| import com.spotify.github.v3.clients.GitHubClient; | ||
|
|
||
| @AutoConfiguration | ||
| @ConditionalOnClass(GitHubClient.class) | ||
| public class GithubClientAutoConfiguration { | ||
|
|
||
| @Bean | ||
| @ConditionalOnBean | ||
| public GitHubClient gitHubClient() { | ||
| return GitHubClient.create(null, null); |
There was a problem hiding this comment.
This auto-configuration doesn’t match the PR goal (registering a GithubApiModule/Jackson Module). It currently creates a GitHubClient bean using GitHubClient.create(null, null), which leaves required constructor inputs (e.g., baseUrl/token) null and can lead to runtime failures when the client is used. Consider changing this configuration to expose a GithubApiModule bean (and only if Jackson is present, and if no user-defined module bean already exists) rather than instantiating a client with null parameters.
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | |
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | |
| import org.springframework.context.annotation.Bean; | |
| import com.spotify.github.v3.clients.GitHubClient; | |
| @AutoConfiguration | |
| @ConditionalOnClass(GitHubClient.class) | |
| public class GithubClientAutoConfiguration { | |
| @Bean | |
| @ConditionalOnBean | |
| public GitHubClient gitHubClient() { | |
| return GitHubClient.create(null, null); | |
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | |
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | |
| import org.springframework.context.annotation.Bean; | |
| import com.fasterxml.jackson.databind.Module; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.spotify.github.v3.GithubApiModule; | |
| import com.spotify.github.v3.clients.GitHubClient; | |
| @AutoConfiguration | |
| @ConditionalOnClass({GitHubClient.class, ObjectMapper.class, Module.class}) | |
| public class GithubClientAutoConfiguration { | |
| @Bean | |
| @ConditionalOnMissingBean(Module.class) | |
| public Module githubApiModule() { | |
| return new GithubApiModule(); |
| @Bean | ||
| @ConditionalOnBean | ||
| public GitHubClient gitHubClient() { |
There was a problem hiding this comment.
@ConditionalOnBean is used without specifying any bean types/names. In Spring Boot this condition is effectively ambiguous and can behave unexpectedly (or not match at all). If the intent is to only enable the bean when a specific dependency is present (e.g., ObjectMapper), specify that explicitly (or use @ConditionalOnClass / @ConditionalOnMissingBean as appropriate).
|
|
||
| package com.spotify.github.v3.spring; | ||
|
|
||
| import org.junit.Test; |
There was a problem hiding this comment.
The project’s existing tests use JUnit Jupiter (org.junit.jupiter.api.Test), but this new test uses JUnit 4 (org.junit.Test). This can cause the test not to run (or require vintage support) and is inconsistent with the repo’s current test framework.
| import org.junit.Test; | |
| import org.junit.jupiter.api.Test; |
| public void testModuleIsRegistered(){ | ||
|
|
||
| this.contextRunner | ||
| .withUserConfiguration(JacksonConfig.class) | ||
| .run((context) -> { | ||
| assertThat(context).hasSingleBean(GithubApiModule.class); | ||
| }); |
There was a problem hiding this comment.
This test asserts that a GithubApiModule bean is registered by GithubClientAutoConfiguration, but the auto-configuration currently defines a GitHubClient bean instead. As written, this test will fail unless some other configuration is providing GithubApiModule; align the test assertions with the actual auto-configured bean(s), or update the auto-configuration to register GithubApiModule as described in the PR.
| @@ -0,0 +1 @@ | |||
| com.spotify.github.v3.spring.GithubClientAutoConfiguration No newline at end of file | |||
There was a problem hiding this comment.
Spring Boot discovers auto-configurations from META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in main resources. This file is currently under src/test/resources and uses META-INF/Spring (capital S), so it won’t be found in real applications (and may not be found on case-sensitive filesystems). Move it to src/main/resources/META-INF/spring/ with the correct casing to actually enable the auto-configuration.
| <artifactId>spring-boot-autoconfigure-processor</artifactId> | ||
| <version>2.7.18</version> | ||
| <optional>true</optional> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.springframework.boot</groupId> |
There was a problem hiding this comment.
spring-boot-autoconfigure-processor is an annotation processor; however, the maven-compiler-plugin config in this POM uses annotationProcessorPaths and only lists Immutables. If you expect Spring’s autoconfigure metadata to be generated, add the processor to annotationProcessorPaths (or otherwise ensure it’s on the annotation processor path); otherwise this dependency is likely unused.
| <artifactId>spring-boot-autoconfigure-processor</artifactId> | |
| <version>2.7.18</version> | |
| <optional>true</optional> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.springframework.boot</groupId> |
Description
This PR introduces Spring Boot AutoConfiguration to the library.
It automatically registers the
GithubApiModulebean when the library is used in a Spring Boot application that has Jackson on the classpath.Resolves
Closes #61
Changes
spring-boot-autoconfigure(v2.7.18 for Java 11 compatibility).GithubClientAutoConfigurationto conditionally register theGithubApiModulebean.META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.GithubClientAutoConfigurationTestusingApplicationContextRunnerto verify the behavior.Verification
mvn clean install -DskipTestslocally -> BUILD SUCCESS.mvn testlocally -> All tests passed.