2424import java .util .*;
2525import java .util .regex .Matcher ;
2626import java .util .regex .Pattern ;
27+ import java .util .stream .Collectors ;
2728
2829/**
2930 * OpenAPI generator for Postman Collection format v2.1
@@ -389,12 +390,14 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
389390 String exampleRef = entry .getValue ().get$ref ();
390391 Example example = this .openAPI .getComponents ().getExamples ().get (extractExampleByName (exampleRef ));
391392 String exampleAsString = getJsonFromExample (example );
393+ String exampleName = entry .getKey ();
392394
393- items .add (new PostmanRequestItem (example .getSummary (), exampleAsString ));
395+ items .add (new PostmanRequestItem (exampleName , example .getSummary (), exampleAsString ));
394396 } else if (entry .getValue ().getValue () != null && entry .getValue ().getValue () instanceof ObjectNode ) {
395397 // find inline
396398 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 ));
398401 }
399402 }
400403 } else if (codegenOperation .bodyParam .example != null ) {
@@ -416,6 +419,24 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
416419 items .add (new PostmanRequestItem (codegenOperation .summary , "" ));
417420 }
418421
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+
419440 return items ;
420441 }
421442
@@ -454,6 +475,37 @@ List<PostmanRequestItem> setPostmanIsoTimestamp(List<PostmanRequestItem> postman
454475 return postmanRequests ;
455476 }
456477
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+
457509 /**
458510 * Returns human-friendly help for the generator. Provide the consumer with help
459511 * tips, parameters here
@@ -836,17 +888,65 @@ public String getPostmanType(CodegenProperty codegenProperty) {
836888 @ Setter
837889 public class PostmanRequestItem {
838890
891+ private String id ;
839892 private String name ;
840893 private String body ;
894+ private List <PostmanResponse > responses ;
895+
896+ private PostmanRequestItem originalRequest ;
841897
842898 public PostmanRequestItem () {
843899 }
844900
901+ public PostmanRequestItem (String id , String name , String body ) {
902+ this .id = id ;
903+ this .name = name ;
904+ this .body = body ;
905+ }
906+
845907 public PostmanRequestItem (String name , String body ) {
846908 this .name = name ;
847909 this .body = body ;
848910 }
849911
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+
850950 }
851951
852952 @ Getter
0 commit comments