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

[Java][okhttp] Fix gson deserialize format byte #7473

Merged

Conversation

benrobot
Copy link
Contributor

The java client based off of okhttp-gson libraries failed to serialized and deserialize type=string, format=byte

This PR aims to add byte[] serialization/deserialization functionality by adding a new Gson type adapter which employs the Base64 class available starting with Java 8

Unit testing showed that the default gson setup was also escaping the = (equal signs) commonly found at the end of a base64 string therefore, the disableHtmlEscaping option has been set via the gson builder

Fix #4824
cc: @bbdouglas, @JFCote, @sreeshas, @jfiala, @lukoyanov, @cbornet, @jeff9finger

@@ -37,6 +37,7 @@ import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
Copy link
Contributor

Choose a reason for hiding this comment

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

Okhttp has a Base64 class that can be used instead of the Java8 one. This way android users can enjoy the fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -917,6 +917,10 @@
"pending",
"sold"
]
},
"profilePhoto": {
Copy link
Contributor

Choose a reason for hiding this comment

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

This will impact all generators. So all examples shall be regenerated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

Copy link
Contributor

Choose a reason for hiding this comment

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

We want to keep petstore.json, petstore.yaml the same as the Petstore server (https://petstore.swagger.io) so please undo the change in these files.

I noticed that you already added the additional test case in petstore-with-fake-endpoints-models-for-testing.yaml (there's an similar test case here: https://github.com/benrobot/swagger-codegen/blob/53018c475ad6bbc097f29d47b11a2bc55b5142ea/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml#L1202)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. I'll undo the addition of Pet.profilePhoto and use the existing format_test.byte.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be clear. I am not regenerating all the clients because I've undone the changes instead.

@@ -55,6 +56,9 @@ public class JSON {
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
{{/jsr310}}
{{#java8}}
private LocalByteArrayAdapter localByteArrayAdapter = new LocalByteArrayAdapter();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just ByteArrayAdapter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I thought I was following the naming convention of the other type adapters but only the DateAdapter was prepended with Local.

@@ -105,6 +109,10 @@ public class JSON {
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
{{/jsr310}}
{{#java8}}
.disableHtmlEscaping(){{! Added to prevent base64 = (equal sign) from being escaped }}
Copy link
Contributor

Choose a reason for hiding this comment

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

The gson default is to escape html (it is the contrary for Jackson) so this should be a choice left to the SDK user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed disableHtmlEscaping. Changed test to compensate.

@Test
public void testApiClient() {
// the default api client is used
Copy link
Contributor

Choose a reason for hiding this comment

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

Something is wrong. The tests shouldn’t be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will put these back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deleted tests have been undeleted

…for base64 conversions instead of java 8 lib since the okhttp-gson client already includes okio. Remove setting to disableHtmlEscaping. Rename LocalByteArrayAdapter to ByteArrayAdapter.
…eady available under the format_test definition.
…erify exception since the server now returns a 500 for this request.
@benrobot
Copy link
Contributor Author

I see that CircleCI tests against a local instance of http://petstore.swagger.io. I will spin up my own instance so my tests run against that instead of the one published on the internet.

…gerapi/petstore (heads up, this test fails if calling out to the internet published version of http://petstore.swagger.io)
@wing328 wing328 merged commit 47111b3 into swagger-api:master Jan 26, 2018
@wing328
Copy link
Contributor

wing328 commented Jan 26, 2018

@benrobot thanks for the PR, which has been merged into master.

@wing328 wing328 changed the title Issue 4824 fix gson deserialize format byte [Java][okhttp] Fix gson deserialize format byte Jan 26, 2018
@brendandburns
Copy link
Contributor

@wing328 we're using the 3.0.0 branch but I'd like to have this fix. Is there a good way to do that? Can I propose a cherry-pick of this PR back into the 3.0.0 branch?

Thanks!
--brendan

@webron
Copy link
Contributor

webron commented Mar 22, 2018

@HugoMario ping

@bakshiabhisek
Copy link

@wing328 Looks like this fix is still not in 3.0.11?

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.

[JAVA] Codegen - swagger type=string format=byte not serialised correctly
7 participants