28
28
29
29
import static org .openapitools .codegen .TestUtils .assertFileContains ;
30
30
import static org .openapitools .codegen .TestUtils .validateJavaSourceFiles ;
31
+ import static org .openapitools .codegen .languages .AbstractJavaCodegen .JAVA8_MODE ;
32
+ import static org .openapitools .codegen .languages .AbstractJavaJAXRSServerCodegen .USE_TAGS ;
33
+ import static org .openapitools .codegen .languages .JavaJAXRSSpecServerCodegen .INTERFACE_ONLY ;
34
+ import static org .openapitools .codegen .languages .JavaJAXRSSpecServerCodegen .SUPPORT_ASYNC ;
35
+ import static org .openapitools .codegen .languages .JavaJAXRSSpecServerCodegen .RETURN_RESPONSE ;
31
36
import static org .testng .Assert .assertTrue ;
32
37
33
38
/**
@@ -95,6 +100,8 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
95
100
codegen .additionalProperties ().put (CodegenConstants .INVOKER_PACKAGE , "xyz.yyyyy.iiii.invoker" );
96
101
codegen .additionalProperties ().put ("serverPort" , "8088" );
97
102
codegen .additionalProperties ().put (JavaJAXRSSpecServerCodegen .OPEN_API_SPEC_FILE_LOCATION , "openapi.yml" );
103
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true );
104
+ codegen .additionalProperties ().put (JAVA8_MODE , false );
98
105
codegen .processOpts ();
99
106
100
107
OpenAPI openAPI = new OpenAPI ();
@@ -112,6 +119,8 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
112
119
Assert .assertEquals (codegen .additionalProperties ().get (AbstractJavaJAXRSServerCodegen .SERVER_PORT ), "8088" );
113
120
Assert .assertEquals (codegen .getOpenApiSpecFileLocation (), "openapi.yml" );
114
121
Assert .assertEquals (codegen .additionalProperties ().get (JavaJAXRSSpecServerCodegen .OPEN_API_SPEC_FILE_LOCATION ), "openapi.yml" );
122
+ Assert .assertEquals (codegen .additionalProperties ().get (SUPPORT_ASYNC ), "true" );
123
+ Assert .assertEquals (codegen .additionalProperties ().get (JAVA8_MODE ), true ); //overridden by supportAsync=true
115
124
}
116
125
117
126
/**
@@ -419,4 +428,170 @@ public void addsImportForSetResponse() throws IOException {
419
428
420
429
assertFileContains (path , "\n import java.util.Set;\n " );
421
430
}
431
+
432
+ @ Test
433
+ public void generateApiWithAsyncSupport () throws Exception {
434
+ final File output = Files .createTempDirectory ("test" ).toFile ();
435
+ output .deleteOnExit ();
436
+
437
+ final OpenAPI openAPI = new OpenAPIParser ()
438
+ .readLocation ("src/test/resources/3_0/ping.yaml" , null , new ParseOptions ()).getOpenAPI ();
439
+
440
+ codegen .setOutputDir (output .getAbsolutePath ());
441
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true ); //Given support async is enabled
442
+
443
+ final ClientOptInput input = new ClientOptInput ()
444
+ .openAPI (openAPI )
445
+ .config (codegen ); //Using JavaJAXRSSpecServerCodegen
446
+
447
+ final DefaultGenerator generator = new DefaultGenerator ();
448
+ final List <File > files = generator .opts (input ).generate (); //When generating files
449
+
450
+ //Then the java files are compilable
451
+ validateJavaSourceFiles (files );
452
+
453
+ //And the generated class contains CompletionStage<Response>
454
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/PingApi.java" );
455
+ assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/PingApi.java" ),
456
+ "\n import java.util.concurrent.CompletionStage;\n " ,
457
+ "\n import java.util.concurrent.CompletableFuture;\n " ,
458
+ "\n public CompletionStage<Response> pingGet() {\n " ,
459
+ "\n CompletableFuture.supplyAsync(() -> Response.ok().entity(\" magic!\" ).build())\n "
460
+ );
461
+ }
462
+
463
+ @ Test
464
+ public void generateApiWithAsyncSupportAndInterfaceOnly () throws Exception {
465
+ final File output = Files .createTempDirectory ("test" ).toFile ().getCanonicalFile ();
466
+ output .deleteOnExit ();
467
+
468
+ final OpenAPI openAPI = new OpenAPIParser ()
469
+ .readLocation ("src/test/resources/3_0/ping.yaml" , null , new ParseOptions ()).getOpenAPI ();
470
+
471
+ codegen .setOutputDir (output .getAbsolutePath ());
472
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true ); //Given support async is enabled
473
+ codegen .additionalProperties ().put (INTERFACE_ONLY , true ); //And only interfaces are generated
474
+
475
+ final ClientOptInput input = new ClientOptInput ()
476
+ .openAPI (openAPI )
477
+ .config (codegen ); //Using JavaJAXRSSpecServerCodegen
478
+
479
+ final DefaultGenerator generator = new DefaultGenerator ();
480
+ final List <File > files = generator .opts (input ).generate (); //When generating files
481
+
482
+ //Then the java files are compilable
483
+ validateJavaSourceFiles (files );
484
+
485
+ //And the generated interface contains CompletionStage<Void>
486
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/PingApi.java" );
487
+ assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/PingApi.java" ),
488
+ "\n import java.util.concurrent.CompletionStage;\n " ,
489
+ "\n CompletionStage<Void> pingGet();\n " );
490
+ }
491
+
492
+ @ Test
493
+ public void generateApiWithAsyncSupportAndInterfaceOnlyAndResponse () throws Exception {
494
+ final File output = Files .createTempDirectory ("test" ).toFile ().getCanonicalFile ();
495
+ output .deleteOnExit ();
496
+
497
+ final OpenAPI openAPI = new OpenAPIParser ()
498
+ .readLocation ("src/test/resources/3_0/ping.yaml" , null , new ParseOptions ()).getOpenAPI ();
499
+
500
+ codegen .setOutputDir (output .getAbsolutePath ());
501
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true ); //Given support async is enabled
502
+ codegen .additionalProperties ().put (INTERFACE_ONLY , true ); //And only interfaces are generated
503
+ codegen .additionalProperties ().put (RETURN_RESPONSE , true ); //And return type is Response
504
+
505
+ final ClientOptInput input = new ClientOptInput ()
506
+ .openAPI (openAPI )
507
+ .config (codegen ); //Using JavaJAXRSSpecServerCodegen
508
+
509
+ final DefaultGenerator generator = new DefaultGenerator ();
510
+ final List <File > files = generator .opts (input ).generate (); //When generating files
511
+
512
+ //Then the java files are compilable
513
+ validateJavaSourceFiles (files );
514
+
515
+ //And the generated interface contains CompletionStage<Response>
516
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/PingApi.java" );
517
+ assertFileContains (output .toPath ().resolve ( "src/gen/java/org/openapitools/api/PingApi.java" ),
518
+ "\n import java.util.concurrent.CompletionStage;\n " ,
519
+ "\n CompletionStage<Response> pingGet();\n " );
520
+ }
521
+
522
+
523
+ @ Test
524
+ public void generatePetstoreAPIWithAsyncSupport () throws Exception {
525
+ final File output = Files .createTempDirectory ("test" ).toFile ().getCanonicalFile ();
526
+ output .deleteOnExit ();
527
+
528
+ final OpenAPI openAPI = new OpenAPIParser ()
529
+ .readLocation ("src/test/resources/3_0/petstore.yaml" , null , new ParseOptions ()).getOpenAPI ();
530
+
531
+ codegen .setOutputDir (output .getAbsolutePath ());
532
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true ); //Given support async is enabled
533
+ codegen .additionalProperties ().put (INTERFACE_ONLY , true ); //And only interfaces are generated
534
+
535
+ final ClientOptInput input = new ClientOptInput ()
536
+ .openAPI (openAPI )
537
+ .config (codegen ); //using JavaJAXRSSpecServerCodegen
538
+
539
+ final DefaultGenerator generator = new DefaultGenerator ();
540
+ final List <File > files = generator .opts (input ).generate (); //When generating files
541
+
542
+ //Then the java files are compilable
543
+ validateJavaSourceFiles (files );
544
+
545
+ //And the generated interfaces contains CompletionStage
546
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/PetApi.java" );
547
+ assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/PetApi.java" ),
548
+ "\n import java.util.concurrent.CompletionStage;\n " ,
549
+ "CompletionStage<Void> deletePet" , //Support empty response
550
+ "CompletionStage<List<Pet>> findPetsByStatus" , //Support type of arrays response
551
+ "CompletionStage<Pet> getPetById" //Support single type response
552
+ );
553
+
554
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/StoreApi.java" );
555
+ assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/StoreApi.java" ),
556
+ "\n import java.util.concurrent.CompletionStage;\n " ,
557
+ "CompletionStage<Map<String, Integer>>" //Support map response
558
+ );
559
+
560
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/UserApi.java" );
561
+ assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/UserApi.java" ),
562
+ "\n import java.util.concurrent.CompletionStage;\n " ,
563
+ "CompletionStage<String>" //Support simple types
564
+ );
565
+ }
566
+
567
+ @ Test
568
+ public void generatePingWithAsyncSupportPrimitiveType () throws Exception {
569
+ final File output = Files .createTempDirectory ("test" ).toFile ().getCanonicalFile ();
570
+ output .deleteOnExit ();
571
+
572
+ final OpenAPI openAPI = new OpenAPIParser ()
573
+ .readLocation ("src/test/resources/3_0/issue_4832.yaml" , null , new ParseOptions ()).getOpenAPI ();
574
+
575
+ codegen .setOutputDir (output .getAbsolutePath ());
576
+ codegen .additionalProperties ().put (SUPPORT_ASYNC , true ); //Given support async is enabled
577
+ codegen .additionalProperties ().put (INTERFACE_ONLY , true ); //And only interfaces are generated
578
+ codegen .additionalProperties ().put (USE_TAGS , true ); //And use tags to generate everything in PingApi.java
579
+
580
+ final ClientOptInput input = new ClientOptInput ()
581
+ .openAPI (openAPI )
582
+ .config (codegen ); //using JavaJAXRSSpecServerCodegen
583
+
584
+ final DefaultGenerator generator = new DefaultGenerator ();
585
+ final List <File > files = generator .opts (input ).generate (); //When generating files
586
+
587
+ //Then the java files are compilable
588
+ validateJavaSourceFiles (files );
589
+
590
+ //And the generated interfaces contains CompletionStage with proper classes instead of primitive types
591
+ TestUtils .ensureContainsFile (files , output , "src/gen/java/org/openapitools/api/PingApi.java" );
592
+ TestUtils .assertFileContains (output .toPath ().resolve ("src/gen/java/org/openapitools/api/PingApi.java" ),
593
+ "CompletionStage<Boolean> pingGetBoolean" , //Support primitive types response
594
+ "CompletionStage<Integer> pingGetInteger" //Support primitive types response
595
+ );
596
+ }
422
597
}
0 commit comments