ninjaframework / ninja

Ninja is a full stack web framework for Java. Rock solid, fast and super productive.
http://www.ninjaframework.org
Apache License 2.0
1.91k stars 518 forks source link

Form parsing fails when pojo extends pojo #254

Open dimitri85 opened 10 years ago

dimitri85 commented 10 years ago

Hello Guys,

I just started to toy around with Ninja and I love it. Though.. did run in some strange behaviour:

I'm talking about http://www.ninjaframework.org/documentation/html_templating/form_parsing.

When an HTML form gets posted, the corresponding values gets automatically into Contact, that's great. public Result postContactForm(Context context, Contact contact) { return Results.html().render(contact); }

What I've seen today is that when I create a new Contact class (eg SpecialContact EXTENDS Contact) then this object doesn't get the values posted.?? Not the old ones (coming from Contact) and not the new ones (from SpecialContact). The object is null.

Is this the default behaviour, is there a way around or is this a bug that will be solved?

My intentions are to make a lot of these methods dynamic so I have one basic codebase and different projects can have different Contact-fields.

Thanks anyway for this wonderful framework, I'll keep enjoy working with it and maybe when I've learned enough, contribute to the project too.

Greetings, Dimitri

raphaelbauer commented 10 years ago

Hi Dimitri,

thanks for the report... Can you provide some example code so that we can check the problem? The deluxe way would be a tiny project with a testcase that allows us to reproduce the problem...

Thanks!

Raphael

metacity commented 10 years ago

Setting the fields of the superclass doesn't work (declared fields are not collected recursively in the class hierarchy), but it should work the declared class's fields and the object shouldn't be null. Do you have an empty contructor available in the SpecialContact class? Ninja uses Class#newInstance() method to instantiate the controller method parameter object, and thus an empty constructor is required.

dimitri85 commented 10 years ago

Well, it is a shame that the declared fields of the superclass aren't collected recursively but I guess you need to stop somewhere (even is it to stop programmers from making too heavy apps or infinitive loops).

I'm not sure anymore about the value of the 'phone'-field in mijn SpecialContact. What I do remember is Freemarker crashing over the phone-field. <#if contact.phone??> show phone here </#if>

As I have no longer a Ninja project setup at the moment, here is what I've added to the example from the website (yes, there is/was an empty constructor):

public class SpecialContact extends Contact {

private String phone;

public SpecialContact() {}

public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}

}

I hope this can be cleared out of people experiencing this. And I suggest to update the docs to avoid getting this kind of post to much :)

Keep up the good work guys.

metacity commented 10 years ago

I suppose class hierarchy traversal just wasn't thought of when the post body parser was implemented. But an important future feature tho!

Was the object actually null in the controller method already? And an important note: when using direct object rendering to Freemarker (á la return Results.html().render(contact);), the Freemarker variable will be named after the object's class name and not after its instance name (as mentioned here). So an instance of SpecialContact would be "specialContact" when accessing it in Freemarker.

As Raphael noted, we would appreciate more through a code example :)