You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ds...@apache.org on 2025/04/27 13:06:26 UTC

(solr) branch main updated: SOLR-17743: V2 API: use correct content-type (#3326)

This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/main by this push:
     new 0bc55d5bc4c SOLR-17743: V2 API: use correct content-type (#3326)
0bc55d5bc4c is described below

commit 0bc55d5bc4c3ba8fdfec749957cf33cc6076ae57
Author: David Smiley <ds...@apache.org>
AuthorDate: Sun Apr 27 22:06:21 2025 +0900

    SOLR-17743: V2 API: use correct content-type (#3326)
    
    V2 API: Raw uploads should use the correct content-type: application/octet-stream not JSON.
---
 .../solr/client/solrj/JacksonContentWriter.java    |  9 +++---
 .../solrj/src/resources/java-template/api.mustache | 36 ++++++++++------------
 2 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/JacksonContentWriter.java b/solr/solrj/src/java/org/apache/solr/client/solrj/JacksonContentWriter.java
index 8ba0c29fa6b..6ae00fb67e5 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/JacksonContentWriter.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/JacksonContentWriter.java
@@ -22,15 +22,16 @@ import java.io.IOException;
 import java.io.OutputStream;
 import org.apache.solr.client.solrj.request.RequestWriter;
 
+/**
+ * @lucene.internal
+ */
 public class JacksonContentWriter implements RequestWriter.ContentWriter {
 
   public static final ObjectMapper DEFAULT_MAPPER = new ObjectMapper();
 
   private final Object toWrite;
-  private final String contentType;
 
-  public JacksonContentWriter(String contentType, Object toWrite) {
-    this.contentType = contentType;
+  public JacksonContentWriter(Object toWrite) {
     this.toWrite = toWrite;
   }
 
@@ -41,6 +42,6 @@ public class JacksonContentWriter implements RequestWriter.ContentWriter {
 
   @Override
   public String getContentType() {
-    return contentType;
+    return "application/json; charset=UTF-8";
   }
 }
diff --git a/solr/solrj/src/resources/java-template/api.mustache b/solr/solrj/src/resources/java-template/api.mustache
index c374e2d879d..4b3163ffd87 100644
--- a/solr/solrj/src/resources/java-template/api.mustache
+++ b/solr/solrj/src/resources/java-template/api.mustache
@@ -130,7 +130,6 @@ public class {{classname}} {
                 {{#isBodyParam}}
                 {{#vendorExtensions.x-genericEntity}}
                     this.requestBody = requestBody;
-                    addHeader("Content-type", "application/json");
                 {{/vendorExtensions.x-genericEntity}}
                 {{/isBodyParam}}
                 {{^isBodyParam}}
@@ -150,7 +149,6 @@ public class {{classname}} {
                     this.requestBody = new {{{dataType}}}();
                 {{/isArray}}
                 {{/isMap}}
-                    addHeader("Content-type", "application/json");
                 {{/vendorExtensions.x-genericEntity}}
                 {{/bodyParam}}
             }
@@ -202,26 +200,24 @@ public class {{classname}} {
 
             @Override
             @SuppressWarnings("unchecked")
-            public RequestWriter.ContentWriter getContentWriter(String expectedType) {
+            public RequestWriter.ContentWriter getContentWriter(String _expectedTypeIGNORE) {
+                // v2 only supports JSON request bodies, so we ignore this type coming from the RequestWriter
                 {{#vendorExtensions.x-genericEntity}}
-                if (requestBody instanceof String) {
-                    return new RequestWriter.StringPayloadContentWriter((String) requestBody, expectedType);
-                } else if (requestBody instanceof InputStream) {
-                    final var inputStream = (InputStream) requestBody;
-                    return new RequestWriter.ContentWriter() {
-                        @Override
-                        public void write(OutputStream os) throws IOException {
-                            inputStream.transferTo(os);
-                        }
+                return new RequestWriter.ContentWriter() {
+                  @Override
+                  public void write(OutputStream os) throws IOException {
+                    ((InputStream) requestBody).transferTo(os);
+                  }
 
-                        @Override
-                        public String getContentType() {
-                            return expectedType;
-                        }
-                    };
-                }
+                  @Override
+                  public String getContentType() {
+                    return "application/octet-stream";
+                  }
+                };
+                {{/vendorExtensions.x-genericEntity}}
+                {{^vendorExtensions.x-genericEntity}}
+                return new JacksonContentWriter(requestBody);
                 {{/vendorExtensions.x-genericEntity}}
-                return new JacksonContentWriter(expectedType, requestBody);
             }
             {{/bodyParam}}
 
@@ -235,7 +231,7 @@ public class {{classname}} {
               final ModifiableSolrParams params = new ModifiableSolrParams();
               {{#queryParams}}
               if ({{paramName}} != null) {
-                  {{#isArray}}{{paramName}}.stream().forEach(v -> params.add("{{baseName}}", v{{^items.isString}}.toString(){{/items.isString}}));{{/isArray}}
+                  {{#isArray}}{{paramName}}.forEach(v -> params.add("{{baseName}}", v{{^items.isString}}.toString(){{/items.isString}}));{{/isArray}}
                   {{^isArray}}params.add("{{baseName}}", {{paramName}}{{^isString}}.toString(){{/isString}});{{/isArray}}
               }
               {{/queryParams}}