Skip to content
This repository was archived by the owner on Dec 2, 2022. It is now read-only.
This repository was archived by the owner on Dec 2, 2022. It is now read-only.

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

@ivanprostran

Description

@ivanprostran

Nomad version

nomad-sdk version 0.11.3.0
Server(agent) version: Nomad v1.0.1 (c9c68aa55a7275f22d2338f2df53e67ebfcb9238)

Operating system and Environment details

/etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
Linux blade1.lab.bulb.hr 3.10.0-693.21.1.el7.x86_64 hashicorp/nomad#1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Issue

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

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))

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

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;
    }
  
}

The same behavior also with the version 0.9.6 and others.

Thank you for your effort and time.
Regards,
Ivan

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions