2

Found a problem with a datetime de-serialization (nomad-sdk version 0.11.3.0).

Server(agent) version: Nomad v1.0.1 (c9c68aa55a7275f22d2338f2df53e67ebfcb9238)

When I try to get an allocation list from the nomad agent (via API) I get the following error:

Complete stack trace could be found here: https://pastebin.pl/view/9bf82a78

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2020-12-17T11:58:59.346780177+01:00": not a valid representation (error
: Failed to parse Date value '2020-12-17T11:58:59.346780177+01:00': Can not parse date "2020-12-17T11:58:59.346780177+0100": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leni
ency? null))

Suggested/Tested workaround:

package com.hashicorp.nomad.apimodel;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class CustomDateDeserializer extends StdDeserializer<Date> {
    
    public CustomDateDeserializer() {
        super(Date.class);
    }

    @Override
    public Date deserialize(com.fasterxml.jackson.core.JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        
        final String date = p.getText();
        if (date.equals("0001-01-01T00:00:00Z")) {
            return new Date();
        }
        DateTimeFormatter isoDateTimeFormat = ISODateTimeFormat.dateTime();
        return isoDateTimeFormat.parseDateTime(date).toDate();
    }
}


public abstract class NomadJson {

    static {
        OBJECT_MAPPER.setConfig(
                OBJECT_MAPPER.getSerializationConfig()
                        .with(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"))
        );

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addDeserializer(Date.class, new CustomDateDeserializer());
        OBJECT_MAPPER.registerModule(simpleModule);

    }

}

//Added  "CustomDateDeserializer"  to AllocDeploymentStatus.java


public final class AllocDeploymentStatus extends ApiObject {

    @JsonProperty("Timestamp")
    @JsonDeserialize(using = CustomDateDeserializer.class)
    public Date getTimestamp() {
        return timestamp;
    }
  
}


Ivan Prostran
  • 53
  • 1
  • 5

0 Answers0