24
24
import io .swagger .v3 .core .util .Json ;
25
25
import io .swagger .v3 .oas .models .OpenAPI ;
26
26
import io .swagger .v3 .oas .models .Operation ;
27
+ import io .swagger .v3 .oas .models .callbacks .Callback ;
27
28
import io .swagger .v3 .oas .models .examples .Example ;
28
29
import io .swagger .v3 .oas .models .headers .Header ;
29
30
import io .swagger .v3 .oas .models .media .ArraySchema ;
50
51
import org .apache .commons .lang3 .ObjectUtils ;
51
52
import org .apache .commons .lang3 .StringEscapeUtils ;
52
53
import org .apache .commons .lang3 .StringUtils ;
54
+ import org .apache .commons .lang3 .tuple .Pair ;
53
55
import org .openapitools .codegen .CodegenDiscriminator .MappedModel ;
54
56
import org .openapitools .codegen .examples .ExampleGenerator ;
55
57
import org .openapitools .codegen .serializer .SerializerUtils ;
75
77
import java .util .TreeSet ;
76
78
import java .util .regex .Matcher ;
77
79
import java .util .regex .Pattern ;
80
+ import java .util .stream .Stream ;
78
81
import java .util .stream .Collectors ;
79
82
80
83
public class DefaultCodegen implements CodegenConfig {
@@ -2232,14 +2235,17 @@ public CodegenOperation fromOperation(String path,
2232
2235
Map <String , Schema > schemas ,
2233
2236
OpenAPI openAPI ) {
2234
2237
LOGGER .debug ("fromOperation => operation: " + operation );
2238
+ if (operation == null )
2239
+ throw new RuntimeException ("operation cannot be null in fromOperation" );
2240
+
2235
2241
CodegenOperation op = CodegenModelFactory .newInstance (CodegenModelType .OPERATION );
2236
2242
Set <String > imports = new HashSet <String >();
2237
2243
if (operation .getExtensions () != null && !operation .getExtensions ().isEmpty ()) {
2238
2244
op .vendorExtensions .putAll (operation .getExtensions ());
2239
- }
2240
2245
2241
- if (operation == null )
2242
- throw new RuntimeException ("operation cannot be null in fromOperation" );
2246
+ Object isCallbackRequest = op .vendorExtensions .remove ("x-callback-request" );
2247
+ op .isCallbackRequest = Boolean .TRUE .equals (isCallbackRequest );
2248
+ }
2243
2249
2244
2250
// store the original operationId for plug-in
2245
2251
op .operationIdOriginal = operation .getOperationId ();
@@ -2253,6 +2259,7 @@ public CodegenOperation fromOperation(String path,
2253
2259
}
2254
2260
}
2255
2261
operationId = removeNonNameElementToCamelCase (operationId );
2262
+
2256
2263
op .path = path ;
2257
2264
op .operationId = toOperationId (operationId );
2258
2265
op .summary = escapeText (operation .getSummary ());
@@ -2344,6 +2351,15 @@ public CodegenOperation fromOperation(String path,
2344
2351
}
2345
2352
}
2346
2353
2354
+ if (operation .getCallbacks () != null && !operation .getCallbacks ().isEmpty ()) {
2355
+ operation .getCallbacks ().forEach ((name , callback ) -> {
2356
+ CodegenCallback c = fromCallback (name , callback , schemas , openAPI );
2357
+ c .hasMore = true ;
2358
+ op .callbacks .add (c );
2359
+ });
2360
+ op .callbacks .get (op .callbacks .size () - 1 ).hasMore = false ;
2361
+ }
2362
+
2347
2363
List <Parameter > parameters = operation .getParameters ();
2348
2364
List <CodegenParameter > allParams = new ArrayList <CodegenParameter >();
2349
2365
List <CodegenParameter > bodyParams = new ArrayList <CodegenParameter >();
@@ -2621,6 +2637,79 @@ public CodegenResponse fromResponse(OpenAPI openAPI, String responseCode, ApiRes
2621
2637
return r ;
2622
2638
}
2623
2639
2640
+ /**
2641
+ * Convert OAS Callback object to Codegen Callback object
2642
+ *
2643
+ * @param name callback name
2644
+ * @param callback OAS Callback object
2645
+ * @param schemas a map of OAS models
2646
+ * @param openAPI a OAS object representing the spec
2647
+ * @return Codegen Response object
2648
+ */
2649
+ public CodegenCallback fromCallback (String name , Callback callback , Map <String , Schema > schemas , OpenAPI openAPI ) {
2650
+ CodegenCallback c = new CodegenCallback ();
2651
+ c .name = name ;
2652
+
2653
+ if (callback .getExtensions () != null && !callback .getExtensions ().isEmpty ()) {
2654
+ c .vendorExtensions .putAll (callback .getExtensions ());
2655
+ }
2656
+
2657
+ callback .forEach ((expression , pi ) -> {
2658
+ CodegenCallback .Url u = new CodegenCallback .Url ();
2659
+ u .expression = expression ;
2660
+ u .hasMore = true ;
2661
+
2662
+ if (pi .getExtensions () != null && !pi .getExtensions ().isEmpty ()) {
2663
+ u .vendorExtensions .putAll (pi .getExtensions ());
2664
+ }
2665
+
2666
+ Stream .of (
2667
+ Pair .of ("get" , pi .getGet ()),
2668
+ Pair .of ("head" , pi .getHead ()),
2669
+ Pair .of ("put" , pi .getPut ()),
2670
+ Pair .of ("post" , pi .getPost ()),
2671
+ Pair .of ("delete" , pi .getDelete ()),
2672
+ Pair .of ("patch" , pi .getPatch ()),
2673
+ Pair .of ("options" , pi .getOptions ()))
2674
+ .filter (p -> p .getValue () != null )
2675
+ .forEach (p -> {
2676
+ String method = p .getKey ();
2677
+ Operation op = p .getValue ();
2678
+
2679
+ boolean genId = op .getOperationId () == null ;
2680
+ if (genId ) {
2681
+ op .setOperationId (getOrGenerateOperationId (op , c .name +"_" +expression .replaceAll ("\\ {\\ $.*}" , "" ), method ));
2682
+ }
2683
+
2684
+ if (op .getExtensions () == null ) {
2685
+ op .setExtensions (new HashMap <>());
2686
+ }
2687
+ // This extension will be removed later by `fromOperation()` as it is only needed here to
2688
+ // distinguish between normal operations and callback requests
2689
+ op .getExtensions ().put ("x-callback-request" , true );
2690
+
2691
+ CodegenOperation co = fromOperation (expression , method , op , schemas , openAPI );
2692
+ if (genId ) {
2693
+ co .operationIdOriginal = null ;
2694
+ // legacy (see `fromOperation()`)
2695
+ co .nickname = co .operationId ;
2696
+ }
2697
+ u .requests .add (co );
2698
+ });
2699
+
2700
+ if (!u .requests .isEmpty ()) {
2701
+ u .requests .get (u .requests .size () - 1 ).hasMore = false ;
2702
+ }
2703
+ c .urls .add (u );
2704
+ });
2705
+
2706
+ if (!c .urls .isEmpty ()) {
2707
+ c .urls .get (c .urls .size () - 1 ).hasMore = false ;
2708
+ }
2709
+
2710
+ return c ;
2711
+ }
2712
+
2624
2713
/**
2625
2714
* Convert OAS Parameter object to Codegen Parameter object
2626
2715
*
0 commit comments