Github user jeking3 commented on a diff in the pull request: https://github.com/apache/thrift/pull/1215#discussion_r108155957 --- Diff: lib/php/src/ext/thrift_protocol/php_thrift_protocol.cpp --- @@ -728,6 +728,42 @@ inline bool ttypes_are_compatible(int8_t t1, int8_t t2) { return ((t1 == t2) || (ttype_is_int(t1) && ttype_is_int(t2))); } +//is used to validate objects before serialization and after deserialization. For now, only required fields are validated. +static +void validate_thrift_object(zval* object) { + + HashPosition key_ptr; + zval** val_ptr; + + TSRMLS_FETCH(); + zend_class_entry* object_class_entry = zend_get_class_entry(object TSRMLS_CC); + HashTable* spec = Z_ARRVAL_P(zend_read_static_property(object_class_entry, "_TSPEC", 6, false TSRMLS_CC)); + + for (zend_hash_internal_pointer_reset_ex(spec, &key_ptr); zend_hash_get_current_data_ex(spec, (void**)&val_ptr, &key_ptr) == SUCCESS; zend_hash_move_forward_ex(spec, &key_ptr)) { + ulong fieldno; + if (zend_hash_get_current_key_ex(spec, NULL, NULL, &fieldno, 0, &key_ptr) != HASH_KEY_IS_LONG) { + throw_tprotocolexception("Bad keytype in TSPEC (expected 'long')", INVALID_DATA); + return; + } + HashTable* fieldspec = Z_ARRVAL_PP(val_ptr); + + // field name + zend_hash_find(fieldspec, "var", 4, (void**)&val_ptr); + char* varname = Z_STRVAL_PP(val_ptr); + + zend_hash_find(fieldspec, "isRequired", 11, (void**)&val_ptr); + bool is_required = Z_BVAL_PP(val_ptr); + + zval* prop = zend_read_property(object_class_entry, object, varname, strlen(varname), false TSRMLS_CC); + + if (is_required && Z_TYPE_P(prop) == IS_NULL) { + char errbuf[128]; + snprintf(errbuf, 128, "Required field %s.%s is unset!", object_class_entry->name, varname); + throw_tprotocolexception(errbuf, INVALID_DATA); --- End diff -- I wonder if folks think it would be useful to add a new TProtocolException cause for this case, MISSING_REQUIRED, across all languages. While technically it is invalid data, I usually think of that as something got garbled.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---