Skip to content

Commit ca80599

Browse files
adityamparikhclaude
andcommitted
refactor(config): inject Spring's ObjectMapper into JsonResponseParser
Replace the static new ObjectMapper() with Spring's auto-configured ObjectMapper bean injected via constructor. Use MediaType.APPLICATION_JSON_VALUE for the content type constant instead of a raw string literal. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: adityamparikh <aditya.m.parikh@gmail.com>
1 parent a387b19 commit ca80599

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/main/java/org/apache/solr/mcp/server/config/JsonResponseParser.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.solr.common.SolrException;
3030
import org.apache.solr.common.util.NamedList;
3131
import org.apache.solr.common.util.SimpleOrderedMap;
32+
import org.springframework.http.MediaType;
3233

3334
/**
3435
* SolrJ {@link ResponseParser} that requests JSON wire format ({@code wt=json})
@@ -67,7 +68,11 @@
6768
*/
6869
class JsonResponseParser extends ResponseParser {
6970

70-
private static final ObjectMapper MAPPER = new ObjectMapper();
71+
private final ObjectMapper mapper;
72+
73+
JsonResponseParser(ObjectMapper mapper) {
74+
this.mapper = mapper;
75+
}
7176

7277
@Override
7378
public String getWriterType() {
@@ -76,13 +81,13 @@ public String getWriterType() {
7681

7782
@Override
7883
public String getContentType() {
79-
return "application/json; charset=UTF-8";
84+
return MediaType.APPLICATION_JSON_VALUE;
8085
}
8186

8287
@Override
8388
public NamedList<Object> processResponse(InputStream body, String encoding) {
8489
try {
85-
return toNamedList(MAPPER.readTree(body));
90+
return toNamedList(mapper.readTree(body));
8691
} catch (IOException e) {
8792
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed to parse Solr JSON response", e);
8893
}
@@ -91,7 +96,7 @@ public NamedList<Object> processResponse(InputStream body, String encoding) {
9196
@Override
9297
public NamedList<Object> processResponse(Reader reader) {
9398
try {
94-
return toNamedList(MAPPER.readTree(reader));
99+
return toNamedList(mapper.readTree(reader));
95100
} catch (IOException e) {
96101
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed to parse Solr JSON response", e);
97102
}

src/main/java/org/apache/solr/mcp/server/config/SolrConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.solr.mcp.server.config;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
1920
import java.util.concurrent.TimeUnit;
2021
import org.apache.solr.client.solrj.SolrClient;
2122
import org.apache.solr.client.solrj.impl.Http2SolrClient;
@@ -168,8 +169,8 @@ public class SolrConfig {
168169
* @see SolrConfigurationProperties#url()
169170
*/
170171
@Bean
171-
JsonResponseParser jsonResponseParser() {
172-
return new JsonResponseParser();
172+
JsonResponseParser jsonResponseParser(ObjectMapper objectMapper) {
173+
return new JsonResponseParser(objectMapper);
173174
}
174175

175176
@Bean

src/test/java/org/apache/solr/mcp/server/config/SolrConfigTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21+
import com.fasterxml.jackson.databind.ObjectMapper;
2122
import org.apache.solr.client.solrj.SolrClient;
2223
import org.apache.solr.client.solrj.impl.Http2SolrClient;
2324
import org.apache.solr.mcp.server.TestcontainersConfiguration;
@@ -81,7 +82,7 @@ void testUrlNormalization(String inputUrl, String expectedUrl) {
8182
SolrConfig solrConfig = new SolrConfig();
8283

8384
// Test URL normalization
84-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser());
85+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
8586
assertNotNull(client);
8687

8788
var httpClient = assertInstanceOf(Http2SolrClient.class, client);
@@ -101,7 +102,7 @@ void testUrlWithoutTrailingSlash() {
101102
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983");
102103
SolrConfig solrConfig = new SolrConfig();
103104

104-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser());
105+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
105106
Http2SolrClient httpClient = (Http2SolrClient) client;
106107

107108
// Should add trailing slash and solr path
@@ -120,7 +121,7 @@ void testUrlWithTrailingSlashButNoSolrPath() {
120121
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/");
121122
SolrConfig solrConfig = new SolrConfig();
122123

123-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser());
124+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
124125
Http2SolrClient httpClient = (Http2SolrClient) client;
125126

126127
// Should add solr path to existing trailing slash
@@ -139,7 +140,7 @@ void testUrlWithSolrPathButNoTrailingSlash() {
139140
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr");
140141
SolrConfig solrConfig = new SolrConfig();
141142

142-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser());
143+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
143144
Http2SolrClient httpClient = (Http2SolrClient) client;
144145

145146
// Should add trailing slash
@@ -158,7 +159,7 @@ void testUrlAlreadyProperlyFormatted() {
158159
SolrConfigurationProperties testProperties = new SolrConfigurationProperties("http://localhost:8983/solr/");
159160
SolrConfig solrConfig = new SolrConfig();
160161

161-
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser());
162+
SolrClient client = solrConfig.solrClient(testProperties, new JsonResponseParser(new ObjectMapper()));
162163
Http2SolrClient httpClient = (Http2SolrClient) client;
163164

164165
// Should remain unchanged

0 commit comments

Comments
 (0)