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

ulyssesyou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 0be523f  [KYUUBI #1135] [SUB-TASK][KPIP-1] Register default 
ObjectMapper instead of manually serialize/deserialize JSON input argument.
0be523f is described below

commit 0be523f6fe16474332cdbd11a31d37c8826f5bf1
Author: Fu Chen <[email protected]>
AuthorDate: Thu Sep 23 13:39:39 2021 +0800

    [KYUUBI #1135] [SUB-TASK][KPIP-1] Register default ObjectMapper instead of 
manually serialize/deserialize JSON input argument.
    
    <!--
    Thanks for sending a pull request!
    
    Here are some tips for you:
      1. If this is your first time, please read our contributor guidelines: 
https://kyuubi.readthedocs.io/en/latest/community/contributions.html
      2. If the PR is related to an issue in 
https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your 
PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
      3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][KYUUBI #XXXX] Your PR title ...'.
    -->
    
    ### _Why are the changes needed?_
    <!--
    Please clarify why the changes are needed. For instance,
      1. If you add a feature, you can talk about the use case of it.
      2. If you fix a bug, you can clarify why it is a bug.
    -->
    
    Register default `ObjectMapper` instead of manually serialize/deserialize 
JSON input argument.
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #1135 from cfmcgrady/objectmapper.
    
    Closes #1135
    
    580707cb [Fu Chen] fix style
    48da4fe4 [Fu Chen] refactor unit test
    067f11be [Fu Chen] fix style
    9462d936 [Fu Chen] object mapper
    
    Authored-by: Fu Chen <[email protected]>
    Signed-off-by: ulysses-you <[email protected]>
---
 .../server/api/KyuubiScalaObjectMapper.scala       | 29 ++++++++++++++++++++++
 .../scala/org/apache/kyuubi/server/api/api.scala   |  3 +--
 .../kyuubi/server/api/v1/SessionsResource.scala    | 15 ++++-------
 .../kyuubi/server/RestFrontendServiceSuite.scala   | 10 +++++---
 .../server/api/v1/SessionsResourceSuite.scala      | 21 +++++-----------
 5 files changed, 48 insertions(+), 30 deletions(-)

diff --git 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/KyuubiScalaObjectMapper.scala
 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/KyuubiScalaObjectMapper.scala
new file mode 100644
index 0000000..776c35b
--- /dev/null
+++ 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/KyuubiScalaObjectMapper.scala
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.kyuubi.server.api
+
+import javax.ws.rs.ext.ContextResolver
+
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+
+class KyuubiScalaObjectMapper extends ContextResolver[ObjectMapper] {
+  private val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
+
+  override def getContext(aClass: Class[_]): ObjectMapper = mapper
+}
diff --git 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala
index 862163c..83068e3 100644
--- a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala
+++ b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/api.scala
@@ -62,12 +62,11 @@ private[server] object ApiUtils {
       "org.apache.kyuubi.server.api.v1")
     servlet.setInitParameter(
       ServerProperties.PROVIDER_CLASSNAMES,
-      "org.glassfish.jersey.jackson.JacksonFeature")
+      classOf[KyuubiScalaObjectMapper].getName)
     val handler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS)
     BackendServiceProvider.setBackendService(handler, backendService)
     handler.setContextPath("/api")
     handler.addServlet(servlet, "/*")
     handler
   }
-
 }
diff --git 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/SessionsResource.scala
 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/SessionsResource.scala
index 2b7830e..e4531d9 100644
--- 
a/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/SessionsResource.scala
+++ 
b/kyuubi-server/src/main/scala/org/apache/kyuubi/server/api/v1/SessionsResource.scala
@@ -17,11 +17,10 @@
 
 package org.apache.kyuubi.server.api.v1
 
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.fasterxml.jackson.module.scala.DefaultScalaModule
 import java.util.UUID
 import javax.ws.rs.{Consumes, DELETE, GET, Path, PathParam, POST, Produces}
 import javax.ws.rs.core.{MediaType, Response}
+
 import org.apache.hive.service.rpc.thrift.TProtocolVersion
 
 import org.apache.kyuubi.cli.HandleIdentifier
