Skip to content

Commit

Permalink
Merge pull request #9054 from parlt91/fix-http-api-date-format
Browse files Browse the repository at this point in the history
Fix date format when using the HTTP API to use iso8601 date
  • Loading branch information
deneb-alpha authored Sep 5, 2024
2 parents f84fd71 + dd90570 commit 107dcd8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
48 changes: 48 additions & 0 deletions java/code/src/com/suse/manager/api/DateSerializer.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
2 changes: 2 additions & 0 deletions java/code/src/com/suse/manager/api/RouteFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
9 changes: 5 additions & 4 deletions java/code/src/com/suse/manager/api/test/RouteFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
1 change: 1 addition & 0 deletions java/spacewalk-java.changes.parlt.fix-http-api-date-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix the date format output when using the HTTP API to use iso8601 format (bsc#1227543)

0 comments on commit 107dcd8

Please sign in to comment.