Skip to content

Commit e2916fd

Browse files
lukoyanovwing328
authored andcommitted
[Java] Play! framework + retrofit2 client exception converter, bug fixes (#6543)
* added exception converter * underscore removal fix * samples updated * added test * test whitespace
1 parent 0a9e378 commit e2916fd

File tree

19 files changed

+671
-7
lines changed

19 files changed

+671
-7
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3255,7 +3255,13 @@ public static String camelize(String word, boolean lowercaseFirstLetter) {
32553255
p = Pattern.compile("(_)(.)");
32563256
m = p.matcher(word);
32573257
while (m.find()) {
3258-
word = m.replaceFirst(m.group(2).toUpperCase());
3258+
String original = m.group(2);
3259+
String upperCase = original.toUpperCase();
3260+
if (original.equals(upperCase)) {
3261+
word = word.replaceFirst("_", "");
3262+
} else {
3263+
word = m.replaceFirst(upperCase);
3264+
}
32593265
m = p.matcher(word);
32603266
}
32613267

modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/play25/Play25CallAdapterFactory.mustache

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ import java.lang.reflect.Type;
99
import java.lang.reflect.WildcardType;
1010
import java.util.concurrent.CompletableFuture;
1111
import java.util.concurrent.CompletionStage;
12+
import java.util.function.Function;
1213

1314
/**
1415
* Creates {@link CallAdapter} instances that convert {@link Call} into {@link java.util.concurrent.CompletionStage}
1516
*/
1617
public class Play25CallAdapterFactory extends CallAdapter.Factory {
1718
19+
private Function<RuntimeException, RuntimeException> exceptionConverter = Function.identity();
20+
21+
public Play25CallAdapterFactory() {
22+
}
23+
24+
public Play25CallAdapterFactory(
25+
Function<RuntimeException, RuntimeException> exceptionConverter) {
26+
this.exceptionConverter = exceptionConverter;
27+
}
28+
1829
@Override
1930
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
2031
if (!(returnType instanceof ParameterizedType)) {
@@ -49,7 +60,7 @@ public class Play25CallAdapterFactory extends CallAdapter.Factory {
4960
includeResponse = true;
5061
}
5162

52-
return new ValueAdapter(resultType, includeResponse);
63+
return new ValueAdapter(resultType, includeResponse, exceptionConverter);
5364
}
5465

5566
/**
@@ -59,10 +70,13 @@ public class Play25CallAdapterFactory extends CallAdapter.Factory {
5970
6071
private final Type responseType;
6172
private final boolean includeResponse;
73+
private Function<RuntimeException, RuntimeException> exceptionConverter;
6274
63-
ValueAdapter(Type responseType, boolean includeResponse) {
75+
ValueAdapter(Type responseType, boolean includeResponse,
76+
Function<RuntimeException, RuntimeException> exceptionConverter) {
6477
this.responseType = responseType;
6578
this.includeResponse = includeResponse;
79+
this.exceptionConverter = exceptionConverter;
6680
}
6781

6882
@Override
@@ -85,7 +99,7 @@ public class Play25CallAdapterFactory extends CallAdapter.Factory {
8599
promise.complete(response.body());
86100
}
87101
} else {
88-
promise.completeExceptionally(new HttpException(response));
102+
promise.completeExceptionally(exceptionConverter.apply(new HttpException(response)));
89103
}
90104
}
91105

modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public void camelizeNamesTest() {
3333
Assert.assertEquals(codegen.camelize(".foo"), "Foo");
3434
Assert.assertEquals(codegen.camelize(".foo.bar"), "FooBar");
3535
Assert.assertEquals(codegen.camelize("foo$bar"), "Foo$bar");
36+
Assert.assertEquals(codegen.camelize("foo_$bar"), "Foo$bar");
37+
3638
Assert.assertEquals(codegen.camelize("foo_bar"), "FooBar");
3739
Assert.assertEquals(codegen.camelize("foo_bar_baz"), "FooBarBaz");
3840
Assert.assertEquals(codegen.camelize("foo/bar.baz"), "FooBarBaz");
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# AnotherFakeApi
2+
3+
All URIs are relative to *http://petstore.swagger.io:80/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**testSpecialTags**](AnotherFakeApi.md#testSpecialTags) | **PATCH** another-fake/dummy | To test special tags
8+
9+
10+
<a name="testSpecialTags"></a>
11+
# **testSpecialTags**
12+
> Client testSpecialTags(body)
13+
14+
To test special tags
15+
16+
To test special tags
17+
18+
### Example
19+
```java
20+
// Import classes:
21+
//import io.swagger.client.ApiException;
22+
//import io.swagger.client.api.AnotherFakeApi;
23+
24+
25+
AnotherFakeApi apiInstance = new AnotherFakeApi();
26+
Client body = new Client(); // Client | client model
27+
try {
28+
Client result = apiInstance.testSpecialTags(body);
29+
System.out.println(result);
30+
} catch (ApiException e) {
31+
System.err.println("Exception when calling AnotherFakeApi#testSpecialTags");
32+
e.printStackTrace();
33+
}
34+
```
35+
36+
### Parameters
37+
38+
Name | Type | Description | Notes
39+
------------- | ------------- | ------------- | -------------
40+
**body** | [**Client**](Client.md)| client model |
41+
42+
### Return type
43+
44+
[**Client**](Client.md)
45+
46+
### Authorization
47+
48+
No authorization required
49+
50+
### HTTP request headers
51+
52+
- **Content-Type**: application/json
53+
- **Accept**: application/json
54+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.CollectionFormats.*;
4+
5+
6+
7+
import retrofit2.Call;
8+
import retrofit2.http.*;
9+
10+
import okhttp3.RequestBody;
11+
12+
import io.swagger.client.model.Client;
13+
14+
import java.util.ArrayList;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import play.libs.F;
20+
import retrofit2.Response;
21+
22+
public interface AnotherFakeApi {
23+
/**
24+
* To test special tags
25+
* To test special tags
26+
* @param body client model (required)
27+
* @return Call&lt;Client&gt;
28+
*/
29+
@Headers({
30+
"Content-Type:application/json"
31+
})
32+
@PATCH("another-fake/dummy")
33+
F.Promise<Response<Client>> testSpecialTags(
34+
@retrofit2.http.Body Client body
35+
);
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.ApiClient;
4+
import io.swagger.client.model.Client;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* API tests for AnotherFakeApi
15+
*/
16+
public class AnotherFakeApiTest {
17+
18+
private AnotherFakeApi api;
19+
20+
@Before
21+
public void setup() {
22+
api = new ApiClient().createService(AnotherFakeApi.class);
23+
}
24+
25+
/**
26+
* To test special tags
27+
*
28+
* To test special tags
29+
*/
30+
@Test
31+
public void testSpecialTagsTest() {
32+
Client body = null;
33+
// Client response = api.testSpecialTags(body);
34+
35+
// TODO: test validations
36+
}
37+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# AnotherFakeApi
2+
3+
All URIs are relative to *http://petstore.swagger.io:80/v2*
4+
5+
Method | HTTP request | Description
6+
------------- | ------------- | -------------
7+
[**testSpecialTags**](AnotherFakeApi.md#testSpecialTags) | **PATCH** another-fake/dummy | To test special tags
8+
9+
10+
<a name="testSpecialTags"></a>
11+
# **testSpecialTags**
12+
> Client testSpecialTags(body)
13+
14+
To test special tags
15+
16+
To test special tags
17+
18+
### Example
19+
```java
20+
// Import classes:
21+
//import io.swagger.client.ApiException;
22+
//import io.swagger.client.api.AnotherFakeApi;
23+
24+
25+
AnotherFakeApi apiInstance = new AnotherFakeApi();
26+
Client body = new Client(); // Client | client model
27+
try {
28+
Client result = apiInstance.testSpecialTags(body);
29+
System.out.println(result);
30+
} catch (ApiException e) {
31+
System.err.println("Exception when calling AnotherFakeApi#testSpecialTags");
32+
e.printStackTrace();
33+
}
34+
```
35+
36+
### Parameters
37+
38+
Name | Type | Description | Notes
39+
------------- | ------------- | ------------- | -------------
40+
**body** | [**Client**](Client.md)| client model |
41+
42+
### Return type
43+
44+
[**Client**](Client.md)
45+
46+
### Authorization
47+
48+
No authorization required
49+
50+
### HTTP request headers
51+
52+
- **Content-Type**: application/json
53+
- **Accept**: application/json
54+

samples/client/petstore/java/retrofit2-play25/src/main/java/io/swagger/client/Play25CallAdapterFactory.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@
99
import java.lang.reflect.WildcardType;
1010
import java.util.concurrent.CompletableFuture;
1111
import java.util.concurrent.CompletionStage;
12+
import java.util.function.Function;
1213

1314
/**
1415
* Creates {@link CallAdapter} instances that convert {@link Call} into {@link java.util.concurrent.CompletionStage}
1516
*/
1617
public class Play25CallAdapterFactory extends CallAdapter.Factory {
1718

19+
private Function<RuntimeException, RuntimeException> exceptionConverter = Function.identity();
20+
21+
public Play25CallAdapterFactory() {
22+
}
23+
24+
public Play25CallAdapterFactory(
25+
Function<RuntimeException, RuntimeException> exceptionConverter) {
26+
this.exceptionConverter = exceptionConverter;
27+
}
28+
1829
@Override
1930
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
2031
if (!(returnType instanceof ParameterizedType)) {
@@ -49,7 +60,7 @@ private CallAdapter<?, CompletionStage<?>> createAdapter(ParameterizedType retur
4960
includeResponse = true;
5061
}
5162

52-
return new ValueAdapter(resultType, includeResponse);
63+
return new ValueAdapter(resultType, includeResponse, exceptionConverter);
5364
}
5465

5566
/**
@@ -59,10 +70,13 @@ private static final class ValueAdapter<R> implements CallAdapter<R, CompletionS
5970

6071
private final Type responseType;
6172
private final boolean includeResponse;
73+
private Function<RuntimeException, RuntimeException> exceptionConverter;
6274

63-
ValueAdapter(Type responseType, boolean includeResponse) {
75+
ValueAdapter(Type responseType, boolean includeResponse,
76+
Function<RuntimeException, RuntimeException> exceptionConverter) {
6477
this.responseType = responseType;
6578
this.includeResponse = includeResponse;
79+
this.exceptionConverter = exceptionConverter;
6680
}
6781

6882
@Override
@@ -85,7 +99,7 @@ public void onResponse(Call<R> call, Response<R> response) {
8599
promise.complete(response.body());
86100
}
87101
} else {
88-
promise.completeExceptionally(new HttpException(response));
102+
promise.completeExceptionally(exceptionConverter.apply(new HttpException(response)));
89103
}
90104
}
91105

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.CollectionFormats.*;
4+
5+
6+
7+
import retrofit2.Call;
8+
import retrofit2.http.*;
9+
10+
import okhttp3.RequestBody;
11+
12+
import io.swagger.client.model.Client;
13+
14+
import java.util.ArrayList;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import java.util.concurrent.*;
20+
import retrofit2.Response;
21+
22+
public interface AnotherFakeApi {
23+
/**
24+
* To test special tags
25+
* To test special tags
26+
* @param body client model (required)
27+
* @return Call&lt;Client&gt;
28+
*/
29+
@Headers({
30+
"Content-Type:application/json"
31+
})
32+
@PATCH("another-fake/dummy")
33+
CompletionStage<Response<Client>> testSpecialTags(
34+
@retrofit2.http.Body Client body
35+
);
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.swagger.client.api;
2+
3+
import io.swagger.client.ApiClient;
4+
import io.swagger.client.model.Client;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* API tests for AnotherFakeApi
15+
*/
16+
public class AnotherFakeApiTest {
17+
18+
private AnotherFakeApi api;
19+
20+
@Before
21+
public void setup() {
22+
api = new ApiClient().createService(AnotherFakeApi.class);
23+
}
24+
25+
/**
26+
* To test special tags
27+
*
28+
* To test special tags
29+
*/
30+
@Test
31+
public void testSpecialTagsTest() {
32+
Client body = null;
33+
// Client response = api.testSpecialTags(body);
34+
35+
// TODO: test validations
36+
}
37+
}

0 commit comments

Comments
 (0)