[POSTMAN] Include response examples (#21073)

* Extract response examples

* Add response examples to spec

* Generate Postman samples

* Test samples
This commit is contained in:
Beppe Catanese 2025-04-13 16:22:27 +02:00 committed by GitHub
parent ca1f02500f
commit c22d790249
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 758 additions and 433 deletions

View File

@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
/** /**
* OpenAPI generator for Postman Collection format v2.1 * OpenAPI generator for Postman Collection format v2.1
@ -389,12 +390,14 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC
String exampleRef = entry.getValue().get$ref(); String exampleRef = entry.getValue().get$ref();
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef)); Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
String exampleAsString = getJsonFromExample(example); 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) { } else if (entry.getValue().getValue() != null && entry.getValue().getValue() instanceof ObjectNode) {
// find inline // find inline
String exampleAsString = convertToJson((ObjectNode) entry.getValue().getValue()); 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) { } else if (codegenOperation.bodyParam.example != null) {
@ -416,6 +419,24 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC
items.add(new PostmanRequestItem(codegenOperation.summary, "")); 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; return items;
} }
@ -454,6 +475,37 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC
return postmanRequests; 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 * Returns human-friendly help for the generator. Provide the consumer with help
* tips, parameters here * tips, parameters here
@ -836,17 +888,65 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC
@Setter @Setter
public class PostmanRequestItem { public class PostmanRequestItem {
private String id;
private String name; private String name;
private String body; private String body;
private List<PostmanResponse> responses;
private PostmanRequestItem originalRequest;
public PostmanRequestItem() { 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) { public PostmanRequestItem(String name, String body) {
this.name = name; this.name = name;
this.body = body; 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 @Getter

View File

@ -5,59 +5,23 @@
{{#vendorExtensions.postmanRequests}} {{#vendorExtensions.postmanRequests}}
{ {
"name": "{{{name}}}", "name": "{{{name}}}",
"request": { "request": {{>request}}
"method": "{{httpMethod}}", ,"response": [
"header": [ {{#responses}}
{{#headerParams}} {"name": "{{name}}",
{ "code": {{code}},
"key": "{{baseName}}", "status": "{{status}}",
"value": "{{schema.defaultValue}}", "header": [{
"description": "{{{description}}}", "key": "Content-Type",
"disabled": {{#schema.defaultValue}}false{{/schema.defaultValue}}{{^schema.defaultValue}}true{{/schema.defaultValue}} "value": "application/json"}
}{{^-last}},{{/-last}} ],
{{/headerParams}} "_postman_previewlanguage": "json",
], "cookie": [],
"body": { "body" : "{{{body}}}",
"mode": "raw", "originalRequest": {{#originalRequest}}{{>request}}{{/originalRequest}}
"raw": "{{{body}}}", }{{^-last}},{{/-last}}
"options": { {{/responses}}
"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}}}"
}
}{{^-last}},{{/-last}} }{{^-last}},{{/-last}}
{{/vendorExtensions.postmanRequests}} {{/vendorExtensions.postmanRequests}}
] ]

View File

@ -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}}}"
}

View File

@ -740,4 +740,50 @@ public class PostmanCollectionCodegenTest {
assertEquals(true, postmanV2Generator.codegenOperationsByTag.containsKey("default")); 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");
}
} }

View File

@ -156,15 +156,10 @@ paths:
schema: schema:
$ref: '#/components/schemas/User' $ref: '#/components/schemas/User'
examples: examples:
Updated User Rebecca Baker: success:
value: $ref: '#/components/examples/response-success'
id: 13 anotherSuccess:
firstName: Rebecca $ref: '#/components/examples/another-response-success'
lastName: Baker
email: rebecca@gmail.com
dateOfBirth: '1985-10-02'
emailVerified: false
createDate: '2019-08-24'
'404': '404':
description: User Not Found description: User Not Found
'409': '409':
@ -188,17 +183,10 @@ paths:
dateOfBirth: dateOfBirth:
type: string type: string
examples: examples:
Update First Name: success:
value: $ref: '#/components/examples/patch-user'
firstName: Rebecca anotherSuccess:
Update Email: $ref: '#/components/examples/patch-user-2'
value:
email: rebecca@gmail.com
verified: true
Update Last Name & Date of Birth:
value:
lastName: Baker
dateOfBirth: '1985-10-02'
description: Patch user properties to update. description: Patch user properties to update.
/user: /user:
post: post:
@ -363,6 +351,40 @@ components:
- user - user
- admin - admin
- guest - guest
patch-user:
summary: Example patch user
value:
firstName: John
tags:
- user
patch-user-2:
summary: Example patch another user
value:
firstName: Bill
tags:
- admin
response-success:
summary: Response 200
value:
id: 001
firstName: Ron
lastName: Edwardz
email: ron.edwardz@example.com
dateOfBirth: '1980-10-31'
emailVerified: true
tags:
- admin
another-response-success:
summary: Another Response 200
value:
id: 002
firstName: Rik
lastName: Tom
email: rik.tom@example.com
dateOfBirth: '1981-10-11'
emailVerified: true
tags:
- guest
tags: tags:
- name: basic - name: basic
description: Basic tag description: Basic tag

View File

@ -17,187 +17,268 @@
"description": "Update the information of an existing user.", "description": "Update the information of an existing user.",
"item": [ "item": [
{ {
"name": "Update First Name", "name": "Example patch user",
"request": { "request": {
"method": "PATCH", "method": "PATCH",
"header": [ "header": [
{ {
"key": "Content-Type", "key": "Content-Type",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode", "key": "strCode",
"value": "code_one", "value": "code_one",
"description": "Code as header", "description": "Code as header",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode2", "key": "strCode2",
"value": "", "value": "",
"description": "Code as header2", "description": "Code as header2",
"disabled": true "disabled": true
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"firstName\" : \"Rebecca\"\n}", "raw": "{\n \"firstName\" : \"John\",\n \"tags\" : [ \"user\" ]\n}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/users/:userId", "raw": "{{baseUrl}}/users/:userId",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"users", "users",
":userId" ":userId"
], ],
"variable": [ "variable": [
{ {
"key": "userId", "key": "userId",
"value": "", "value": "",
"description": "Id of an existing user." "description": "Id of an existing user."
} }
], ],
"query": [ "query": [
] ]
}, },
"description": "Update the information of an existing user." "description": "Update the information of an existing user."
}
,"response": [
{"name": "User Updated",
"code": 200,
"status": "OK",
"header": [{
"key": "Content-Type",
"value": "application/json"}
],
"_postman_previewlanguage": "json",
"cookie": [],
"body" : "{\n \"id\" : 1,\n \"firstName\" : \"Ron\",\n \"lastName\" : \"Edwardz\",\n \"email\" : \"ron.edwardz@example.com\",\n \"dateOfBirth\" : \"1980-10-31\",\n \"emailVerified\" : true,\n \"tags\" : [ \"admin\" ]\n}",
"originalRequest": {
"method": "PATCH",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": "",
"disabled": false
},
{
"key": "Accept",
"value": "application/json",
"description": "",
"disabled": false
},
{
"key": "strCode",
"value": "code_one",
"description": "Code as header",
"disabled": false
},
{
"key": "strCode2",
"value": "",
"description": "Code as header2",
"disabled": true
}
],
"body": {
"mode": "raw",
"raw": "{\n \"firstName\" : \"John\",\n \"tags\" : [ \"user\" ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/users/:userId",
"host": [
"{{baseUrl}}"
],
"path": [
"users",
":userId"
],
"variable": [
{
"key": "userId",
"value": "",
"description": "Id of an existing user."
}
],
"query": [
]
},
"description": "Update the information of an existing user."
}
} }
]
}, },
{ {
"name": "Update Email", "name": "Example patch another user",
"request": { "request": {
"method": "PATCH", "method": "PATCH",
"header": [ "header": [
{ {
"key": "Content-Type", "key": "Content-Type",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode", "key": "strCode",
"value": "code_one", "value": "code_one",
"description": "Code as header", "description": "Code as header",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode2", "key": "strCode2",
"value": "", "value": "",
"description": "Code as header2", "description": "Code as header2",
"disabled": true "disabled": true
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"email\" : \"rebecca@gmail.com\",\n \"verified\" : true\n}", "raw": "{\n \"firstName\" : \"Bill\",\n \"tags\" : [ \"admin\" ]\n}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/users/:userId", "raw": "{{baseUrl}}/users/:userId",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"users", "users",
":userId" ":userId"
], ],
"variable": [ "variable": [
{ {
"key": "userId", "key": "userId",
"value": "", "value": "",
"description": "Id of an existing user." "description": "Id of an existing user."
} }
], ],
"query": [ "query": [
] ]
}, },
"description": "Update the information of an existing user." "description": "Update the information of an existing user."
} }
}, ,"response": [
{ {"name": "User Updated",
"name": "Update Last Name & Date of Birth", "code": 200,
"request": { "status": "OK",
"method": "PATCH", "header": [{
"header": [ "key": "Content-Type",
{ "value": "application/json"}
"key": "Content-Type", ],
"value": "application/json", "_postman_previewlanguage": "json",
"description": "", "cookie": [],
"disabled": false "body" : "{\n \"id\" : 2,\n \"firstName\" : \"Rik\",\n \"lastName\" : \"Tom\",\n \"email\" : \"rik.tom@example.com\",\n \"dateOfBirth\" : \"1981-10-11\",\n \"emailVerified\" : true,\n \"tags\" : [ \"guest\" ]\n}",
}, "originalRequest": {
{ "method": "PATCH",
"key": "Accept", "header": [
"value": "application/json", {
"description": "", "key": "Content-Type",
"disabled": false "value": "application/json",
}, "description": "",
{ "disabled": false
"key": "strCode", },
"value": "code_one", {
"description": "Code as header", "key": "Accept",
"disabled": false "value": "application/json",
}, "description": "",
{ "disabled": false
"key": "strCode2", },
"value": "", {
"description": "Code as header2", "key": "strCode",
"disabled": true "value": "code_one",
} "description": "Code as header",
], "disabled": false
"body": { },
"mode": "raw", {
"raw": "{\n \"lastName\" : \"Baker\",\n \"dateOfBirth\" : \"1985-10-02\"\n}", "key": "strCode2",
"options": { "value": "",
"raw": { "description": "Code as header2",
"language": "json" "disabled": true
} }
} ],
}, "body": {
"url": { "mode": "raw",
"raw": "{{baseUrl}}/users/:userId", "raw": "{\n \"firstName\" : \"Bill\",\n \"tags\" : [ \"admin\" ]\n}",
"host": [ "options": {
"{{baseUrl}}" "raw": {
], "language": "json"
"path": [ }
"users", }
":userId" },
], "url": {
"variable": [ "raw": "{{baseUrl}}/users/:userId",
{ "host": [
"key": "userId", "{{baseUrl}}"
"value": "", ],
"description": "Id of an existing user." "path": [
} "users",
], ":userId"
"query": [ ],
] "variable": [
}, {
"description": "Update the information of an existing user." "key": "userId",
"value": "",
"description": "Id of an existing user."
}
],
"query": [
]
},
"description": "Update the information of an existing user."
}
} }
]
} }
] ]
} }
@ -213,45 +294,47 @@
{ {
"name": "Get group by ID", "name": "Get group by ID",
"request": { "request": {
"method": "GET", "method": "GET",
"header": [ "header": [
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "", "raw": "",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/groups/:groupId", "raw": "{{baseUrl}}/groups/:groupId",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"groups", "groups",
":groupId" ":groupId"
], ],
"variable": [ "variable": [
{ {
"key": "groupId", "key": "groupId",
"value": "1", "value": "1",
"description": "group Id" "description": "group Id"
} }
], ],
"query": [ "query": [
] ]
}, },
"description": "Get group of users" "description": "Get group of users"
} }
,"response": [
]
} }
] ]
}, },
@ -262,57 +345,59 @@
{ {
"name": "Get User Info by User ID", "name": "Get User Info by User ID",
"request": { "request": {
"method": "GET", "method": "GET",
"header": [ "header": [
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode", "key": "strCode",
"value": "code_one", "value": "code_one",
"description": "Code as header", "description": "Code as header",
"disabled": false "disabled": false
}, },
{ {
"key": "strCode2", "key": "strCode2",
"value": "", "value": "",
"description": "Code as header2", "description": "Code as header2",
"disabled": true "disabled": true
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "", "raw": "",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/users/:userId", "raw": "{{baseUrl}}/users/:userId",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"users", "users",
":userId" ":userId"
], ],
"variable": [ "variable": [
{ {
"key": "userId", "key": "userId",
"value": "", "value": "",
"description": "Id of an existing user." "description": "Id of an existing user."
} }
], ],
"query": [ "query": [
] ]
}, },
"description": "Retrieve the information of the user with the matching user ID." "description": "Retrieve the information of the user with the matching user ID."
} }
,"response": [
]
} }
] ]
} }
@ -328,45 +413,98 @@
{ {
"name": "Example request for Get User", "name": "Example request for Get User",
"request": { "request": {
"method": "POST", "method": "POST",
"header": [ "header": [
{ {
"key": "Content-Type", "key": "Content-Type",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\",\n \"tags\" : [ \"user\", \"admin\", \"guest\" ]\n}", "raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\",\n \"tags\" : [ \"user\", \"admin\", \"guest\" ]\n}",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/user", "raw": "{{baseUrl}}/user",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"user" "user"
], ],
"variable": [ "variable": [
], ],
"query": [ "query": [
] ]
}, },
"description": "Create a new user." "description": "Create a new user."
}
,"response": [
{"name": "User Created",
"code": 200,
"status": "OK",
"header": [{
"key": "Content-Type",
"value": "application/json"}
],
"_postman_previewlanguage": "json",
"cookie": [],
"body" : "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\",\n \"tags\" : [ \"user\", \"admin\", \"guest\" ]\n}",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": "",
"disabled": false
},
{
"key": "Accept",
"value": "application/json",
"description": "",
"disabled": false
}
],
"body": {
"mode": "raw",
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\",\n \"tags\" : [ \"user\", \"admin\", \"guest\" ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{baseUrl}}/user",
"host": [
"{{baseUrl}}"
],
"path": [
"user"
],
"variable": [
],
"query": [
]
},
"description": "Create a new user."
}
} }
]
} }
] ]
}, },
@ -377,57 +515,59 @@
{ {
"name": "Get User Info by Query Param", "name": "Get User Info by Query Param",
"request": { "request": {
"method": "GET", "method": "GET",
"header": [ "header": [
{ {
"key": "Accept", "key": "Accept",
"value": "application/json", "value": "application/json",
"description": "", "description": "",
"disabled": false "disabled": false
}, },
{ {
"key": "Custom-Header", "key": "Custom-Header",
"value": "", "value": "",
"description": "Custom HTTP header", "description": "Custom HTTP header",
"disabled": true "disabled": true
}, },
{ {
"key": "Another-Custom-Header", "key": "Another-Custom-Header",
"value": "abc", "value": "abc",
"description": "Custom HTTP header with default", "description": "Custom HTTP header with default",
"disabled": false "disabled": false
} }
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "", "raw": "",
"options": { "options": {
"raw": { "raw": {
"language": "json" "language": "json"
} }
} }
}, },
"url": { "url": {
"raw": "{{baseUrl}}/users/", "raw": "{{baseUrl}}/users/",
"host": [ "host": [
"{{baseUrl}}" "{{baseUrl}}"
], ],
"path": [ "path": [
"users" "users"
], ],
"variable": [ "variable": [
], ],
"query": [ "query": [
{ {
"key": "pUserId", "key": "pUserId",
"value": "888", "value": "888",
"description": "Query Id.", "description": "Query Id.",
"disabled": false "disabled": false
} }
] ]
}, },
"description": "Retrieve the information of the user with the matching user ID." "description": "Retrieve the information of the user with the matching user ID."
} }
,"response": [
]
} }
] ]
} }

View File

@ -25,9 +25,9 @@ class TestParameters(unittest.TestCase):
def test_request_from_inline_examples(self): def test_request_from_inline_examples(self):
# item # item
item = self.json_data['item'][0]['item'][0]['item'][0] item = self.json_data['item'][0]['item'][0]['item'][0]
self.assertEqual(item['name'], 'Update First Name') self.assertEqual(item['name'], 'Example patch user')
self.assertEqual(item['request']["method"], 'PATCH') self.assertEqual(item['request']["method"], 'PATCH')
self.assertEqual(item['request']["body"]["raw"], '{\n "firstName" : "Rebecca"\n}') self.assertEqual(item['request']["body"]["raw"], '{\n "firstName" : "John",\n "tags" : [ "user" ]\n}')
def test_request_with_array_strings(self): def test_request_with_array_strings(self):
# item # item
@ -42,9 +42,9 @@ class TestParameters(unittest.TestCase):
def test_request_boolean_field(self): def test_request_boolean_field(self):
# item # item
item = self.json_data['item'][0]['item'][0]['item'][1] item = self.json_data['item'][0]['item'][0]['item'][1]
self.assertEqual(item['name'], 'Update Email') self.assertEqual(item['name'], 'Example patch another user')
self.assertEqual(item['request']["method"], 'PATCH') self.assertEqual(item['request']["method"], 'PATCH')
self.assertEqual(item['request']["body"]["raw"], '{\n "email" : "rebecca@gmail.com",\n "verified" : true\n}') self.assertEqual(item['request']["body"]["raw"], '{\n "firstName" : "Bill",\n "tags" : [ "admin" ]\n}')
if __name__ == '__main__': if __name__ == '__main__':