YAML Dates not parsed correctly

Bug #594186 reported by dirk
12
This bug affects 2 people
Affects Status Importance Assigned to Milestone
play framework
Status tracked in 1.0
1.0
Invalid
Undecided
Unassigned
1.1
Fix Committed
Undecided
Unassigned

Bug Description

When parsing a YAML file the Fixture.load() method uses Snake YAML to create an object graph, and then converts all values in the graph to strings, and then converts them back to objects. This works for most objects, however not for YAML dates.

eg "2001-12-14t21:59:43.10-05:00" is a valid YAML date, and Snake YAML parses it correctly, but Play they converts it into a string, and when it tries to convert it back to a date it fails. This is because Play uses the date.format configuration key when parsing the date. It will only work if date.format is specified to be "yyyy-MM-dd't'HH:mm:ss.SSSZ".

One possible solution would be to avoid converting Date objects to a String in the first place, as snake YAML already parses the dates correctly.

See http://yaml.org/spec/1.1/#id858600 for valid YAML date formats.

Revision history for this message
dirk (australiandeveloper) wrote :

The fix for this is to change one line in play.test.Fixtures.java (in play 1.1 it is line number 192).

Change this:

for(Field f : model.getClass().getFields()) {
    // TODO: handle something like FileAttachment
    if (f.getType().isAssignableFrom(Map.class)) {
        f.set(model, objects.get(key).get(f.getName()));
    }

}

To this:

for(Field f : model.getClass().getFields()) {
    // TODO: handle something like FileAttachment
    if (f.getType().isAssignableFrom(Map.class) ||
            f.getType().isAssignableFrom(java.util.Date.class)) {
        f.set(model, objects.get(key).get(f.getName()));
    }

}

Revision history for this message
dirk (australiandeveloper) wrote :

Sample files:

Java model
=========
package models;

import java.util.Date;
import javax.persistence.Entity;
import play.db.jpa.Model;

@Entity
public class Country extends Model {
    public String name;
    public Date founded;
}

Yaml
====
Country(argentina):
    name: Argentina
    founded: 2001-01-01

Revision history for this message
dirk (australiandeveloper) wrote :

To make things easier I create a branch of play 1.1 and made the fix:
http://bazaar.launchpad.net/~australiandeveloper/play/1.1-dev/revision/980

Revision history for this message
Erwan Loisant (eloisant) wrote :

Hi,

Thank you for the patch. Next time, could you create a branch with just the fix for this bug? It's easier to merge when you do so.

(with a test in the "just-test-cases" sample app it's even better!)

Revision history for this message
dirk (australiandeveloper) wrote :

Hi Erwan,
I'm more accustomed to git than to bzr so I may be wrong about this, but if I create a branch for each patch, that means I have to upload the entire branch to launchpad right? I did that yesterday and it took several hours because it was so big that it kept failing half way through.
Is there another way?
Thanks,
Dirk

Revision history for this message
Erwan Loisant (eloisant) wrote : Re: [Bug 594186] Re: YAML Dates not parsed correctly

On Tue, Aug 3, 2010 at 16:16, dirk <email address hidden> wrote:
> Hi Erwan,
> I'm more accustomed to git than to bzr so I may be wrong about this, but if I create a branch for each patch, that means I have to upload the entire branch to launchpad right? I did that yesterday and it took several hours because it was so big that it kept failing half way through.
> Is there another way?

Yes, that's right - I'm more used to Git also and I forgot about it.
In that case I guess a patch would be the best.

--
Erwan Loisant

Revision history for this message
dirk (australiandeveloper) wrote :

Yeah I think in general it's a bit of a barrier to entry for people wanting to make improvements to the code and documentation. Someone made a similar comment recently on the google group:
http://groups.google.com/group/play-framework/browse_thread/thread/c5cd8d2f822cae93#

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.