Skip to content

GET requests: define expected response #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,12 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {

List<PostmanRequestItem> 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 &&
Expand All @@ -362,44 +363,60 @@ List<PostmanRequestItem> 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<CodegenResponse> responses = codegenOperation.responses;
List<PostmanResponse> allPostmanResponses = new ArrayList<>();
for (CodegenResponse response : responses) {
List<PostmanResponse> postmanResponses = getResponseExamples(response, response.message);
allPostmanResponses.addAll(postmanResponses);
List<PostmanResponse> postmanResponses = getResponseExamples(response, response.message);
allPostmanResponses.addAll(postmanResponses);
}

// Adding responses to corresponding requests
for(PostmanRequestItem item: items){
List<PostmanResponse> postmanResponses = allPostmanResponses.stream().filter( r -> Objects.equals(r.getId(), item.getId())).collect(Collectors.toList());
if(!postmanResponses.isEmpty()){
for (PostmanRequestItem item : items) {
List<PostmanResponse> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ public class PostmanRequestItem {
private String id;
private String name;
private String body;
private String httpMethod;

private List<PostmanResponse> 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
}

Expand Down Expand Up @@ -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<PostmanResponse> getResponses() {
return responses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public void processRequestExample() {
postmanV2Generator.postmanVariableNames = new String[]{"MY_VAR_1", "MY_VAR_2"};

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

requestItems = postmanV2Generator.createPostmanVariables(requestItems);

Expand Down Expand Up @@ -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<File> 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\",");

}

}
2 changes: 1 addition & 1 deletion src/test/resources/CheckoutService-v71.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down