From 15b3dfbb80049a801a100f3b67fd7fd3748465e1 Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 26 Jan 2018 10:02:24 +0800 Subject: [PATCH] update java petstore samples --- .../src/main/java/io/swagger/client/JSON.java | 31 +++++++++++++++++++ .../java/okhttp-gson/.swagger-codegen/VERSION | 2 +- .../petstore/java/okhttp-gson/build.gradle | 8 ----- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/io/swagger/client/JSON.java index df84d97023..15ce43f658 100644 --- a/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/io/swagger/client/JSON.java +++ b/samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/io/swagger/client/JSON.java @@ -28,6 +28,7 @@ import org.threeten.bp.OffsetDateTime; import org.threeten.bp.format.DateTimeFormatter; import io.swagger.client.model.*; +import okio.ByteString; import java.io.IOException; import java.io.StringReader; @@ -46,6 +47,7 @@ public class JSON { private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); public static GsonBuilder createGson() { GsonFireBuilder fireBuilder = new GsonFireBuilder() @@ -87,6 +89,7 @@ public class JSON { .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) .registerTypeAdapter(LocalDate.class, localDateTypeAdapter) + .registerTypeAdapter(byte[].class, byteArrayAdapter) .create(); } @@ -153,6 +156,34 @@ public class JSON { } } + /** + * Gson TypeAdapter for Byte Array type + */ + public class ByteArrayAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, byte[] value) throws IOException { + if (value == null) { + out.nullValue(); + } else { + out.value(ByteString.of(value).base64()); + } + } + + @Override + public byte[] read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String bytesAsBase64 = in.nextString(); + ByteString byteString = ByteString.decodeBase64(bytesAsBase64); + return byteString.toByteArray(); + } + } + } + /** * Gson TypeAdapter for JSR310 OffsetDateTime type */ diff --git a/samples/client/petstore/java/okhttp-gson/.swagger-codegen/VERSION b/samples/client/petstore/java/okhttp-gson/.swagger-codegen/VERSION index b4308ebebb..855ff9501e 100644 --- a/samples/client/petstore/java/okhttp-gson/.swagger-codegen/VERSION +++ b/samples/client/petstore/java/okhttp-gson/.swagger-codegen/VERSION @@ -1 +1 @@ -2.4.0-SNAPSHOT +2.4.0-SNAPSHOT \ No newline at end of file diff --git a/samples/client/petstore/java/okhttp-gson/build.gradle b/samples/client/petstore/java/okhttp-gson/build.gradle index b6f39a58d5..349c0805d9 100644 --- a/samples/client/petstore/java/okhttp-gson/build.gradle +++ b/samples/client/petstore/java/okhttp-gson/build.gradle @@ -91,14 +91,6 @@ if(hasProperty('target') && target == 'android') { main = System.getProperty('mainClass') classpath = sourceSets.main.runtimeClasspath } - - compileJava { - options.encoding = "UTF8" - } - - compileTestJava { - options.encoding = "UTF8" - } } dependencies {