Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* OpenAPI generator for Postman Collection format v2.1
Expand Down Expand Up @@ -389,12 +390,14 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
String exampleRef = entry.getValue().get$ref();
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
String exampleAsString = getJsonFromExample(example);
String exampleName = entry.getKey();

items.add(new PostmanRequestItem(example.getSummary(), exampleAsString));
items.add(new PostmanRequestItem(exampleName, example.getSummary(), exampleAsString));
} else if (entry.getValue().getValue() != null && entry.getValue().getValue() instanceof ObjectNode) {
// find inline
String exampleAsString = convertToJson((ObjectNode) entry.getValue().getValue());
items.add(new PostmanRequestItem(entry.getKey(), exampleAsString));
String exampleName = entry.getKey();
items.add(new PostmanRequestItem(exampleName, entry.getKey(), exampleAsString));
}
}
} else if (codegenOperation.bodyParam.example != null) {
Expand All @@ -416,6 +419,24 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
items.add(new PostmanRequestItem(codegenOperation.summary, ""));
}

// 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);
}

// 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()){
postmanResponses.forEach(r -> r.setOriginalRequest(item));
item.addResponses(postmanResponses);
}
}


return items;
}

Expand Down Expand Up @@ -454,6 +475,37 @@ List<PostmanRequestItem> setPostmanIsoTimestamp(List<PostmanRequestItem> postman
return postmanRequests;
}

List<PostmanResponse> getResponseExamples(CodegenResponse codegenResponse, String message) {
List<PostmanResponse> postmanResponses = new ArrayList<>();

if (codegenResponse.getContent() != null && codegenResponse.getContent().get("application/json") != null &&
codegenResponse.getContent().get("application/json").getExamples() != null) {

var examples = codegenResponse.getContent().get("application/json").getExamples();
for (Map.Entry<String, Example> entry : examples.entrySet()) {
String key = entry.getKey();
String ref = entry.getValue().get$ref();

String response;
if (ref != null) {
// get example by $ref
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(ref));
response = getJsonFromExample(example);
} else {
// get inline example
response = getJsonFromExample(entry.getValue());
}
postmanResponses.add(new PostmanResponse(key, codegenResponse, message, response));
}

} else if (codegenResponse.getContent() != null) {
// TODO : Implement
}

return postmanResponses;
}


