Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 8.1 serialization #43

Merged
merged 9 commits into from
Nov 8, 2022
Merged

Fix 8.1 serialization #43

merged 9 commits into from
Nov 8, 2022

Conversation

bix0r
Copy link
Collaborator

@bix0r bix0r commented Oct 21, 2022

See issue #42 for information.

This fixes tests for all versions after 8.1 compatibility fixes.

Only for php8.1+.
Calling the old php functions instead of mimicking the code.
This is slower, but the code is simpler, and here we are only talking about when you run (un)serialize() function.
The new serialize methods should throw on error.

Issue: phpGH-42
Because we changed the output of the serialization in version 8.1
Copy old tests with fixes to output
Shows that the old serialized string works, and it results in the same code as the new format.
Also, show that __serialize() method returns an array with 'xml' key
@bix0r bix0r marked this pull request as ready for review October 25, 2022 09:01
@bix0r
Copy link
Collaborator Author

bix0r commented Oct 25, 2022

@cmb69 I wonder if you can review this code for me?

@cmb69
Copy link
Member

cmb69 commented Oct 25, 2022

I wonder if you can review this code for me?

I'll try to have a closer look tomorrow, but I strongly suggest to set up some CI. Likely GH actions would be the best solution.

Also consider to introduce stub files. These can be used to build the required arginfo structures automatically.

@bix0r
Copy link
Collaborator Author

bix0r commented Oct 25, 2022

I'll try to have a closer look tomorrow

Awesome. Thank you!

I strongly suggest to set up some CI. Likely GH actions would be the best solution

It is planned, and is pretty much next on my todo list.

Also consider to introduce stub files.

Sounds good. I will take a look.

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but I've left some suggestions for your consideration.

RETURN_THROWS();
}
array_init(return_value);
add_assoc_stringl(return_value, "xml", Z_STRVAL(serialized), Z_STRLEN(serialized));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified:

Suggested change
add_assoc_stringl(return_value, "xml", Z_STRVAL(serialized), Z_STRLEN(serialized));
add_assoc_str(return_value, "xml", serialized);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Since serialized is a zval I used add_assoc_zval

zend_string *key_str = zend_string_init("xml", sizeof("xml") -1, 1);
zend_object *object = NULL;
zval *tmp = NULL;
/* Process the parameters passed to the default constructor */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"default constructor" sounds wrong. Maybe just drop the comment?

RETURN_THROWS();
}
array_init(return_value);
add_assoc_stringl(return_value, "xml", Z_STRVAL(serialized), Z_STRLEN(serialized));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_assoc_stringl(return_value, "xml", Z_STRVAL(serialized), Z_STRLEN(serialized));
add_assoc_str(return_value, "xml", serialized);

@@ -180,6 +180,12 @@ ZEND_BEGIN_ARG_INFO(SolrDocument_unserialize_args, 0)
ZEND_ARG_INFO(0, serialized)
ZEND_END_ARG_INFO()

#if PHP_VERSION_ID >= 80100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to preserve full BC with PHP < 8.1, this looks right. If, on the other hand, that would not be necessary, you could do all the new PHP 8.1 stuff unconditionally. Prior to PHP 7.4.0, it would be ignored; as of PHP 7.4.0, Serializable would be ignored.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, here I had many problems. I ended up with only breaking BC for 8.1 (I guess I am not really breaking anything since we were not really supporting 8.1).

  1. If both serialize() and __serialize() are available PHP will pick the new one in 7.4+. Those two create totally different outputs, as seen in the tests I duplicated instead of changed because of this difference.
  2. Method signature for zend_call_method() was changed in 8.0. So, I would have had to deal with that mismatch somehow.
  3. This extension uses the unserialization process to create objects from the XML responses, so I don't really dare to change things too much :)

@@ -0,0 +1,50 @@
--TEST--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider not to split the tests; it might be okay to use some placeholders for the version-dependent parts (if all else fails, you can use regular expressions inside of %r markers, e.g. %rC:20:"SolrModifiableParams"|O:12:"SolrDocument"%r).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kinda like having the tests split because I am going with the different api for 8.1+.
Also, the difference in the serialization output is quite different between the old and the new.

C:12:"SolrDocument":172:{<?xml version="1.0" encoding="UTF-8"?>

vs.

O:12:"SolrDocument":1:{s:3:"xml";s:172:"<?xml version="1.0" encoding="UTF-8"?>

And then also the ending.

print_r(unserialize($serializedString)->toArray());
?>
--EXPECT--
O:12:"SolrDocument":1:{s:3:"xml";s:172:"<?xml version="1.0" encoding="UTF-8"?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, avoid hard-coded string lengths in tests if there are line-breaks; this might not work on Windows (LF vs. CRLF). Instead you can use a placeholder:

Suggested change
O:12:"SolrDocument":1:{s:3:"xml";s:172:"<?xml version="1.0" encoding="UTF-8"?>
O:12:"SolrDocument":1:{s:3:"xml";s:%d:"<?xml version="1.0" encoding="UTF-8"?>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I don't want to change these tests, they are old and tried (I hope).

@cmb69
Copy link
Member

cmb69 commented Oct 28, 2022

@remicollet might have some thoughts about this, too, since he noted that #30 breaks BC.

@bix0r bix0r merged commit d516f85 into php:master Nov 8, 2022
@bix0r bix0r deleted the fix-8-1-serialization branch November 8, 2022 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants