forked from loafle/openapi-generator-original
[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
This commit is contained in:
parent
5ed2283e01
commit
cb75fa0d9e
@ -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) {
|
||||
// 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()) {
|
||||
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());
|
||||
|
@ -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."
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user