/**
* Returns human-friendly help for the generator. Provide the consumer with help
* tips, parameters here
Expand Down Expand Up @@ -836,17 +888,65 @@ public String getPostmanType(CodegenProperty codegenProperty) {
@Setter
public class PostmanRequestItem {

private String id;
private String name;
private String body;
private List<PostmanResponse> responses;

private PostmanRequestItem originalRequest;

public PostmanRequestItem() {
}

public PostmanRequestItem(String id, String name, String body) {
this.id = id;
this.name = name;
this.body = body;
}

public PostmanRequestItem(String name, String body) {
this.name = name;
this.body = body;
}

public void addResponses(List<PostmanResponse> responses) {
if(this.responses == null) { this.responses = new ArrayList<>(); }

this.responses.addAll(responses);
}

}

@Getter
@Setter
public class PostmanResponse {

private String id;
private String code;
private String status;
private String name;
private String body;
private PostmanRequestItem originalRequest;

public PostmanResponse(String id, CodegenResponse response, String name, String body) {
this.id = id;
this.code = response.code;
this.status = PostmanCollectionCodegen.this.getStatus(response);
this.name = name;
this.body = body;
this.originalRequest = null; // Setting this here explicitly for clarity
}


public PostmanRequestItem getOriginalRequest() {
return originalRequest;
}

public void setOriginalRequest(PostmanRequestItem originalRequest) {
this.originalRequest = originalRequest;
}


}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,23 @@
{{#vendorExtensions.postmanRequests}}
{
"name": "{{{name}}}",
"request": {
"method": "{{httpMethod}}",
"header": [
{{#headerParams}}
{
"key": "{{baseName}}",
"value": "{{schema.defaultValue}}",
"description": "{{{description}}}",
"disabled": {{#schema.defaultValue}}false{{/schema.defaultValue}}{{^schema.defaultValue}}true{{/schema.defaultValue}}
}{{^-last}},{{/-last}}
{{/headerParams}}
],
"body": {
"mode": "raw",
"raw": "{{{body}}}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{=<% %>=}}{{baseUrl}}<%={{ }}=%>{{{path}}}",
"host": [
"{{=<% %>=}}{{baseUrl}}<%={{ }}=%>"
],
"path": [
{{#vendorExtensions.pathSegments}}
"{{.}}"{{^-last}},{{/-last}}
{{/vendorExtensions.pathSegments}}
],
"variable": [
{{#pathParams}}
{
"key": "{{paramName}}",
"value": "{{defaultValue}}",
"description": "{{{description}}}"
}{{^-last}},{{/-last}}
{{/pathParams}}
],
"query": [
{{#queryParams}}
{
"key": "{{paramName}}",
"value": "{{example}}",
"description": "{{{description}}}",
"disabled": {{#required}}false{{/required}}{{^required}}true{{/required}}
}{{^-last}},{{/-last}}
{{/queryParams}}
]
},
"description": "{{{notes}}}"
}
"request": {{>request}}
,"response": [
{{#responses}}
{"name": "{{name}}",
"code": {{code}},
"status": "{{status}}",
"header": [{
"key": "Content-Type",
"value": "application/json"}
],
"_postman_previewlanguage": "json",
"cookie": [],
"body" : "{{{body}}}",
"originalRequest": {{#originalRequest}}{{>request}}{{/originalRequest}}
}{{^-last}},{{/-last}}
{{/responses}}
]
}{{^-last}},{{/-last}}
{{/vendorExtensions.postmanRequests}}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"method": "{{httpMethod}}",
"header": [
{{#headerParams}}
{
"key": "{{baseName}}",
"value": "{{schema.defaultValue}}",
"description": "{{{description}}}",
"disabled": {{#schema.defaultValue}}false{{/schema.defaultValue}}{{^schema.defaultValue}}true{{/schema.defaultValue}}
}{{^-last}},{{/-last}}
{{/headerParams}}
],
"body": {
"mode": "raw",
"raw": "{{{body}}}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{=<% %>=}}{{baseUrl}}<%={{ }}=%>{{path}}",
"host": [
"{{=<% %>=}}{{baseUrl}}<%={{ }}=%>"
],
"path": [
{{#vendorExtensions.pathSegments}}
"{{.}}"{{^-last}},{{/-last}}
{{/vendorExtensions.pathSegments}}
],
"variable": [
{{#pathParams}}
{
"key": "{{paramName}}",
"value": "{{defaultValue}}",
"description": "{{{description}}}"
}{{^-last}},{{/-last}}
{{/pathParams}}
],
"query": [
{{#queryParams}}
{
"key": "{{paramName}}",
"value": "{{example}}",
"description": "{{{description}}}",
"disabled": {{#required}}false{{/required}}{{^required}}true{{/required}}
}{{^-last}},{{/-last}}
{{/queryParams}}
]
},
"description": "{{{notes}}}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,4 +740,50 @@ public void testAddToMapUsingDefaultTag() {
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("default"));
}

@Test
public void testResponses() throws IOException {

File output = Files.createTempDirectory("postmantest_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("postman-collection")
.setInputSpec("src/test/resources/3_0/postman-collection/SampleProject.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();

System.out.println(files);
//files.forEach(File::deleteOnExit);

Path path = Paths.get(output + "/postman.json");
TestUtils.assertFileExists(path);

TestUtils.assertFileContains(path, "\"response\": [\n" +
" {\"name\": \"User Updated\",\n" +
" \"code\": 200,\n" +
" \"status\": \"OK\",\n" +
" \"header\": [{\n" +
" \"key\": \"Content-Type\",\n" +
" \"value\": \"application/json\"}\n" +
" ],\n" +
" \"_postman_previewlanguage\": \"json\",\n" +
" \"cookie\": [],\n" +
" \"body\" : \"{\\n \\\"id\\\" : 1,\\n");

TestUtils.assertFileContains(path, "\"response\": [\n" +
" {\"name\": \"User Updated\",\n" +
" \"code\": 200,\n" +
" \"status\": \"OK\",\n" +
" \"header\": [{\n" +
" \"key\": \"Content-Type\",\n" +
" \"value\": \"application/json\"}\n" +
" ],\n" +
" \"_postman_previewlanguage\": \"json\",\n" +
" \"cookie\": [],\n" +
" \"body\" : \"{\\n \\\"id\\\" : 2,\\n");

}

}
Loading
Loading