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

[PHP] Use String instead of Byte Array #1990

Merged
merged 2 commits into from
Mar 4, 2016

Conversation

jqr
Copy link
Contributor

@jqr jqr commented Jan 27, 2016

When { "type": "string", format": "binary" } is requested, it's internally represented as a ByteArray, which was then translated into PHP as a literal byte array. A literal byte array in PHP is [72, 101, 108, 108, 111] and is not a format supported by any file, echoing, or any standard library operations other than general array handling.

I propose that a "binary string" should represent just that in PHP, an unencoded string. This allows access to the full standard library of tools for dealing with strings as bytes, including file writing and echoing.

This should have some notable performance improvements for end-users as the previous fix was essentially to do the following, which ends up making 4 copies of the data as it passes through the various layers of unpacking, and repacking.

# customer code                                                 swagger code
#            copy                      copy                     copy         original
$string = call_user_func_array('pack', array_merge(array('C*'), unpack("C*", (string)response)) )

I tried fixing the response type in documentation to string instead of ByteArray by manipulating the mapping, but this runs into issues with bypassing the raw decoding in ApiClient.php and then gets decoded as a JSON object. I'm open to advice on how to fix this.

@jqr jqr changed the title Use String instead of Byte Array in PHP [PHP] Use String instead of Byte Array Jan 27, 2016
@wing328 wing328 added this to the v2.1.6 milestone Jan 28, 2016
@wing328
Copy link
Contributor

wing328 commented Jan 29, 2016

@jqr thanks for the PR. For the issue, I think you're referring to this line: https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php#L237

What about adding another check (else) for response type at https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php#L235 and return the raw body if the response type is string?

@jqr
Copy link
Contributor Author

jqr commented Jan 30, 2016

Won't ByteArray effectively disappear with this change? I was thinking of changing the mapping in PhpClientCodegen.java#L104 so the generated comments would say string instead of ByteArray.

I think I have combined the approaches to get what we're after, let me know what you think.

@wing328
Copy link
Contributor

wing328 commented Jan 31, 2016

Thinking about this more. I suggest we rename "ByteArray" to "binary(string)" as PHP developers may not have a clue what ByteArray means or string actually refers to "binary" in the method help text, e.g. https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php#L983

@wing328
Copy link
Contributor

wing328 commented Feb 15, 2016

@jqr what's your view on my suggestion?

@jqr
Copy link
Contributor Author

jqr commented Mar 3, 2016

I prefer just plain string, as implemented now. I think that's the actual type involved and what should be expressed.

I get that there are consistency issues across a bunch of languages though so I'm happy to defer to your opinion. string(binary) makes slightly more sense to me than binary(string) but you let me know what you want and I'll make it happen. Or feel free to just take this branch forward as you see fit.

@wing328
Copy link
Contributor

wing328 commented Mar 3, 2016

@jqr I respect your opinion as well. Would you please rebase on the latest master to resolve the merge conflicts? Then I'll review and approve accordingly.

@jqr
Copy link
Contributor Author

jqr commented Mar 3, 2016

Generating the clients locally adds a good amount of new code to the petstore php client which I have opted not to check in.

@wing328
Copy link
Contributor

wing328 commented Mar 3, 2016

OK. Those are new fake endpoints we added to unit test some bugs we've addressed.

I'll add those in my next PHP PR.

wing328 added a commit that referenced this pull request Mar 4, 2016
[PHP] Use String instead of Byte Array
@wing328 wing328 merged commit a92a9f2 into swagger-api:master Mar 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants