@@ -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 ;
0 commit comments