jakartaee / jsonb-api

Jakarta JSON Binding
https://eclipse-ee4j.github.io/jsonb-api/
Other
78 stars 39 forks source link

Jsonb as a CDI bean must support CDI Jsonb component lookup must not standalone instances #265

Open rmannibucau opened 3 years ago

rmannibucau commented 3 years ago

Today if you do:

jsonb = JsonbBuilder.create()

and map a model using a cdi adapter bound with

@JsonbTypeAdapter(MyCDIImpl.class)

then it works whereas it must not.

However if you would do:

@Inject Jsonb jsonb;

it should work

The issue not respecting that is you never know when creating a jsonb instance if CDI must be used or not. When aggregating libraries and inheriting from jsonb "standalone" instances you can end up using a CDI bean and make the SE library misbehave. The other issue for implementation is to know CDI must be used or not, in some environment like OSGi it is almost impossible - leading to dropping that feature.

Therefore the proposal of this ticket is to:

  1. Make it correctly define - stadanlone instance are not CDI friendly, add a Jsonb CDI bean, and make only this jsonb instance cdi friendly
  2. Add a jsonb.enableCDI boolean property (no need to define a constant or setter, config.setProperty is fine) to enable to switch back to v1.

Note: doing 2 in the other way is a workaround but keeps existing code broken. I understand changing 1.+2. can break code also but since it is saner and both options break some libs/apps I think it is worth make it clean now (+ it is very rarely used).

rotty3000 commented 3 years ago

I completely agree. Trying to make a magical connection between a CDI container and a jsonb provider is at best unreliable and so should not even be considered. CDI should only play a role if the the instance was provided via a bean. This is clean and free of magic (while current approach is prone to error and hard to implement.)