pulsar-client-php icon indicating copy to clipboard operation
pulsar-client-php copied to clipboard

Schema doesn't respect Object

Open djdance opened this issue 1 year ago • 4 comments

We are trying to work with schemas. It appears that it sends any king of object, regardless of schema set. For example, it will send your initial Person Object from the example without any errors. The only thing it checks is schema itself, it must be valid (that's okay).

Is there any option to validate an object before send?

thanks

djdance avatar Mar 23 '23 13:03 djdance

@djdance

Yes, schemas can send any object to pulsar without validating the object options, but it must meet this one requirement, the object must be able to json_encode, however json_encode will only extract public properties, so as long as the object can successfully json_encode it will be sent successfully, the same goes for the consumer, when it receives When the json data is received, it will be converted to the corresponding object, and there is no need to manually convert it to an object, which saves us a lot of conversion work

example

class Person
{
    public $id;
    public $name;
    public $age;

    protected $test = 'ignore';
    // ...
}

$person = new Person();
$person->id = 1;
$person->name = 'Tony';
$person->age = 1;

echo json_encode($person);

// output:{"id":1,"name":"Tony","age":1}

$producer->send($person);   // The actual message sent is `{"id":1,"name":"Tony ","age":1}`



ikilobyte avatar Mar 23 '23 13:03 ikilobyte

got it. Would you plan to add some kind of validation to Object sending? as in appropriate Java library (they say)

djdance avatar Mar 23 '23 14:03 djdance

I don't know what verification there is in the java library, can you tell me in detail?

ikilobyte avatar Mar 23 '23 14:03 ikilobyte

I am not so familiar with this java realization, but apparently this is here https://github.com/spring-projects-experimental/spring-pulsar/blob/488c5d2e64294009453611d83884321fe228b834/spring-pulsar/src/main/java/org/springframework/pulsar/core/DefaultSchemaResolver.java#L159

djdance avatar Mar 23 '23 15:03 djdance