Skip to content

refactor: inject JsonMapper instead of ObjectMapper - #276

Open
adityamparikh wants to merge 1 commit into
apache:mainfrom
adityamparikh:refactor/inject-json-mapper
Open

adityamparikh wants to merge 1 commit into
apache:mainfrom
adityamparikh:refactor/inject-json-mapper

Conversation

@adityamparikh

Copy link
Copy Markdown
Contributor

Spring Boot 4's JacksonAutoConfiguration publishes format-specific Jackson 3 mapper beans:

Bean Registered when
JsonMapper jacksonJsonMapper always
XmlMapper xmlMapper jackson-dataformat-xml is on the classpath
CBORMapper cborMapper jackson-dataformat-cbor is on the classpath

None of them is @Primary, and all three extend ObjectMapper. Our constructors currently ask for ObjectMapper, which resolves only because JsonMapper is the sole candidate today. If either dataformat ever lands on the classpath, even transitively, every one of those injection points fails at startup with NoUniqueBeanDefinitionException.

Every mapper in this codebase is used for JSON, so this declares JsonMapper:

  • Main: CollectionService, SchemaService, JsonDocumentCreator, JsonResponseParser and the SolrConfig.jsonResponseParser bean method. Plus the SolrDocumentCreator Javadoc example.
  • Tests: @Autowired, @Mock and field types switch to JsonMapper, and new ObjectMapper() becomes JsonMapper.builder().build(), matching the existing tests that already build mappers that way.

Only types change; field and parameter names are unchanged. JsonUtils is left alone because #275 deletes it.

./gradlew build passes: 465 tests, 0 failures, 0 skipped.

🤖 Generated with Claude Code

Spring Boot 4's JacksonAutoConfiguration publishes format-specific mapper
beans: JsonMapper always, plus XmlMapper and CBORMapper when
jackson-dataformat-xml or jackson-dataformat-cbor is on the classpath. None
is @primary and all extend ObjectMapper, so injecting ObjectMapper only
resolves while JsonMapper is the sole candidate; adding either dataformat
would fail startup with NoUniqueBeanDefinitionException.

Every injection point and field here relies on JSON, so declare JsonMapper.
Tests construct JsonMapper.builder().build() in place of new ObjectMapper().

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Aditya Parikh <aditya.m.parikh@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant