Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.

Commit 8ba6e6d

Browse files
committed
Added snippet option
1 parent d57d2ac commit 8ba6e6d

File tree

4 files changed

+75
-22
lines changed

4 files changed

+75
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
*.iml
33
target
4-
dependency-reduced-pom.xml
4+
dependency-reduced-pom.xml
5+
snippet.java

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ Parameter | Description | Default
1717
--group-id [groupId] | Maven Group Id | Package Name (see --package)
1818
--artifact-id [artifactId] | Maven Artifact Id | API Name (see --api-name)
1919
--version [version] | Maven Artifact Version | `version` from API Info (1.0 if not present)
20+
--snippet [file] | A snippet that will be added to the client (multiple allowed) | None

src/main/java/org/javawebstack/openapi/client/JavaClientGenerator.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class JavaClientGenerator {
4040
String groupId;
4141
String artifactId;
4242
String version;
43+
List<String> snippet = new ArrayList<>();
4344

4445
public JavaClientGenerator(OpenAPI api){
4546
this.api = api;
@@ -148,7 +149,12 @@ private void generateClient(File targetFolder){
148149
.append("Tag(this);\n")
149150
);
150151
sb
151-
.append(" }\n\n")
152+
.append(" }\n\n");
153+
if(snippet != null && snippet.size() > 0){
154+
snippet.forEach(l -> sb.append(" ").append(l).append("\n"));
155+
sb.append("\n");
156+
}
157+
sb
152158
.append(" public <T> T orError(HTTPRequest request, Class<T> type) throws ")
153159
.append(apiName)
154160
.append("Exception {\n")
@@ -253,18 +259,24 @@ private void generateTag(File targetFolder, String tag){
253259
parameters.addAll(pathObject.getParameters());
254260
if(operation.getParameters() != null)
255261
parameters.addAll(operation.getParameters());
256-
String returnType = getJavaType(
257-
getResponseSchema(
258-
operation
259-
.getResponses()
260-
.entrySet()
261-
.stream()
262-
.filter(e -> e.getKey().startsWith("2"))
263-
.map(Map.Entry::getValue)
264-
.findFirst()
265-
.orElse(null)
266-
)
267-
);
262+
OpenAPIResponse response = operation
263+
.getResponses()
264+
.entrySet()
265+
.stream()
266+
.filter(e -> e.getKey().startsWith("2"))
267+
.map(Map.Entry::getValue)
268+
.findFirst()
269+
.orElse(null);
270+
String returnType;
271+
if(response != null && response.getReference() != null){
272+
returnType = response.getReference().split("/")[3];
273+
}else{
274+
returnType = getJavaType(
275+
getResponseSchema(
276+
response
277+
)
278+
);
279+
}
268280
List<String> methodParams = new ArrayList<>();
269281
String url = "\""+path+"\"";
270282
for(OpenAPIParameter p : parameters.stream().filter(p -> p.getIn() == OpenAPIParameter.Location.PATH).collect(Collectors.toList())){
@@ -326,7 +338,7 @@ private void generateSchema(File targetFolder, String name, OpenAPISchema schema
326338
.append(basePackage)
327339
.append(".schemas;\n\n")
328340
.append("import com.google.gson.annotations.SerializedName;\n\n");
329-
generateSchema(sb, "", name, schema);
341+
generateSchema(sb, "", name, schema, new ArrayList<>());
330342
writeClassFile(targetFolder, basePackage+".schemas."+name, sb.toString());
331343
}
332344

@@ -345,18 +357,20 @@ private void generateResponse(File targetFolder, String name, OpenAPIResponse re
345357
OpenAPISchema schema = getResponseSchema(response);
346358
if(schema == null)
347359
return;
348-
generateSchema(sb, "", name, schema);
360+
generateSchema(sb, "", name, schema, new ArrayList<>());
349361
writeClassFile(targetFolder, basePackage+".responses."+name, sb.toString());
350362
}
351363

352-
private void generateSchema(StringBuilder sb, String intendation, String name, OpenAPISchema schema){
364+
private void generateSchema(StringBuilder sb, String intendation, String name, OpenAPISchema schema, List<String> usedNames){
353365
Map<String, String> types = new HashMap<>();
354366
Map<String, OpenAPISchema> subSchemas = new HashMap<>();
355367
Map<String, String> serializedNames = new HashMap<>();
356368
if(schema.getProperties() != null){
357369
schema.getProperties().forEach((pName, pSchema) -> {
358370
String type = getJavaType(pSchema);
359-
if(type == null){
371+
if(type == null)
372+
type = "null";
373+
if(type.startsWith("null")){
360374
String subName = capitalize(pName);
361375
if(subName.endsWith("ies")){
362376
subName = subName.substring(0, subName.length()-3)+"y";
@@ -365,8 +379,15 @@ private void generateSchema(StringBuilder sb, String intendation, String name, O
365379
}else if(subName.endsWith("s")) {
366380
subName = subName.substring(0, subName.length() - 1);
367381
}
368-
subSchemas.put(subName, pSchema);
369-
type = subName;
382+
int i=0;
383+
String orig = subName;
384+
while (usedNames.contains(subName)){
385+
i++;
386+
subName = orig + i;
387+
}
388+
usedNames.add(subName);
389+
subSchemas.put(subName, pSchema.getType() == OpenAPIDataType.ARRAY ? pSchema.getItems() : pSchema);
390+
type = subName+type.substring(4);
370391
}
371392
String sName = getSerializedName(pName, type);
372393
if(sName != null){
@@ -443,7 +464,7 @@ private void generateSchema(StringBuilder sb, String intendation, String name, O
443464
}
444465
});
445466
if(subSchemas.size()>0){
446-
subSchemas.forEach((subName, subSchema) -> generateSchema(sb, intendation+" ", subName, subSchema));
467+
subSchemas.forEach((subName, subSchema) -> generateSchema(sb, intendation+" ", subName, subSchema, usedNames));
447468
sb.append("\n");
448469
}
449470
sb.append(intendation).append("}");
@@ -510,7 +531,8 @@ private String getJavaType(OpenAPISchema schema){
510531
return elementType+"[]";
511532
}
512533
case OBJECT: {
513-
return "com.google.gson.JsonObject";
534+
return null;
535+
//return "com.google.gson.JsonObject";
514536
}
515537
}
516538
return null;

src/main/java/org/javawebstack/openapi/client/command/JavaCommand.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import org.javawebstack.openapi.client.JavaClientGenerator;
77
import org.javawebstack.openapi.parser.OpenAPI;
88

9+
import java.io.ByteArrayOutputStream;
910
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.ArrayList;
15+
import java.util.Arrays;
1016
import java.util.List;
1117
import java.util.Map;
1218

@@ -21,6 +27,14 @@ public CommandResult execute(CommandSystem system, List<String> args, Map<String
2127
return CommandResult.error("The spec file isn't readable");
2228
JavaClientGenerator clientGenerator = new JavaClientGenerator(api);
2329

30+
if(params.containsKey("snippet")){
31+
for(String name : params.get("snippet")){
32+
File snippetFile = new File(name);
33+
if(!snippetFile.exists())
34+
return CommandResult.error("Snippet '"+name+"' not found");
35+
clientGenerator.getSnippet().addAll(readFile(snippetFile));
36+
}
37+
}
2438
if(params.containsKey("s"))
2539
clientGenerator.setJustSource(true);
2640
if(params.containsKey("artifact-id"))
@@ -42,4 +56,19 @@ public CommandResult execute(CommandSystem system, List<String> args, Map<String
4256
return CommandResult.success();
4357
}
4458

59+
private static List<String> readFile(File file){
60+
try {
61+
FileInputStream fis = new FileInputStream(file);
62+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
63+
byte[] buffer = new byte[1024];
64+
int r;
65+
while ((r = fis.read(buffer)) != -1)
66+
baos.write(buffer, 0, r);
67+
fis.close();
68+
return Arrays.asList(new String(baos.toByteArray(), StandardCharsets.UTF_8).replace("\r", "").split("\n"));
69+
}catch (IOException ignored){
70+
return new ArrayList<>();
71+
}
72+
}
73+
4574
}

0 commit comments

Comments
 (0)