Skip to content

Commit 5047ab3

Browse files
authored
Merge pull request #3182 from cbornet/okhttp_java8
[Okhttp-gson] Add support for jsr310 dates to okhttp-gson client
2 parents 244831b + d29a553 commit 5047ab3

File tree

11 files changed

+99
-32
lines changed

11 files changed

+99
-32
lines changed

bin/java-petstore-okhttp-gson.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson -DhideGenerationTimestamp=true"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson -DhideGenerationTimestamp=true"
3030

3131
rm -rf samples/client/petstore/java/okhttp-gson/src/main
3232
find samples/client/petstore/java/okhttp-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} +

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/JSON.mustache

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ import java.io.StringReader;
2121
import java.lang.reflect.Type;
2222
import java.util.Date;
2323

24+
{{^java8}}
2425
import org.joda.time.DateTime;
2526
import org.joda.time.LocalDate;
2627
import org.joda.time.format.DateTimeFormatter;
2728
import org.joda.time.format.ISODateTimeFormat;
29+
{{/java8}}
30+
{{#java8}}
31+
import java.time.LocalDate;
32+
import java.time.OffsetDateTime;
33+
import java.time.format.DateTimeFormatter;
34+
{{/java8}}
2835

2936
public class JSON {
3037
private ApiClient apiClient;
@@ -39,7 +46,12 @@ public class JSON {
3946
this.apiClient = apiClient;
4047
gson = new GsonBuilder()
4148
.registerTypeAdapter(Date.class, new DateAdapter(apiClient))
49+
{{^java8}}
4250
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
51+
{{/java8}}
52+
{{#java8}}
53+
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeTypeAdapter())
54+
{{/java8}}
4355
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
4456
.create();
4557
}
@@ -154,6 +166,7 @@ class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
154166
}
155167
}
156168

169+
{{^java8}}
157170
/**
158171
* Gson TypeAdapter for Joda DateTime type
159172
*/
@@ -211,4 +224,67 @@ class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
211224
}
212225
}
213226
}
214-
227+
{{/java8}}
228+
{{#java8}}
229+
/**
230+
* Gson TypeAdapter for jsr310 OffsetDateTime type
231+
*/
232+
class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {
233+
234+
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
235+
236+
@Override
237+
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
238+
if (date == null) {
239+
out.nullValue();
240+
} else {
241+
out.value(formatter.format(date));
242+
}
243+
}
244+
245+
@Override
246+
public OffsetDateTime read(JsonReader in) throws IOException {
247+
switch (in.peek()) {
248+
case NULL:
249+
in.nextNull();
250+
return null;
251+
default:
252+
String date = in.nextString();
253+
if (date.endsWith("+0000")) {
254+
date = date.substring(0, date.length()-5) + "Z";
255+
}
256+
257+
return OffsetDateTime.parse(date, formatter);
258+
}
259+
}
260+
}
261+
262+
/**
263+
* Gson TypeAdapter for jsr310 LocalDate type
264+
*/
265+
class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
266+
267+
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
268+
269+
@Override
270+
public void write(JsonWriter out, LocalDate date) throws IOException {
271+
if (date == null) {
272+
out.nullValue();
273+
} else {
274+
out.value(formatter.format(date));
275+
}
276+
}
277+
278+
@Override
279+
public LocalDate read(JsonReader in) throws IOException {
280+
switch (in.peek()) {
281+
case NULL:
282+
in.nextNull();
283+
return null;
284+
default:
285+
String date = in.nextString();
286+
return LocalDate.parse(date, formatter);
287+
}
288+
}
289+
}
290+
{{/java8}}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/build.gradle.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ if(hasProperty('target') && target == 'android') {
7878
apply plugin: 'java'
7979
apply plugin: 'maven'
8080
81-
sourceCompatibility = JavaVersion.VERSION_1_7
82-
targetCompatibility = JavaVersion.VERSION_1_7
81+
sourceCompatibility = JavaVersion.VERSION_{{^java8}}1_7{{/java8}}{{#java8}}1_8{{/java8}}
82+
targetCompatibility = JavaVersion.VERSION_{{^java8}}1_7{{/java8}}{{#java8}}1_8{{/java8}}
8383

8484
install {
8585
repositories.mavenInstaller {
@@ -98,6 +98,8 @@ dependencies {
9898
compile 'com.squareup.okhttp:okhttp:2.7.5'
9999
compile 'com.squareup.okhttp:logging-interceptor:2.7.5'
100100
compile 'com.google.code.gson:gson:2.6.2'
101+
{{^java8}}
101102
compile 'joda-time:joda-time:2.9.3'
103+
{{/java8}}
102104
testCompile 'junit:junit:4.12'
103105
}

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/build.sbt.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ lazy val root = (project in file(".")).
1313
"com.squareup.okhttp" % "okhttp" % "2.7.5",
1414
"com.squareup.okhttp" % "logging-interceptor" % "2.7.5",
1515
"com.google.code.gson" % "gson" % "2.6.2",
16+
{{^java8}}
1617
"joda-time" % "joda-time" % "2.9.3" % "compile",
18+
{{/java8}}
1719
"junit" % "junit" % "4.12" % "test",
1820
"com.novocode" % "junit-interface" % "0.10" % "test"
1921
)

modules/swagger-codegen/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@
9696
</execution>
9797
</executions>
9898
</plugin>
99-
<plugin>
100-
<groupId>org.apache.maven.plugins</groupId>
101-
<artifactId>maven-compiler-plugin</artifactId>
102-
<version>2.3.2</version>
103-
<configuration>
104-
<source>1.7</source>
105-
<target>1.7</target>
106-
</configuration>
107-
</plugin>
10899
</plugins>
109100
</build>
110101
<dependencies>
@@ -128,11 +119,13 @@
128119
<artifactId>gson</artifactId>
129120
<version>${gson-version}</version>
130121
</dependency>
122+
{{^java8}}
131123
<dependency>
132124
<groupId>joda-time</groupId>
133125
<artifactId>joda-time</artifactId>
134126
<version>${jodatime-version}</version>
135-
</dependency>
127+
</dependency>
128+
{{/java8}}
136129

137130
<!-- test dependencies -->
138131
<dependency>
@@ -143,6 +136,9 @@
143136
</dependency>
144137
</dependencies>
145138
<properties>
139+
<java.version>{{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}}</java.version>
140+
<maven.compiler.source>${java.version}</maven.compiler.source>
141+
<maven.compiler.target>${java.version}</maven.compiler.target>
146142
<swagger-core-version>1.5.9</swagger-core-version>
147143
<okhttp-version>2.7.5</okhttp-version>
148144
<gson-version>2.6.2</gson-version>

samples/client/petstore/java/okhttp-gson/pom.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@
9696
</execution>
9797
</executions>
9898
</plugin>
99-
<plugin>
100-
<groupId>org.apache.maven.plugins</groupId>
101-
<artifactId>maven-compiler-plugin</artifactId>
102-
<version>2.3.2</version>
103-
<configuration>
104-
<source>1.7</source>
105-
<target>1.7</target>
106-
</configuration>
107-
</plugin>
10899
</plugins>
109100
</build>
110101
<dependencies>
@@ -132,7 +123,7 @@
132123
<groupId>joda-time</groupId>
133124
<artifactId>joda-time</artifactId>
134125
<version>${jodatime-version}</version>
135-
</dependency>
126+
</dependency>
136127

137128
<!-- test dependencies -->
138129
<dependency>
@@ -143,6 +134,9 @@
143134
</dependency>
144135
</dependencies>
145136
<properties>
137+
<java.version>1.7</java.version>
138+
<maven.compiler.source>${java.version}</maven.compiler.source>
139+
<maven.compiler.target>${java.version}</maven.compiler.target>
146140
<swagger-core-version>1.5.9</swagger-core-version>
147141
<okhttp-version>2.7.5</okhttp-version>
148142
<gson-version>2.6.2</gson-version>

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ public ApiClient() {
173173

174174
// Setup authentications (key: authentication name, value: authentication).
175175
authentications = new HashMap<String, Authentication>();
176-
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
177176
authentications.put("petstore_auth", new OAuth());
177+
authentications.put("api_key", new ApiKeyAuth("header", "api_key"));
178178
// Prevent the authentications from being modified.
179179
authentications = Collections.unmodifiableMap(authentications);
180180
}

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/JSON.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,3 @@ public LocalDate read(JsonReader in) throws IOException {
234234
}
235235
}
236236
}
237-

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/FakeApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import java.io.IOException;
4040

4141
import org.joda.time.LocalDate;
42-
import org.joda.time.DateTime;
4342
import java.math.BigDecimal;
43+
import org.joda.time.DateTime;
4444

4545
import java.lang.reflect.Type;
4646
import java.util.ArrayList;

samples/client/petstore/java/okhttp-gson/src/main/java/io/swagger/client/api/PetApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
import java.io.IOException;
4040

4141
import io.swagger.client.model.Pet;
42-
import java.io.File;
4342
import io.swagger.client.model.ModelApiResponse;
43+
import java.io.File;
4444

4545
import java.lang.reflect.Type;
4646
import java.util.ArrayList;

0 commit comments

Comments
 (0)