@@ -31,25 +30,21 @@ import org.apache.kyuubi.session.SessionHandle
 @Produces(Array(MediaType.APPLICATION_JSON))
 private[v1] class SessionsResource extends ApiRequestContext {
 
-  private val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
-
   @GET
   @Path("count")
-  def sessionCount(): String = {
-    
mapper.writeValueAsString(SessionOpenCount(backendService.sessionManager.getOpenSessionCount))
+  def sessionCount(): SessionOpenCount = {
+    SessionOpenCount(backendService.sessionManager.getOpenSessionCount)
   }
 
   @POST
   @Consumes(Array(MediaType.APPLICATION_JSON))
-  def openSession(req : String): String = {
-    val request = mapper.readValue(req, classOf[SessionOpenRequest])
-    val sessionHandle = backendService.openSession(
+  def openSession(request: SessionOpenRequest): SessionHandle = {
+    backendService.openSession(
       TProtocolVersion.findByValue(request.protocolVersion),
       request.user,
       request.password,
       request.ipAddr,
       request.configs)
-    mapper.writeValueAsString(sessionHandle)
   }
 
   @DELETE
diff --git 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
index d7a2d7f..287e417 100644
--- 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
+++ 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/RestFrontendServiceSuite.scala
@@ -20,8 +20,7 @@ package org.apache.kyuubi.server
 import java.util.Locale
 import javax.ws.rs.core.Application
 
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import org.glassfish.jersey.client.ClientConfig
 import org.glassfish.jersey.server.ResourceConfig
 import org.glassfish.jersey.test.{JerseyTest, TestProperties}
 import org.glassfish.jersey.test.jetty.JettyTestContainerFactory
@@ -33,6 +32,7 @@ import scala.io.Source
 import org.apache.kyuubi.KyuubiFunSuite
 import org.apache.kyuubi.config.KyuubiConf
 import 
org.apache.kyuubi.server.RestFrontendServiceSuite.{withKyuubiRestServer, 
TEST_SERVER_PORT}
+import org.apache.kyuubi.server.api.KyuubiScalaObjectMapper
 import org.apache.kyuubi.service.NoopServer
 import org.apache.kyuubi.service.ServiceState._
 
@@ -86,7 +86,6 @@ object RestFrontendServiceSuite {
     override val frontendService = new RestFrontendService(backendService)
   }
 
-  val OBJECT_MAPPER = new ObjectMapper().registerModule(DefaultScalaModule)
   val TEST_SERVER_PORT = KyuubiConf().get(KyuubiConf.FRONTEND_REST_BIND_PORT)
 
   def withKyuubiRestServer(f: (RestFrontendService, String, Int) => Unit): 
Unit = {
@@ -117,6 +116,11 @@ class RestApiBaseSuite extends JerseyTest {
     new ResourceConfig(getClass)
   }
 
+  override def configureClient(config: ClientConfig): Unit = {
+    super.configureClient(config)
+    config.register(classOf[KyuubiScalaObjectMapper])
+  }
+
   override def getTestContainerFactory: TestContainerFactory = new 
JettyTestContainerFactory
 
 }
diff --git 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
index d60d8df..4f2f0f6 100644
--- 
a/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
+++ 
b/kyuubi-server/src/test/scala/org/apache/kyuubi/server/api/v1/SessionsResourceSuite.scala
@@ -24,7 +24,6 @@ import org.junit.Test
 
 import org.apache.kyuubi.server.RestApiBaseSuite
 import org.apache.kyuubi.server.RestFrontendServiceSuite
-import org.apache.kyuubi.server.RestFrontendServiceSuite.OBJECT_MAPPER
 import org.apache.kyuubi.session.SessionHandle
 
 class SessionsResourceSuite extends RestApiBaseSuite {
@@ -34,26 +33,22 @@ class SessionsResourceSuite extends RestApiBaseSuite {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
-    val requestObjStr = OBJECT_MAPPER.writeValueAsString(requestObj)
-
     RestFrontendServiceSuite.withKyuubiRestServer {
       (_, _, _) =>
         var response = target(s"api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
-          .post(Entity.entity(requestObjStr, MediaType.APPLICATION_JSON_TYPE))
+          .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
         assert(200 == response.getStatus)
 
-        val sessionHandle = OBJECT_MAPPER.readValue(
-          response.readEntity(classOf[String]), classOf[SessionHandle])
+        val sessionHandle = response.readEntity(classOf[SessionHandle])
 
         assert(sessionHandle.protocol.getValue == 1)
         assert(sessionHandle.identifier != null)
 
         // verify the open session count
         response = target("api/v1/sessions/count").request().get()
-        val openedSessionCount = OBJECT_MAPPER.readValue(
-          response.readEntity(classOf[String]), classOf[SessionOpenCount])
+        val openedSessionCount = response.readEntity(classOf[SessionOpenCount])
         assert(openedSessionCount.openSessionCount == 1)
     }
   }
@@ -63,18 +58,15 @@ class SessionsResourceSuite extends RestApiBaseSuite {
     val requestObj = SessionOpenRequest(
       1, "admin", "123456", "localhost", Map("testConfig" -> "testValue"))
 
-    val requestObjStr = OBJECT_MAPPER.writeValueAsString(requestObj)
-
     RestFrontendServiceSuite.withKyuubiRestServer {
       (_, _, _) =>
         var response = target(s"api/v1/sessions")
           .request(MediaType.APPLICATION_JSON_TYPE)
-          .post(Entity.entity(requestObjStr, MediaType.APPLICATION_JSON_TYPE))
+          .post(Entity.entity(requestObj, MediaType.APPLICATION_JSON_TYPE))
 
         assert(200 == response.getStatus)
 
-        val sessionHandle = OBJECT_MAPPER.readValue(
-          response.readEntity(classOf[String]), classOf[SessionHandle])
+        val sessionHandle = response.readEntity(classOf[SessionHandle])
 
         assert(sessionHandle.protocol.getValue == 1)
         assert(sessionHandle.identifier != null)
@@ -87,8 +79,7 @@ class SessionsResourceSuite extends RestApiBaseSuite {
 
         // verify the open session count again
         response = target("api/v1/sessions/count").request().get()
-        val openedSessionCount = OBJECT_MAPPER.readValue(
-          response.readEntity(classOf[String]), classOf[SessionOpenCount])
+        val openedSessionCount = response.readEntity(classOf[SessionOpenCount])
         assert(openedSessionCount.openSessionCount == 0)
     }
   }

Reply via email to