Skip to content

bean converter: merge with @BindParam #3648

Open
@jknack

Description

@jknack

Today, BeanConverter looks like:

public interface BeanConverter extends ValueConverter<ValueNode> {

  boolean supports(@NonNull Class<?> type);


  Object convert(@NonNull ValueNode node, @NonNull Class<?> type);
}

The convert method is tied/bind to one of the request object model like: query, form, header, etc...

There is also a BindParam annotation for MVC controllers, which is more powerful due it has access to the entire Context. Signature of bind factory looks like:

public static MyBean of(Context ctx) {
    // build MyBean from HTTP request
  }

There are 2 ways of doing the same thing, where the Bind alternative is a bit more flexible.

The goal here is to merge both into a single alternative in 4.x like:

public interface BeanConverter {

  <T> T create(@NonNull Context ctx, Class<T> type)  {
      if (type == Person.class) {
         return createPerson(ctx);
      }
      throw new Unsupported();
   }
}

The bean converter must be registered like today:

{
   converter(MyBeanConverter());
}

Then every time we ask for Person:

From lambda

{
   get("/", ctx -> {
     var queryPerson = ctx.query(Person.class);
     var formPerson = ctx.form(Person.class);
  })
}

Or mvc

{
   public Person doSomething(@QueryParam Person person) {
      return person;
   }
}

It will look at bean converter or fallback to reflection.

The BindParam will be removed/deleted in 4.x. This also allows to remove reflection from bean converter which makes #3546 useless

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions