Skip to content

Commit

Permalink
[POSTMAN] Genarate Postman requests from inline examples (OpenAPITool…
Browse files Browse the repository at this point in the history
…s#18086)

* Process inline examples (if available)

* Test generation with inline examples

* Update samples

* Move test to CI testing suite
  • Loading branch information
gcatanese authored Mar 14, 2024
1 parent 5ed2283 commit cb75fa0
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,25 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
items.add(new PostmanRequestItem(codegenOperation.summary, getJsonFromSchema(codegenOperation.bodyParam)));
} else {
// get from examples
if (codegenOperation.bodyParam.example != null) {
// find in bodyParam example
items.add(new PostmanRequestItem(codegenOperation.summary, formatJson(codegenOperation.bodyParam.example)));
} else if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
codegenOperation.bodyParam.getContent().get("application/json").getExamples() != null) {
// find in components/examples
for (Map.Entry<String, Example> entry : codegenOperation.bodyParam.getContent().get("application/json").getExamples().entrySet()) {
String exampleRef = entry.getValue().get$ref();
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
String exampleAsString = getJsonFromExample(example);

items.add(new PostmanRequestItem(example.getSummary(), exampleAsString));
if(entry.getValue().get$ref() != null) {
// find in components/examples
String exampleRef = entry.getValue().get$ref();
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
String exampleAsString = getJsonFromExample(example);

items.add(new PostmanRequestItem(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));
}
}
} else if (codegenOperation.bodyParam.example != null) {
// find in bodyParam example
items.add(new PostmanRequestItem(codegenOperation.summary, formatJson(codegenOperation.bodyParam.example)));
} else if (codegenOperation.bodyParam.getSchema() != null) {
// find in schema example
String exampleAsString = formatJson(codegenOperation.bodyParam.getSchema().getExample());
Expand Down
124 changes: 123 additions & 1 deletion samples/schema/postman-collection/postman.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"description": "Update the information of an existing user.",
"item": [
{
"name": "Update User Information",
"name": "Update First Name",
"request": {
"method": "PATCH",
"header": [
Expand Down Expand Up @@ -76,6 +76,128 @@
},
"description": "Update the information of an existing user."
}
},
{
"name": "Update Email",
"request": {
"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 \"email\" : \"[email protected]\"\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 Last Name & Date of Birth",
"request": {
"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 \"lastName\" : \"Baker\",\n \"dateOfBirth\" : \"1985-10-02\"\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."
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def test_endpoint_deprecated(self):
path = self.json_data['item'][0]['item'][0]
self.assertEqual(path['name'], '/users/:userId (DEPRECATED)')

def test_request_from_inline_examples(self):
# item
item = self.json_data['item'][0]['item'][0]['item'][0]
self.assertEqual(item['name'], 'Update First Name')
self.assertEqual(item['request']["method"], 'PATCH')
self.assertEqual(item['request']["body"]["raw"], '{\n "firstName" : "Rebecca"\n}')


if __name__ == '__main__':
unittest.main()

0 comments on commit cb75fa0

Please sign in to comment.