From fec0363f7dad7ce9103d6b1c0dba9c361ef6f777 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 1 Feb 2018 21:49:22 +0800 Subject: [PATCH] [Java] Add back byte array enhancement (#7546) * add back byte array enhanceement * add okio dependency to java rest assured api client --- .../src/main/resources/Java/JSON.mustache | 31 +++++++++++++++++++ .../rest-assured/build.gradle.mustache | 2 ++ .../libraries/rest-assured/build.sbt.mustache | 3 +- .../Java/libraries/rest-assured/pom.mustache | 26 ++++++++++------ .../petstore/java/rest-assured/build.gradle | 2 ++ .../petstore/java/rest-assured/build.sbt | 3 +- .../client/petstore/java/rest-assured/pom.xml | 16 +++++++--- .../src/main/java/io/swagger/client/JSON.java | 31 +++++++++++++++++++ 8 files changed, 97 insertions(+), 17 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/JSON.mustache b/modules/swagger-codegen/src/main/resources/Java/JSON.mustache index 34ae1795cf86..63814a361fce 100644 --- a/modules/swagger-codegen/src/main/resources/Java/JSON.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/JSON.mustache @@ -26,6 +26,7 @@ import org.threeten.bp.format.DateTimeFormatter; {{/threetenbp}} import {{modelPackage}}.*; +import okio.ByteString; import java.io.IOException; import java.io.StringReader; @@ -55,6 +56,7 @@ public class JSON { private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); {{/jsr310}} + private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter(); public static GsonBuilder createGson() { GsonFireBuilder fireBuilder = new GsonFireBuilder() @@ -105,6 +107,7 @@ public class JSON { .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) .registerTypeAdapter(LocalDate.class, localDateTypeAdapter) {{/jsr310}} + .registerTypeAdapter(byte[].class, byteArrayAdapter) .create(); } @@ -171,6 +174,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(); + } + } + } + {{#joda}} /** * Gson TypeAdapter for Joda DateTime type diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache index 0b3d148d8654..fc67b62d13e9 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache @@ -105,6 +105,7 @@ ext { {{#threetenbp}} threetenbp_version = "1.3.5" {{/threetenbp}} + okio_version = "1.13.0" } dependencies { @@ -118,5 +119,6 @@ dependencies { {{#threetenbp}} compile "org.threeten:threetenbp:$threetenbp_version" {{/threetenbp}} + compile "com.squareup.okio:okio:$okio_version" testCompile "junit:junit:$junit_version" } diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache index da24a8fdd8c8..289345c3a0a0 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache @@ -10,7 +10,7 @@ lazy val root = (project in file(".")). resolvers += Resolver.mavenLocal, libraryDependencies ++= Seq( "io.swagger" % "swagger-annotations" % "1.5.15", - "io.rest-assured" % "scala-support" % "3.0.6" + "io.rest-assured" % "scala-support" % "3.0.6", "com.google.code.gson" % "gson" % "2.6.1", "io.gsonfire" % "gson-fire" % "1.8.2" % "compile", {{#joda}} @@ -19,6 +19,7 @@ lazy val root = (project in file(".")). {{#threetenbp}} "org.threeten" % "threetenbp" % "1.3.5" % "compile", {{/threetenbp}} + "com.squareup.okio" % "okio" % "1.13.0" % "compile", "junit" % "junit" % "4.12" % "test", "com.novocode" % "junit-interface" % "0.10" % "test" ) diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache index 086980646476..80f043340c2d 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache @@ -212,24 +212,29 @@ ${gson-version} {{#joda}} - - joda-time - joda-time - ${jodatime-version} - + + joda-time + joda-time + ${jodatime-version} + {{/joda}} {{#threetenbp}} - - org.threeten - threetenbp - ${threetenbp-version} - + + org.threeten + threetenbp + ${threetenbp-version} + {{/threetenbp}} io.gsonfire gson-fire ${gson-fire-version} + + com.squareup.okio + okio + ${okio-version} + junit @@ -251,6 +256,7 @@ {{#threetenbp}} 1.3.5 {{/threetenbp}} + 1.13.0 4.12 diff --git a/samples/client/petstore/java/rest-assured/build.gradle b/samples/client/petstore/java/rest-assured/build.gradle index 7f6177babb85..37767811dab9 100644 --- a/samples/client/petstore/java/rest-assured/build.gradle +++ b/samples/client/petstore/java/rest-assured/build.gradle @@ -100,6 +100,7 @@ ext { gson_version = "2.6.1" gson_fire_version = "1.8.2" threetenbp_version = "1.3.5" + okio_version = "1.13.0" } dependencies { @@ -107,5 +108,6 @@ dependencies { compile "io.rest-assured:scala-support:$rest_assured_version" compile "io.gsonfire:gson-fire:$gson_fire_version" compile "org.threeten:threetenbp:$threetenbp_version" + compile "com.squareup.okio:okio:$okio_version" testCompile "junit:junit:$junit_version" } diff --git a/samples/client/petstore/java/rest-assured/build.sbt b/samples/client/petstore/java/rest-assured/build.sbt index 13a2cedb077c..d4f15d414928 100644 --- a/samples/client/petstore/java/rest-assured/build.sbt +++ b/samples/client/petstore/java/rest-assured/build.sbt @@ -10,10 +10,11 @@ lazy val root = (project in file(".")). resolvers += Resolver.mavenLocal, libraryDependencies ++= Seq( "io.swagger" % "swagger-annotations" % "1.5.15", - "io.rest-assured" % "scala-support" % "3.0.6" + "io.rest-assured" % "scala-support" % "3.0.6", "com.google.code.gson" % "gson" % "2.6.1", "io.gsonfire" % "gson-fire" % "1.8.2" % "compile", "org.threeten" % "threetenbp" % "1.3.5" % "compile", + "com.squareup.okio" % "okio" % "1.13.0" % "compile", "junit" % "junit" % "4.12" % "test", "com.novocode" % "junit-interface" % "0.10" % "test" ) diff --git a/samples/client/petstore/java/rest-assured/pom.xml b/samples/client/petstore/java/rest-assured/pom.xml index 511e863f081f..7d5b031a0836 100644 --- a/samples/client/petstore/java/rest-assured/pom.xml +++ b/samples/client/petstore/java/rest-assured/pom.xml @@ -211,16 +211,21 @@ gson ${gson-version} - - org.threeten - threetenbp - ${threetenbp-version} - + + org.threeten + threetenbp + ${threetenbp-version} + io.gsonfire gson-fire ${gson-fire-version} + + com.squareup.okio + okio + ${okio-version} + junit @@ -237,6 +242,7 @@ 1.8.2 1.0.0 1.3.5 + 1.13.0 4.12 diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java index df84d970239b..15ce43f658ae 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java @@ -28,6 +28,7 @@ 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 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 T deserialize(String body, Type returnType) { } } + /** + * 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 */