citerus/dddsample-core

Date parsing failure when assigning cargo to route

Closed this issue · 4 comments

I can get the application to error when booking cargo:

  1. Go to http://localhost:8080/dddsample/admin/list
  2. Click on book new cargo http://localhost:8080/dddsample/admin/registration
  3. Origin JNTKO Destination AUMEL, Select arrival deadline a couple of months out "03/06/2023" (not US-locale, it uses Europe Locale)
  4. Click book http://localhost:8080/dddsample/admin/show?trackingId=7FEB8AEE
  5. Says Not routed - so click "Route this cargo" - http://localhost:8080/dddsample/admin/selectItinerary?trackingId=7FEB8AEE
  6. Click first button, "assign cargo to this route"

See page with exception:

Sun Apr 23 14:13:04 PDT 2023
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='routeAssignmentCommand'. Error count: 10

The logs show:

Field error in object 'routeAssignmentCommand' on field 'legs[1].toDate': rejected value [2023-04-29 07:05]; codes [typeMismatch.routeAssignmentCommand.legs[1].toDate,typeMismatch.routeAssignmentCommand.legs.toDate,typeMismatch.legs[1].toDate,typeMismatch.legs.toDate,typeMismatch.toDate,typeMismatch.java.time.Instant,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [routeAssignmentCommand.legs[1].toDate,legs[1].toDate]; arguments []; default message [legs[1].toDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.Instant' for property 'legs[1].toDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.Instant] for value '2023-04-29 07:05'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-04-29 07:05]]

Running on Ubuntu 18, JDK 11

❱ java -version                 
Picked up JAVA_TOOL_OPTIONS: -XX:-UseBiasedLocking -Djdk.nio.maxCachedBufferSize=262144 -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints
openjdk version "11.0.14.1" 2022-02-08 LTS
OpenJDK Runtime Environment Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS, mixed mode)

Looks like it's probably

th:value="${#dates.format(leg.loadTime,'yyyy-MM-dd hh:mm')}"/>

orende commented

Hi @wsargent ! I had a look at the PR but would like to see a test verifying the fix.

image
public class InstDateEditor extends PropertyEditorSupport {

@Override
public void setAsText(@Nullable String text) throws IllegalArgumentException {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    LocalDateTime dateTime = LocalDateTime.parse(text, formatter);
    setValue(dateTime.toInstant(ZoneOffset.UTC));
}

/**
 * Format the Date as String, using the specified DateFormat.
 */
@Override
public String getAsText() {
    Instant value = (Instant) getValue();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    String formattedDateTime = formatter.format(value.atZone(ZoneOffset.UTC));
    return (value != null ? formattedDateTime : "");
}