Reuse ObjectMapper instance 15/104615/5
authorYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Mon, 27 Feb 2023 09:01:55 +0000 (11:01 +0200)
committerYaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
Tue, 28 Feb 2023 15:36:00 +0000 (17:36 +0200)
It's recommended to reuse ObjectMapper instances but in current codebase
we are recreating it.
ObjectMapper field was made static to prevent multiple object creation.

JIRA: NETCONF-938
Change-Id: If6ed5b5b1959b28d2330e3dc450afb3f20c15c29
Signed-off-by: Yaroslav Lastivka <yaroslav.lastivka@pantheon.tech>
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/BaseYangSwaggerGenerator.java
restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/jaxrs/JaxbContextResolver.java
restconf/sal-rest-docgen/src/test/java/org/opendaylight/netconf/sal/rest/doc/impl/ApiDocServiceImplTest.java

index 864a574f81eb49069bda56f2ea0ab6c2e7ddb3d4..4a345b66fa3c2bf5ce17fb2a0f13f39cacf5a177 100644 (file)
@@ -74,18 +74,20 @@ public abstract class BaseYangSwaggerGenerator {
     private static final String API_VERSION = "1.0.0";
     private static final String SWAGGER_VERSION = "2.0";
     private static final String OPEN_API_VERSION = "3.0.3";
+    private static final ObjectMapper MAPPER = new ObjectMapper();
 
     private final DefinitionGenerator jsonConverter = new DefinitionGenerator();
-
-    private final ObjectMapper mapper = new ObjectMapper();
     private final DOMSchemaService schemaService;
 
     public static final String BASE_PATH = "/";
     public static final String MODULE_NAME_SUFFIX = "_module";
 
+    static {
+        MAPPER.configure(SerializationFeature.INDENT_OUTPUT, true);
+    }
+
     protected BaseYangSwaggerGenerator(final Optional<DOMSchemaService> schemaService) {
         this.schemaService = schemaService.orElse(null);
-        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
     }
 
     public DOMSchemaService getSchemaService() {
@@ -240,7 +242,7 @@ public abstract class BaseYangSwaggerGenerator {
                 addFields(doc.getDefinitions(), definitions.fields());
             }
             if (LOG.isDebugEnabled()) {
-                LOG.debug("Document: {}", mapper.writeValueAsString(doc));
+                LOG.debug("Document: {}", MAPPER.writeValueAsString(doc));
             }
         } catch (final IOException e) {
             LOG.error("Exception occured in DefinitionGenerator", e);
index 4c1edc6ddaa13aa624427c095cd948de3873550c..7af7b02827f6bc2c51e4ae576b226c3bc299d194 100644 (file)
@@ -20,16 +20,12 @@ import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
 @Produces(MediaType.APPLICATION_JSON)
 public class JaxbContextResolver implements ContextResolver<ObjectMapper> {
 
-    private final ObjectMapper ctx;
-
-    public JaxbContextResolver() {
-        ctx = new ObjectMapper();
-    }
+    private static final ObjectMapper CTX = new ObjectMapper();
 
     @Override
     public ObjectMapper getContext(final Class<?> klass) {
         if (SwaggerObject.class.isAssignableFrom(klass)) {
-            return ctx;
+            return CTX;
         }
 
         return null; // must return null so that JAX-RS can continue context search
index 04dd409d91db58c626d1b2775d6bc88d12949b01..9a8d067c30f77c4e0a3cf215dfcea0bdee4dce21 100644 (file)
@@ -29,6 +29,7 @@ public final class ApiDocServiceImplTest extends AbstractApiDocTest {
             .node(QName.create("", "nodes"))
             .node(QName.create("", "node"))
             .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
+    private static final ObjectMapper MAPPER = new ObjectMapper();
 
     private ApiDocService apiDocService;
 
@@ -50,8 +51,7 @@ public final class ApiDocServiceImplTest extends AbstractApiDocTest {
     public void getListOfMounts() throws Exception {
         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
         // simulate the behavior of JacksonJaxbJsonProvider
-        final ObjectMapper mapper = new ObjectMapper();
-        final String result = mapper.writer().writeValueAsString(apiDocService.getListOfMounts(mockInfo).getEntity());
+        final String result = MAPPER.writer().writeValueAsString(apiDocService.getListOfMounts(mockInfo).getEntity());
         assertEquals("[{\"instance\":\"/nodes/node=123/\",\"id\":1}]", result);
     }
 }