From 8c5a53f9eea0b388a94d7f4ed8fd2fe10285fc35 Mon Sep 17 00:00:00 2001 From: Beppe Catanese Date: Fri, 18 Apr 2025 16:14:20 +0200 Subject: [PATCH 1/2] Use first response for GET operations --- .../codegen/postman/PostmanV2Generator.java | 43 +++++++++++++------ .../postman/model/PostmanRequestItem.java | 15 ++++++- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java b/src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java index 8887458..06f00c6 100644 --- a/src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java +++ b/src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java @@ -337,11 +337,12 @@ List getPostmanRequests(CodegenOperation codegenOperation) { List items = new ArrayList<>(); - if(codegenOperation.getHasBodyParam()) { + if (codegenOperation.getHasBodyParam()) { // operation with bodyParam if (requestParameterGeneration.equalsIgnoreCase("Schema")) { // get from schema - items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam))); + items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam), + codegenOperation.httpMethod)); } else { // get from examples if (codegenOperation.bodyParam.getContent().get("application/json") != null && @@ -362,44 +363,60 @@ List getPostmanRequests(CodegenOperation codegenOperation) { exampleAsString = new ExampleJsonHelper().getJsonFromExample(entry.getValue()); exampleName = entry.getValue().getSummary(); } - items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey())); + items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey(), codegenOperation.httpMethod)); } } else if (codegenOperation.bodyParam.example != null) { // find in bodyParam example - items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example))); + items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example), + codegenOperation.httpMethod)); } else if (codegenOperation.bodyParam.getSchema() != null) { // find in schema example String exampleAsString = new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.getSchema().getExample()); - items.add(new PostmanRequestItem(codegenOperation.summary, exampleAsString)); + items.add(new PostmanRequestItem(codegenOperation.summary, exampleAsString, codegenOperation.httpMethod)); } else { // example not found // get from schema - items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam))); + items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam), codegenOperation.httpMethod)); } } } else { // operation without bodyParam - items.add(new PostmanRequestItem(codegenOperation.summary, "")); + PostmanRequestItem postmanRequestItem = new PostmanRequestItem(codegenOperation.summary, "", codegenOperation.httpMethod); + items.add(postmanRequestItem); } // Grabbing responses List responses = codegenOperation.responses; List allPostmanResponses = new ArrayList<>(); for (CodegenResponse response : responses) { - List postmanResponses = getResponseExamples(response, response.message); - allPostmanResponses.addAll(postmanResponses); + List postmanResponses = getResponseExamples(response, response.message); + allPostmanResponses.addAll(postmanResponses); } // Adding responses to corresponding requests - for(PostmanRequestItem item: items){ - List postmanResponses = allPostmanResponses.stream().filter( r -> Objects.equals(r.getId(), item.getId())).collect(Collectors.toList()); - if(!postmanResponses.isEmpty()){ + for (PostmanRequestItem item : items) { + List postmanResponses = allPostmanResponses.stream().filter(r -> Objects.equals(r.getId(), item.getId())).collect(Collectors.toList()); + if (!postmanResponses.isEmpty()) { postmanResponses.forEach(r -> r.setOriginalRequest(item)); item.addResponses(postmanResponses); + } else { + // no matching response example + if (item.getHttpMethod() != null && item.getHttpMethod().equals("GET")) { + // in case of GET use first response example + if (allPostmanResponses.size() > 0) { + PostmanResponse postmanResponse = allPostmanResponses.stream() + .findFirst() + .orElse(null); + + if(postmanResponse != null) { + postmanResponse.setOriginalRequest(item); + item.addResponse(postmanResponse); + } + } + } } } - return items; } diff --git a/src/main/java/com/adyen/codegen/postman/model/PostmanRequestItem.java b/src/main/java/com/adyen/codegen/postman/model/PostmanRequestItem.java index c63eb73..275435f 100644 --- a/src/main/java/com/adyen/codegen/postman/model/PostmanRequestItem.java +++ b/src/main/java/com/adyen/codegen/postman/model/PostmanRequestItem.java @@ -8,17 +8,20 @@ public class PostmanRequestItem { private String id; private String name; private String body; + private String httpMethod; private List responses; - public PostmanRequestItem(String name, String body, String id) { + public PostmanRequestItem(String name, String body, String id, String httpMethod) { this.id = id; this.name = name; this.body = body; + this.httpMethod = httpMethod; } - public PostmanRequestItem(String name, String body) { + public PostmanRequestItem(String name, String body, String httpMethod) { this.name = name; this.body = body; + this.httpMethod = httpMethod; this.id = ""; // TODO : do better later } @@ -46,6 +49,14 @@ public void setId(String id) { this.id = id; } + public String getHttpMethod() { + return httpMethod; + } + + public void setHttpMethod(String httpMethod) { + this.httpMethod = httpMethod; + } + public List getResponses() { return responses; } From c049146cd311d1ad0e85b45cdffdfbd0768b69e1 Mon Sep 17 00:00:00 2001 From: Beppe Catanese Date: Fri, 18 Apr 2025 16:14:28 +0200 Subject: [PATCH 2/2] Add test --- .../postman/PostmanV2GeneratorTest.java | 28 ++++++++++++++++++- src/test/resources/CheckoutService-v71.yaml | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java b/src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java index 873188f..277b4f8 100644 --- a/src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java +++ b/src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java @@ -413,7 +413,7 @@ public void processRequestExample() { postmanV2Generator.postmanVariableNames = new String[]{"MY_VAR_1", "MY_VAR_2"}; List requestItems = new ArrayList<>(); - requestItems.add(new PostmanRequestItem("get by id", STR)); + requestItems.add(new PostmanRequestItem("get by id", STR, "GET")); requestItems = postmanV2Generator.createPostmanVariables(requestItems); @@ -899,4 +899,30 @@ public void testInlineExamples() throws IOException { " \"value\": \"application/json\"}\n"); } + @Test + public void getGetResponseExample() throws IOException, ParseException { + + File output = Files.createTempDirectory("postmantest_").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("postman-v2") + .setInputSpec("./src/test/resources/CheckoutService-v71.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + final ClientOptInput clientOptInput = configurator.toClientOptInput(); + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(clientOptInput).generate(); + + System.out.println(files); + files.forEach(File::deleteOnExit); + + Path path = Paths.get(output + "/postman.json"); + TestUtils.assertFileExists(path); + + TestUtils.assertFileContains(path, "\"description\": \"Retrieves the payment link details using the payment link `id`."); + TestUtils.assertFileContains(path, "\"name\": \"Successful getPaymentLink GET response example\","); + + } + } \ No newline at end of file diff --git a/src/test/resources/CheckoutService-v71.yaml b/src/test/resources/CheckoutService-v71.yaml index 4377a57..fac0e5b 100644 --- a/src/test/resources/CheckoutService-v71.yaml +++ b/src/test/resources/CheckoutService-v71.yaml @@ -751,7 +751,7 @@ paths: $ref: '#/components/examples/get-paymentLinks-linkId-basic-200' schema: $ref: '#/components/schemas/PaymentLinkResponse' - description: OK - the request has succeeded. + description: Successful getPaymentLink GET response example '400': content: application/json: