Skip to content

Commit

Permalink
Merging example request bodies with OpenAPI request body.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinswiber committed Sep 13, 2023
1 parent 44eddb0 commit cd90f5c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,24 @@ impl<'a> Transpiler<'a> {

if let Some(responses) = &item.response {
for r in responses.iter().flatten() {
if let Some(or) = &r.original_request {
if let Some(body) = &or.body {
content_type = Some("text/plain".to_string());
if let Some(options) = body.options.clone() {
if let Some(raw_options) = options.raw {
if raw_options.language.is_some() {
content_type = match raw_options.language.unwrap().as_str() {
"xml" => Some("application/xml".to_string()),
"json" => Some("application/json".to_string()),
"html" => Some("text/html".to_string()),
_ => Some("text/plain".to_string()),
}
}
}
}
self.extract_request_body(body, op, request_name, content_type);
}
}
let mut oas_response = openapi3::Response::default();
let mut response_media_types = BTreeMap::<String, openapi3::MediaType>::new();

Expand Down
112 changes: 111 additions & 1 deletion tests/fixtures/wasm/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,48 @@
"items": {
"type": "object",
"properties": {
"event": {
"type": "array",
"items": {
"type": "object",
"properties": {
"listen": {
"type": "string",
"example": "test"
},
"script": {
"type": "object",
"properties": {
"exec": {
"type": "array",
"items": {
"type": "string",
"example": "tests['response code is 200'] = (responseCode.code === 200);"
},
"example": [
"tests['response code is 200'] = (responseCode.code === 200);"
]
},
"type": {
"type": "string",
"example": "text/javascript"
}
}
}
}
},
"example": [
{
"listen": "test",
"script": {
"exec": [
"tests['response code is 200'] = (responseCode.code === 200);"
],
"type": "text/javascript"
}
}
]
},
"name": {
"type": "string",
"example": "A simple GET request"
Expand Down Expand Up @@ -2260,6 +2302,17 @@
},
"example": [
{
"event": [
{
"listen": "test",
"script": {
"exec": [
"tests['response code is 200'] = (responseCode.code === 200);"
],
"type": "text/javascript"
}
}
],
"name": "A simple GET request",
"request": {
"method": "GET",
Expand All @@ -2282,6 +2335,23 @@
"method": "POST",
"url": "https://postman-echo.com/post"
}
},
{
"name": "A simple POST request with JSON body",
"request": {
"body": {
"mode": "raw",
"raw": "{\"text\":\"Duis posuere augue vel cursus pharetra. In luctus a ex nec pretium...\"}"
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"method": "POST",
"url": "https://postman-echo.com/post"
}
}
]
}
Expand All @@ -2296,7 +2366,8 @@
"name": "Sample Postman Collection",
"order": [
"4d9134be-e8bf-4693-9cd7-1c0fc66ae739",
"141ba274-cc50-4377-a59c-e080066f375e"
"141ba274-cc50-4377-a59c-e080066f375e",
"4511ca8b-0bc7-430f-b894-a7ec1036f322"
],
"requests": [
{
Expand All @@ -2321,6 +2392,17 @@
"name": "A simple POST request",
"rawModeData": "Duis posuere augue vel cursus pharetra. In luctus a ex nec pretium...",
"url": "https://postman-echo.com/post"
},
{
"collectionId": "877b9dae-a50e-4152-9b89-870c37216f78",
"data": [],
"dataMode": "raw",
"headers": "Content-Type: application/json",
"id": "4511ca8b-0bc7-430f-b894-a7ec1036f322",
"method": "POST",
"name": "A simple POST request with JSON body",
"rawModeData": "{\"text\":\"Duis posuere augue vel cursus pharetra. In luctus a ex nec pretium...\"}",
"url": "https://postman-echo.com/post"
}
]
}
Expand All @@ -2334,6 +2416,17 @@
},
"item": [
{
"event": [
{
"listen": "test",
"script": {
"exec": [
"tests['response code is 200'] = (responseCode.code === 200);"
],
"type": "text/javascript"
}
}
],
"name": "A simple GET request",
"request": {
"method": "GET",
Expand All @@ -2356,6 +2449,23 @@
"method": "POST",
"url": "https://postman-echo.com/post"
}
},
{
"name": "A simple POST request with JSON body",
"request": {
"body": {
"mode": "raw",
"raw": "{\"text\":\"Duis posuere augue vel cursus pharetra. In luctus a ex nec pretium...\"}"
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"method": "POST",
"url": "https://postman-echo.com/post"
}
}
]
}
Expand Down

0 comments on commit cd90f5c

Please sign in to comment.