From cb75fa0d9e8af082d41a371120fee84b689bf546 Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:03:31 +0100 Subject: [PATCH] [POSTMAN] Genarate Postman requests from inline examples (#18086) * Process inline examples (if available) * Test generation with inline examples * Update samples * Move test to CI testing suite --- .../languages/PostmanCollectionCodegen.java | 28 ++-- .../schema/postman-collection/postman.json | 124 +++++++++++++++++- .../python/test/test_endpoints.py | 7 + 3 files changed, 147 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java index 10745a5fd20..0ffc061b24a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java @@ -374,19 +374,25 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC items.add(new PostmanRequestItem(codegenOperation.summary, getJsonFromSchema(codegenOperation.bodyParam))); } else { // get from examples - if (codegenOperation.bodyParam.example != null) { + if (codegenOperation.bodyParam.getContent().get("application/json") != null && + codegenOperation.bodyParam.getContent().get("application/json").getExamples() != null) { + for (Map.Entry entry : codegenOperation.bodyParam.getContent().get("application/json").getExamples().entrySet()) { + 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.getContent().get("application/json") != null && - codegenOperation.bodyParam.getContent().get("application/json").getExamples() != null) { - // find in components/examples - for (Map.Entry 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)); - } } else if (codegenOperation.bodyParam.getSchema() != null) { // find in schema example String exampleAsString = formatJson(codegenOperation.bodyParam.getSchema().getExample()); diff --git a/samples/schema/postman-collection/postman.json b/samples/schema/postman-collection/postman.json index 95542f3ff14..6d1088e6742 100644 --- a/samples/schema/postman-collection/postman.json +++ b/samples/schema/postman-collection/postman.json @@ -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": [ @@ -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\" : \"rebecca@gmail.com\"\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." + } } ] } diff --git a/samples/schema/postman-collection/python/test/test_endpoints.py b/samples/schema/postman-collection/python/test/test_endpoints.py index 6db1cd5b746..7e0c18f6944 100644 --- a/samples/schema/postman-collection/python/test/test_endpoints.py +++ b/samples/schema/postman-collection/python/test/test_endpoints.py @@ -22,6 +22,13 @@ class TestParameters(unittest.TestCase): 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()