Skip to content

Commit 8bf042b

Browse files
committed
updated sample with form param support
1 parent 8325a12 commit 8bf042b

File tree

9 files changed

+297
-133
lines changed

9 files changed

+297
-133
lines changed

samples/client/petstore/java/JavaPetstoreCodegen.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object JavaPetstoreCodegen extends BasicJavaGenerator {
2020
def main(args: Array[String]) = generateClient(args)
2121

2222
// location of templates
23-
override def templateDir = "Java"
23+
override def templateDir = "src/main/resources/Java"
2424

2525
// where to write generated code
2626
override def destinationDir = "samples/client/petstore/java/src/main/java"

samples/client/petstore/java/pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.wordnik.client</groupId>
5-
<artifactId>api-client</artifactId>
4+
<groupId>com.wordnik</groupId>
5+
<artifactId>swagger-petstore</artifactId>
66
<packaging>jar</packaging>
7-
<name>api-client</name>
7+
<name>swagger-petstore</name>
88
<version>1.0.0</version>
99
<scm>
1010
<connection>scm:git:[email protected]:wordnik/swagger-mustache.git</connection>
@@ -178,6 +178,17 @@
178178
<version>${jackson-version}</version>
179179
<scope>compile</scope>
180180
</dependency>
181+
<dependency>
182+
<groupId>com.fasterxml.jackson.datatype</groupId>
183+
<artifactId>jackson-datatype-joda</artifactId>
184+
<version>2.1.5</version>
185+
</dependency>
186+
<dependency>
187+
<groupId>joda-time</groupId>
188+
<artifactId>joda-time</artifactId>
189+
<version>${jodatime-version}</version>
190+
<scope>compile</scope>
191+
</dependency>
181192

182193
<!-- test dependencies -->
183194
<dependency>
@@ -203,6 +214,7 @@
203214
<properties>
204215
<jersey-version>1.7</jersey-version>
205216
<jackson-version>2.1.4</jackson-version>
217+
<jodatime-version>2.3</jodatime-version>
206218
<scala-version>2.9.1-1</scala-version>
207219
<junit-version>4.8.1</junit-version>
208220
<maven-plugin-version>1.0.0</maven-plugin-version>

samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.io.IOException;
22+
import java.net.URLEncoder;
23+
import java.io.UnsupportedEncodingException;
2224

2325
public class ApiInvoker {
2426
private static ApiInvoker INSTANCE = new ApiInvoker();
@@ -34,7 +36,12 @@ public void addDefaultHeader(String key, String value) {
3436
}
3537

3638
public String escapeString(String str) {
37-
return str;
39+
try{
40+
return URLEncoder.encode(str, "utf8").replaceAll("\\+", "%20");
41+
}
42+
catch(UnsupportedEncodingException e) {
43+
return str;
44+
}
3845
}
3946

4047
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
@@ -71,7 +78,7 @@ public static String serialize(Object obj) throws ApiException {
7178
}
7279
}
7380

74-
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, String contentType) throws ApiException {
81+
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, Map<String, String> formParams, String contentType) throws ApiException {
7582
Client client = getClient(host);
7683

7784
StringBuilder b = new StringBuilder();
@@ -107,19 +114,41 @@ else if ("POST".equals(method)) {
107114
if(body == null)
108115
response = builder.post(ClientResponse.class, serialize(body));
109116
else
110-
response = builder.type("application/json").post(ClientResponse.class, serialize(body));
117+
response = builder.type(contentType).post(ClientResponse.class, serialize(body));
111118
}
112119
else if ("PUT".equals(method)) {
113120
if(body == null)
114121
response = builder.put(ClientResponse.class, serialize(body));
115-
else
116-
response = builder.type("application/json").put(ClientResponse.class, serialize(body));
122+
else {
123+
if("application/x-www-form-urlencoded".equals(contentType)) {
124+
StringBuilder formParamBuilder = new StringBuilder();
125+
126+
// encode the form params
127+
for(String key : headerParams.keySet()) {
128+
String value = headerParams.get(key);
129+
if(value != null && !"".equals(value.trim())) {
130+
if(formParamBuilder.length() > 0) {
131+
formParamBuilder.append("&");
132+
}
133+
try {
134+
formParamBuilder.append(URLEncoder.encode(key, "utf8")).append("=").append(URLEncoder.encode(value, "utf8"));
135+
}
136+
catch (Exception e) {
137+
// move on to next
138+
}
139+
}
140+
}
141+
response = builder.type(contentType).put(ClientResponse.class, formParamBuilder.toString());
142+
}
143+
else
144+
response = builder.type(contentType).put(ClientResponse.class, serialize(body));
145+
}
117146
}
118147
else if ("DELETE".equals(method)) {
119148
if(body == null)
120149
response = builder.delete(ClientResponse.class, serialize(body));
121150
else
122-
response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
151+
response = builder.type(contentType).delete(ClientResponse.class, serialize(body));
123152
}
124153
else {
125154
throw new ApiException(500, "unknown method type " + method);

samples/client/petstore/java/src/main/java/com/wordnik/client/JsonUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
66
import com.fasterxml.jackson.core.JsonGenerator.Feature;
77

8+
import com.fasterxml.jackson.datatype.joda.*;
9+
810
public class JsonUtil {
911
public static ObjectMapper mapper;
1012

1113
static {
1214
mapper = new ObjectMapper();
1315
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1416
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
17+
mapper.registerModule(new JodaModule());
1518
}
1619

1720
public static ObjectMapper getJsonMapper() {

samples/client/petstore/java/src/main/java/com/wordnik/petstore/api/PetApi.java

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,6 @@ public String getBasePath() {
2222
return basePath;
2323
}
2424

25-
public void uploadFile (String additionalMetadata, File body) throws ApiException {
26-
// create path and map variables
27-
String path = "/pet/uploadImage".replaceAll("\\{format\\}","json");
28-
29-
// query params
30-
Map<String, String> queryParams = new HashMap<String, String>();
31-
Map<String, String> headerParams = new HashMap<String, String>();
32-
33-
String contentType = "application/json";
34-
35-
try {
36-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
37-
if(response != null){
38-
return ;
39-
}
40-
else {
41-
return ;
42-
}
43-
} catch (ApiException ex) {
44-
if(ex.getCode() == 404) {
45-
return ;
46-
}
47-
else {
48-
throw ex;
49-
}
50-
}
51-
}
5225
public Pet getPetById (Long petId) throws ApiException {
5326
// verify required params are set
5427
if(petId == null ) {
@@ -60,11 +33,15 @@ public Pet getPetById (Long petId) throws ApiException {
6033
// query params
6134
Map<String, String> queryParams = new HashMap<String, String>();
6235
Map<String, String> headerParams = new HashMap<String, String>();
36+
Map<String, String> formParams = new HashMap<String, String>();
6337

64-
String contentType = "application/json";
38+
String[] contentTypes = {
39+
"application/json"};
40+
41+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
6542

6643
try {
67-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
44+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
6845
if(response != null){
6946
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
7047
}
@@ -91,11 +68,15 @@ public void deletePet (String petId) throws ApiException {
9168
// query params
9269
Map<String, String> queryParams = new HashMap<String, String>();
9370
Map<String, String> headerParams = new HashMap<String, String>();
71+
Map<String, String> formParams = new HashMap<String, String>();
72+
73+
String[] contentTypes = {
74+
"application/json"};
9475

95-
String contentType = "application/json";
76+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
9677

9778
try {
98-
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType);
79+
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, formParams, contentType);
9980
if(response != null){
10081
return ;
10182
}
@@ -122,11 +103,15 @@ public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
122103
// query params
123104
Map<String, String> queryParams = new HashMap<String, String>();
124105
Map<String, String> headerParams = new HashMap<String, String>();
106+
Map<String, String> formParams = new HashMap<String, String>();
125107

126-
String contentType = "application/json";
108+
String[] contentTypes = {
109+
"application/json","application/xml"};
110+
111+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
127112

128113
try {
129-
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, contentType);
114+
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, formParams, contentType);
130115
if(response != null){
131116
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
132117
}
@@ -153,11 +138,49 @@ public void updatePetWithForm (String petId, String name, String status) throws
153138
// query params
154139
Map<String, String> queryParams = new HashMap<String, String>();
155140
Map<String, String> headerParams = new HashMap<String, String>();
141+
Map<String, String> formParams = new HashMap<String, String>();
142+
143+
formParams.put("name", name);
144+
formParams.put("status", status);
145+
String[] contentTypes = {
146+
"application/x-www-form-urlencoded"};
156147

157-
String contentType = "application/json";
148+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
158149

159150
try {
160-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType);
151+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, formParams, contentType);
152+
if(response != null){
153+
return ;
154+
}
155+
else {
156+
return ;
157+
}
158+
} catch (ApiException ex) {
159+
if(ex.getCode() == 404) {
160+
return ;
161+
}
162+
else {
163+
throw ex;
164+
}
165+
}
166+
}
167+
public void uploadFile (String additionalMetadata, File body) throws ApiException {
168+
// create path and map variables
169+
String path = "/pet/uploadImage".replaceAll("\\{format\\}","json");
170+
171+
// query params
172+
Map<String, String> queryParams = new HashMap<String, String>();
173+
Map<String, String> headerParams = new HashMap<String, String>();
174+
Map<String, String> formParams = new HashMap<String, String>();
175+
176+
formParams.put("additionalMetadata", additionalMetadata);
177+
String[] contentTypes = {
178+
"multipart/form-data"};
179+
180+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
181+
182+
try {
183+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType);
161184
if(response != null){
162185
return ;
163186
}
@@ -184,11 +207,15 @@ public void addPet (Pet body) throws ApiException {
184207
// query params
185208
Map<String, String> queryParams = new HashMap<String, String>();
186209
Map<String, String> headerParams = new HashMap<String, String>();
210+
Map<String, String> formParams = new HashMap<String, String>();
211+
212+
String[] contentTypes = {
213+
"application/json","application/xml"};
187214

188-
String contentType = "application/json";
215+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
189216

190217
try {
191-
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
218+
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, formParams, contentType);
192219
if(response != null){
193220
return ;
194221
}
@@ -215,11 +242,15 @@ public void updatePet (Pet body) throws ApiException {
215242
// query params
216243
Map<String, String> queryParams = new HashMap<String, String>();
217244
Map<String, String> headerParams = new HashMap<String, String>();
245+
Map<String, String> formParams = new HashMap<String, String>();
218246

219-
String contentType = "application/json";
247+
String[] contentTypes = {
248+
"application/json"};
249+
250+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
220251

221252
try {
222-
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType);
253+
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, formParams, contentType);
223254
if(response != null){
224255
return ;
225256
}
@@ -246,13 +277,17 @@ public List<Pet> findPetsByStatus (String status) throws ApiException {
246277
// query params
247278
Map<String, String> queryParams = new HashMap<String, String>();
248279
Map<String, String> headerParams = new HashMap<String, String>();
280+
Map<String, String> formParams = new HashMap<String, String>();
249281

250282
if(!"null".equals(String.valueOf(status)))
251283
queryParams.put("status", String.valueOf(status));
252-
String contentType = "application/json";
284+
String[] contentTypes = {
285+
"application/json"};
286+
287+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
253288

254289
try {
255-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
290+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
256291
if(response != null){
257292
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
258293
}
@@ -279,13 +314,17 @@ public List<Pet> findPetsByTags (String tags) throws ApiException {
279314
// query params
280315
Map<String, String> queryParams = new HashMap<String, String>();
281316
Map<String, String> headerParams = new HashMap<String, String>();
317+
Map<String, String> formParams = new HashMap<String, String>();
282318

283319
if(!"null".equals(String.valueOf(tags)))
284320
queryParams.put("tags", String.valueOf(tags));
285-
String contentType = "application/json";
321+
String[] contentTypes = {
322+
"application/json"};
323+
324+
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
286325

287326
try {
288-
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType);
327+
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, formParams, contentType);
289328
if(response != null){
290329
return (List<Pet>) ApiInvoker.deserialize(response, "Array", Pet.class);
291330
}

0 commit comments

Comments
 (0)