Skip to content

Commit eb752d4

Browse files
authored
Merge pull request #97 from adyen-examples/add-get-response
GET requests: define expected response
2 parents 63f8ad6 + c049146 commit eb752d4

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java

+30-13
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,12 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
337337

338338
List<PostmanRequestItem> items = new ArrayList<>();
339339

340-
if(codegenOperation.getHasBodyParam()) {
340+
if (codegenOperation.getHasBodyParam()) {
341341
// operation with bodyParam
342342
if (requestParameterGeneration.equalsIgnoreCase("Schema")) {
343343
// get from schema
344-
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam)));
344+
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam),
345+
codegenOperation.httpMethod));
345346
} else {
346347
// get from examples
347348
if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
@@ -362,44 +363,60 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
362363
exampleAsString = new ExampleJsonHelper().getJsonFromExample(entry.getValue());
363364
exampleName = entry.getValue().getSummary();
364365
}
365-
items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey()));
366+
items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey(), codegenOperation.httpMethod));
366367
}
367368
} else if (codegenOperation.bodyParam.example != null) {
368369
// find in bodyParam example
369-
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example)));
370+
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example),
371+
codegenOperation.httpMethod));
370372
} else if (codegenOperation.bodyParam.getSchema() != null) {
371373
// find in schema example
372374
String exampleAsString = new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.getSchema().getExample());
373-
items.add(new PostmanRequestItem(codegenOperation.summary, exampleAsString));
375+
items.add(new PostmanRequestItem(codegenOperation.summary, exampleAsString, codegenOperation.httpMethod));
374376
} else {
375377
// example not found
376378
// get from schema
377-
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam)));
379+
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam), codegenOperation.httpMethod));
378380

379381
}
380382
}
381383
} else {
382384
// operation without bodyParam
383-
items.add(new PostmanRequestItem(codegenOperation.summary, ""));
385+
PostmanRequestItem postmanRequestItem = new PostmanRequestItem(codegenOperation.summary, "", codegenOperation.httpMethod);
386+
items.add(postmanRequestItem);
384387
}
385388

386389
// Grabbing responses
387390
List<CodegenResponse> responses = codegenOperation.responses;
388391
List<PostmanResponse> allPostmanResponses = new ArrayList<>();
389392
for (CodegenResponse response : responses) {
390-
List<PostmanResponse> postmanResponses = getResponseExamples(response, response.message);
391-
allPostmanResponses.addAll(postmanResponses);
393+
List<PostmanResponse> postmanResponses = getResponseExamples(response, response.message);
394+
allPostmanResponses.addAll(postmanResponses);
392395
}
393396

394397
// Adding responses to corresponding requests
395-
for(PostmanRequestItem item: items){
396-
List<PostmanResponse> postmanResponses = allPostmanResponses.stream().filter( r -> Objects.equals(r.getId(), item.getId())).collect(Collectors.toList());
397-
if(!postmanResponses.isEmpty()){
398+
for (PostmanRequestItem item : items) {
399+
List<PostmanResponse> postmanResponses = allPostmanResponses.stream().filter(r -> Objects.equals(r.getId(), item.getId())).collect(Collectors.toList());
400+
if (!postmanResponses.isEmpty()) {
398401
postmanResponses.forEach(r -> r.setOriginalRequest(item));
399402
item.addResponses(postmanResponses);
403+
} else {
404+
// no matching response example
405+
if (item.getHttpMethod() != null && item.getHttpMethod().equals("GET")) {
406+
// in case of GET use first response example
407+
if (allPostmanResponses.size() > 0) {
408+
PostmanResponse postmanResponse = allPostmanResponses.stream()
409+
.findFirst()
410+
.orElse(null);
411+
412+
if(postmanResponse != null) {
413+
postmanResponse.setOriginalRequest(item);
414+
item.addResponse(postmanResponse);
415+
}
416+
}
417+
}
400418
}
401419
}
402-
403420
return items;
404421
}
405422

src/main/java/com/adyen/codegen/postman/model/PostmanRequestItem.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ public class PostmanRequestItem {
88
private String id;
99
private String name;
1010
private String body;
11+
private String httpMethod;
1112

1213
private List<PostmanResponse> responses;
1314

14-
public PostmanRequestItem(String name, String body, String id) {
15+
public PostmanRequestItem(String name, String body, String id, String httpMethod) {
1516
this.id = id;
1617
this.name = name;
1718
this.body = body;
19+
this.httpMethod = httpMethod;
1820
}
19-
public PostmanRequestItem(String name, String body) {
21+
public PostmanRequestItem(String name, String body, String httpMethod) {
2022
this.name = name;
2123
this.body = body;
24+
this.httpMethod = httpMethod;
2225
this.id = ""; // TODO : do better later
2326
}
2427

@@ -46,6 +49,14 @@ public void setId(String id) {
4649
this.id = id;
4750
}
4851

52+
public String getHttpMethod() {
53+
return httpMethod;
54+
}
55+
56+
public void setHttpMethod(String httpMethod) {
57+
this.httpMethod = httpMethod;
58+
}
59+
4960
public List<PostmanResponse> getResponses() {
5061
return responses;
5162
}

src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public void processRequestExample() {
413413
postmanV2Generator.postmanVariableNames = new String[]{"MY_VAR_1", "MY_VAR_2"};
414414

415415
List<PostmanRequestItem> requestItems = new ArrayList<>();
416-
requestItems.add(new PostmanRequestItem("get by id", STR));
416+
requestItems.add(new PostmanRequestItem("get by id", STR, "GET"));
417417

418418
requestItems = postmanV2Generator.createPostmanVariables(requestItems);
419419

@@ -899,4 +899,30 @@ public void testInlineExamples() throws IOException {
899899
" \"value\": \"application/json\"}\n");
900900
}
901901

902+
@Test
903+
public void getGetResponseExample() throws IOException, ParseException {
904+
905+
File output = Files.createTempDirectory("postmantest_").toFile();
906+
output.deleteOnExit();
907+
908+
final CodegenConfigurator configurator = new CodegenConfigurator()
909+
.setGeneratorName("postman-v2")
910+
.setInputSpec("./src/test/resources/CheckoutService-v71.yaml")
911+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
912+
913+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
914+
DefaultGenerator generator = new DefaultGenerator();
915+
List<File> files = generator.opts(clientOptInput).generate();
916+
917+
System.out.println(files);
918+
files.forEach(File::deleteOnExit);
919+
920+
Path path = Paths.get(output + "/postman.json");
921+
TestUtils.assertFileExists(path);
922+
923+
TestUtils.assertFileContains(path, "\"description\": \"Retrieves the payment link details using the payment link `id`.");
924+
TestUtils.assertFileContains(path, "\"name\": \"Successful getPaymentLink GET response example\",");
925+
926+
}
927+
902928
}

src/test/resources/CheckoutService-v71.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ paths:
751751
$ref: '#/components/examples/get-paymentLinks-linkId-basic-200'
752752
schema:
753753
$ref: '#/components/schemas/PaymentLinkResponse'
754-
description: OK - the request has succeeded.
754+
description: Successful getPaymentLink GET response example
755755
'400':
756756
content:
757757
application/json:

0 commit comments

Comments
 (0)