From dd90570c5e9e8f27fa8706134b4b8c81c3741a80 Mon Sep 17 00:00:00 2001
From: Pascal Arlt <parlt@suse.com>
Date: Mon, 15 Jul 2024 13:32:24 +0200
Subject: [PATCH] Fix date format when using the HTTP API to use iso8601 date
 (bsc#1227543)

Signed-off-by: Pascal Arlt <parlt@suse.com>
---
 .../com/suse/manager/api/DateSerializer.java  | 48 +++++++++++++++++++
 .../com/suse/manager/api/RouteFactory.java    |  2 +
 .../manager/api/test/RouteFactoryTest.java    |  9 ++--
 ...ava.changes.parlt.fix-http-api-date-format |  1 +
 4 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 java/code/src/com/suse/manager/api/DateSerializer.java
 create mode 100644 java/spacewalk-java.changes.parlt.fix-http-api-date-format

diff --git a/java/code/src/com/suse/manager/api/DateSerializer.java b/java/code/src/com/suse/manager/api/DateSerializer.java
new file mode 100644
index 000000000000..87428767e928
--- /dev/null
+++ b/java/code/src/com/suse/manager/api/DateSerializer.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2024 SUSE LLC
+ *
+ * This software is licensed to you under the GNU General Public License,
+ * version 2 (GPLv2). There is NO WARRANTY for this software, express or
+ * implied, including the implied warranties of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
+ * along with this software; if not, see
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
+ *
+ * Red Hat trademarks are not licensed under GPLv2. No permission is
+ * granted to use or replicate Red Hat trademarks that are incorporated
+ * in this software or its documentation.
+ */
+
+package com.suse.manager.api;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import java.lang.reflect.Type;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.TimeZone;
+
+/**
+ * Custom {@link Date} serializer to output Date objects in iso8601 format
+ */
+public class DateSerializer implements JsonSerializer<Date> {
+
+    private final DateFormat dateFormat;
+
+    /**
+     * Initialize dateFormat and set timezone to UTC
+     */
+    public DateSerializer() {
+        dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
+    }
+
+    @Override
+    public JsonElement serialize(Date date, Type type, JsonSerializationContext context) {
+        return new JsonPrimitive(dateFormat.format(date));
+    }
+}
diff --git a/java/code/src/com/suse/manager/api/RouteFactory.java b/java/code/src/com/suse/manager/api/RouteFactory.java
index 2e7047f3ed87..94c3153bb3b8 100644
--- a/java/code/src/com/suse/manager/api/RouteFactory.java
+++ b/java/code/src/com/suse/manager/api/RouteFactory.java
@@ -46,6 +46,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -289,6 +290,7 @@ else if ("sessionKey".equals(param.getName())) {
      */
     private Gson initGsonWithSerializers() {
         GsonBuilder builder = new GsonBuilder()
+                .registerTypeAdapter(Date.class, new DateSerializer())
                 .registerTypeAdapter(Map.class, new MapDeserializer())
                 .registerTypeAdapter(List.class, new ListDeserializer());
 
diff --git a/java/code/src/com/suse/manager/api/test/RouteFactoryTest.java b/java/code/src/com/suse/manager/api/test/RouteFactoryTest.java
index 5996e63527d5..7a642bfccc1e 100644
--- a/java/code/src/com/suse/manager/api/test/RouteFactoryTest.java
+++ b/java/code/src/com/suse/manager/api/test/RouteFactoryTest.java
@@ -244,11 +244,12 @@ public void testBasicDate() throws Exception {
         Method basicDate = handler.getClass().getMethod("basicDate", Date.class);
         Route route = routeFactory.createRoute(basicDate, handler);
 
-        Request req = createRequest("/manager/api/test/basicDate", Collections.singletonMap("myDate", "2022-04-01"));
+        Request req = createRequest("/manager/api/test/basicDate",
+                Collections.singletonMap("myDate", "2022-04-01T00:00:00+02:00"));
         Response res = createResponse();
         String result = getResult((String) route.handle(req, res), String.class);
 
-        assertEquals("Apr 1, 2022, 12:00:00 AM", result);
+        assertEquals("2022-03-31T22:00:00Z", result);
     }
 
     /**
@@ -260,11 +261,11 @@ public void testBasicDateInBody() throws Exception {
         Route route = routeFactory.createRoute(basicDate, handler);
 
         Request req = createRequest("/manager/api/test/basicDate",
-                GSON.toJson(Collections.singletonMap("myDate", "2022-04-01")));
+                GSON.toJson(Collections.singletonMap("myDate", "2022-04-01T00:00:00+02:00")));
         Response res = createResponse();
         String result = getResult((String) route.handle(req, res), String.class);
 
-        assertEquals("Apr 1, 2022, 12:00:00 AM", result);
+        assertEquals("2022-03-31T22:00:00Z", result);
     }
 
     /**

