24
24
import java .util .*;
25
25
import java .util .regex .Matcher ;
26
26
import java .util .regex .Pattern ;
27
+ import java .util .stream .Collectors ;
27
28
28
29
/**
29
30
* OpenAPI generator for Postman Collection format v2.1
@@ -389,12 +390,14 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
389
390
String exampleRef = entry .getValue ().get$ref ();
390
391
Example example = this .openAPI .getComponents ().getExamples ().get (extractExampleByName (exampleRef ));
391
392
String exampleAsString = getJsonFromExample (example );
393
+ String exampleName = entry .getKey ();
392
394
393
- items .add (new PostmanRequestItem (example .getSummary (), exampleAsString ));
395
+ items .add (new PostmanRequestItem (exampleName , example .getSummary (), exampleAsString ));
394
396
} else if (entry .getValue ().getValue () != null && entry .getValue ().getValue () instanceof ObjectNode ) {
395
397
// find inline
396
398
String exampleAsString = convertToJson ((ObjectNode ) entry .getValue ().getValue ());
397
- items .add (new PostmanRequestItem (entry .getKey (), exampleAsString ));
399
+ String exampleName = entry .getKey ();
400
+ items .add (new PostmanRequestItem (exampleName , entry .getKey (), exampleAsString ));
398
401
}
399
402
}
400
403
} else if (codegenOperation .bodyParam .example != null ) {
@@ -416,6 +419,24 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
416
419
items .add (new PostmanRequestItem (codegenOperation .summary , "" ));
417
420
}
418
421
422
+ // Grabbing responses
423
+ List <CodegenResponse > responses = codegenOperation .responses ;
424
+ List <PostmanResponse > allPostmanResponses = new ArrayList <>();
425
+ for (CodegenResponse response : responses ) {
426
+ List <PostmanResponse > postmanResponses = getResponseExamples (response , response .message );
427
+ allPostmanResponses .addAll (postmanResponses );
428
+ }
429
+
430
+ // Adding responses to corresponding requests
431
+ for (PostmanRequestItem item : items ){
432
+ List <PostmanResponse > postmanResponses = allPostmanResponses .stream ().filter ( r -> Objects .equals (r .getId (), item .getId ())).collect (Collectors .toList ());
433
+ if (!postmanResponses .isEmpty ()){
434
+ postmanResponses .forEach (r -> r .setOriginalRequest (item ));
435
+ item .addResponses (postmanResponses );
436
+ }
437
+ }
438
+
439
+
419
440
return items ;
420
441
}
421
442
@@ -454,6 +475,37 @@ List<PostmanRequestItem> setPostmanIsoTimestamp(List<PostmanRequestItem> postman
454
475
return postmanRequests ;
455
476
}
456
477
478
+ List <PostmanResponse > getResponseExamples (CodegenResponse codegenResponse , String message ) {
479
+ List <PostmanResponse > postmanResponses = new ArrayList <>();
480
+
481
+ if (codegenResponse .getContent () != null && codegenResponse .getContent ().get ("application/json" ) != null &&
482
+ codegenResponse .getContent ().get ("application/json" ).getExamples () != null ) {
483
+
484
+ var examples = codegenResponse .getContent ().get ("application/json" ).getExamples ();
485
+ for (Map .Entry <String , Example > entry : examples .entrySet ()) {
486
+ String key = entry .getKey ();
487
+ String ref = entry .getValue ().get$ref ();
488
+
489
+ String response ;
490
+ if (ref != null ) {
491
+ // get example by $ref
492
+ Example example = this .openAPI .getComponents ().getExamples ().get (extractExampleByName (ref ));
493
+ response = getJsonFromExample (example );
494
+ } else {
495
+ // get inline example
496
+ response = getJsonFromExample (entry .getValue ());
497
+ }
498
+ postmanResponses .add (new PostmanResponse (key , codegenResponse , message , response ));
499
+ }
500
+
501
+ } else if (codegenResponse .getContent () != null ) {
502
+ // TODO : Implement
503
+ }
504
+
505
+ return postmanResponses ;
506
+ }
507
+
508
+
457
509
/**
458
510
* Returns human-friendly help for the generator. Provide the consumer with help
459
511
* tips, parameters here
@@ -836,17 +888,65 @@ public String getPostmanType(CodegenProperty codegenProperty) {
836
888
@ Setter
837
889
public class PostmanRequestItem {
838
890
891
+ private String id ;
839
892
private String name ;
840
893
private String body ;
894
+ private List <PostmanResponse > responses ;
895
+
896
+ private PostmanRequestItem originalRequest ;
841
897
842
898
public PostmanRequestItem () {
843
899
}
844
900
901
+ public PostmanRequestItem (String id , String name , String body ) {
902
+ this .id = id ;
903
+ this .name = name ;
904
+ this .body = body ;
905
+ }
906
+
845
907
public PostmanRequestItem (String name , String body ) {
846
908
this .name = name ;
847
909
this .body = body ;
848
910
}
849
911
912
+ public void addResponses (List <PostmanResponse > responses ) {
913
+ if (this .responses == null ) { this .responses = new ArrayList <>(); }
914
+
915
+ this .responses .addAll (responses );
916
+ }
917
+
918
+ }
919
+
920
+ @ Getter
921
+ @ Setter
922
+ public class PostmanResponse {
923
+
924
+ private String id ;
925
+ private String code ;
926
+ private String status ;
927
+ private String name ;
928
+ private String body ;
929
+ private PostmanRequestItem originalRequest ;
930
+
931
+ public PostmanResponse (String id , CodegenResponse response , String name , String body ) {
932
+ this .id = id ;
933
+ this .code = response .code ;
934
+ this .status = PostmanCollectionCodegen .this .getStatus (response );
935
+ this .name = name ;
936
+ this .body = body ;
937
+ this .originalRequest = null ; // Setting this here explicitly for clarity
938
+ }
939
+
940
+
941
+ public PostmanRequestItem getOriginalRequest () {
942
+ return originalRequest ;
943
+ }
944
+
945
+ public void setOriginalRequest (PostmanRequestItem originalRequest ) {
946
+ this .originalRequest = originalRequest ;
947
+ }
948
+
949
+
850
950
}
851
951
852
952
@ Getter
0 commit comments