diff --git a/CHANGELOG.md b/CHANGELOG.md index 258e233..0cfe457 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [2.1.0] - 2024-10-8 +## [2.3.0] - 2026-03-05 + +### Added +- Added support for OpenAPI 3.1. + +## [2.1.0] - 2024-10-08 ## Added diff --git a/package.json b/package.json index 7458a1f..9bc5297 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi2postman", - "version": "2.2.7", + "version": "2.3.0", "description": "openapi2postman", "bin": { "o2p": "index.js" diff --git a/seeds/generatorEndpointsResult.json b/seeds/generatorEndpointsResult.json index 0f0ea6f..1160f27 100644 --- a/seeds/generatorEndpointsResult.json +++ b/seeds/generatorEndpointsResult.json @@ -1,282 +1,282 @@ -[ - { - "name":"/pets-200", - "aux":{ - "status":"200", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false +[ + { + "name": "/pets-200", + "path": "/pets", + "count": 1, + "microcks": false, + "aux": { + "status": "200", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"GET", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets" + ] } - }, - { - "name":"/pets-500", - "aux":{ - "status":"500", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets-500", + "path": "/pets", + "count": 1, + "microcks": false, + "aux": { + "status": "500", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"GET", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets" + ] } - }, - { - "name":"/pets-200", - "aux":{ - "status":"200", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets-200", + "path": "/pets", + "count": 1, + "microcks": false, + "aux": { + "status": "200", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"POST", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets" + ] } - }, - { - "name":"/pets-500", - "aux":{ - "status":"500", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets-500", + "path": "/pets", + "count": 1, + "microcks": false, + "aux": { + "status": "500", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"POST", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets" + ] } - }, - { - "name":"/pets/{id}-200", - "aux":{ - "status":"200", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets/{id}-200", + "path": "/pets/{id}", + "count": 1, + "microcks": false, + "aux": { + "status": "200", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"GET", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets/{id}", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets/{id}" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{id}", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets/{id}" + ] } - }, - { - "name":"/pets/{id}-500", - "aux":{ - "status":"500", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets/{id}-500", + "path": "/pets/{id}", + "count": 1, + "microcks": false, + "aux": { + "status": "500", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"GET", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets/{id}", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets/{id}" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{id}", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets/{id}" + ] } - }, - { - "name":"/pets/{id}-204", - "aux":{ - "status":"204", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets/{id}-204", + "path": "/pets/{id}", + "count": 1, + "microcks": false, + "aux": { + "status": "204", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "DELETE", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"DELETE", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets/{id}", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets/{id}" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{id}", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets/{id}" + ] } - }, - { - "name":"/pets/{id}-500", - "aux":{ - "status":"500", - "body":false, - "consumes":false, - "bodyResponse":false, - "authorization":false, - "summary":false, - "queryParams":false, - "pathParameter":false + } + }, + { + "name": "/pets/{id}-500", + "path": "/pets/{id}", + "count": 1, + "microcks": false, + "aux": { + "status": "500", + "body": false, + "consumes": false, + "bodyResponse": false, + "authorization": false, + "summary": false, + "queryParams": false, + "pathParameter": null, + "pathParameterExample": "" + }, + "response": [], + "request": { + "method": "DELETE", + "header": [], + "body": { + "mode": "raw", + "raw": "" }, - "response":[ - - ], - "request":{ - "method":"DELETE", - "header":[ - - ], - "body":{ - "mode":"raw", - "raw":"" - }, - "url":{ - "raw":"{{host}}{{port}}{{basePath}}/pets/{id}", - "host":[ - "{{host}}{{port}}{{basePath}}" - ], - "path":[ - "/pets/{id}" - ] - } + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{id}", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "pets/{id}" + ] } - } + } + } ] \ No newline at end of file diff --git a/seeds/generatorFoldersResult.json b/seeds/generatorFoldersResult.json index 5bd601a..a7f4121 100644 --- a/seeds/generatorFoldersResult.json +++ b/seeds/generatorFoldersResult.json @@ -6,7 +6,7 @@ "name":"001.001.uploads an image", "item":[ { - "name":"TC.001.001.200 uploads an image ", + "name":"TC.001.001.200", "aux":{ "status":"200", "body":false, @@ -97,7 +97,7 @@ "name":"001.002.Update an existing pet", "item":[ { - "name":"TC.001.002.400 Update an existing pet without.name", + "name":"TC.001.002.400 without.name", "aux":{ "status":"400", "body":{ @@ -272,7 +272,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet without.photoUrls", + "name":"TC.001.002.400 without.photoUrls", "aux":{ "status":"400", "body":{ @@ -444,7 +444,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.id.wrong", + "name":"TC.001.002.400 with.id.wrong", "aux":{ "status":"400", "body":{ @@ -619,7 +619,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.category.wrong", + "name":"TC.001.002.400 with.category.wrong", "aux":{ "status":"400", "body":{ @@ -794,7 +794,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.category.id.wrong", + "name":"TC.001.002.400 with.category.id.wrong", "aux":{ "status":"400", "body":{ @@ -969,7 +969,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.category.name.wrong", + "name":"TC.001.002.400 with.category.name.wrong", "aux":{ "status":"400", "body":{ @@ -1144,7 +1144,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.name.wrong", + "name":"TC.001.002.400 with.name.wrong", "aux":{ "status":"400", "body":{ @@ -1319,7 +1319,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.photoUrls.wrong", + "name":"TC.001.002.400 with.photoUrls.wrong", "aux":{ "status":"400", "body":{ @@ -1491,7 +1491,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.tags.wrong", + "name":"TC.001.002.400 with.tags.wrong", "aux":{ "status":"400", "body":{ @@ -1656,7 +1656,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.tags.id.wrong", + "name":"TC.001.002.400 with.tags.id.wrong", "aux":{ "status":"400", "body":{ @@ -1831,7 +1831,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.tags.name.wrong", + "name":"TC.001.002.400 with.tags.name.wrong", "aux":{ "status":"400", "body":{ @@ -2006,7 +2006,7 @@ ] }, { - "name":"TC.001.002.400 Update an existing pet with.status.wrong", + "name":"TC.001.002.400 with.status.wrong", "aux":{ "status":"400", "body":{ @@ -2191,7 +2191,7 @@ "name":"001.003.Finds Pets by status", "item":[ { - "name":"TC.001.003.200 Finds Pets by status ", + "name":"TC.001.003.200", "aux":{ "status":"200", "body":false, @@ -2365,7 +2365,7 @@ ] }, { - "name":"TC.001.003.400 Finds Pets by status .without.status", + "name":"TC.001.003.400 .without.status", "aux":{ "status":"400", "body":false, @@ -2542,7 +2542,7 @@ "name":"001.004.Finds Pets by tags", "item":[ { - "name":"TC.001.004.200 Finds Pets by tags ", + "name":"TC.001.004.200", "aux":{ "status":"200", "body":false, @@ -2615,7 +2615,7 @@ ] }, { - "name":"TC.001.004.400 Finds Pets by tags .without.tags", + "name":"TC.001.004.400 .without.tags", "aux":{ "status":"400", "body":false, @@ -2691,7 +2691,7 @@ "name":"001.005.Find pet by ID", "item":[ { - "name":"TC.001.005.200 Find pet by ID ", + "name":"TC.001.005.200", "aux":{ "status":"200", "body":false, @@ -2857,7 +2857,7 @@ ] }, { - "name":"TC.001.005.404 Find pet by ID ", + "name":"TC.001.005.404", "aux":{ "status":"404", "body":false, @@ -3030,7 +3030,7 @@ "name":"002.001.Place an order for a pet", "item":[ { - "name":"TC.002.001.200 Place an order for a pet ", + "name":"TC.002.001.200", "aux":{ "status":"200", "body":{ @@ -3223,7 +3223,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.id.wrong", + "name":"TC.002.001.400 with.id.wrong", "aux":{ "status":"400", "body":{ @@ -3409,7 +3409,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.petId.wrong", + "name":"TC.002.001.400 with.petId.wrong", "aux":{ "status":"400", "body":{ @@ -3595,7 +3595,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.quantity.wrong", + "name":"TC.002.001.400 with.quantity.wrong", "aux":{ "status":"400", "body":{ @@ -3781,7 +3781,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.shipDate.wrong", + "name":"TC.002.001.400 with.shipDate.wrong", "aux":{ "status":"400", "body":{ @@ -3967,7 +3967,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.status.wrong", + "name":"TC.002.001.400 with.status.wrong", "aux":{ "status":"400", "body":{ @@ -4153,7 +4153,7 @@ ] }, { - "name":"TC.002.001.400 Place an order for a pet with.complete.wrong", + "name":"TC.002.001.400 with.complete.wrong", "aux":{ "status":"400", "body":{ @@ -4349,7 +4349,7 @@ "name":"002.002.Returns pet inventories by status", "item":[ { - "name":"TC.002.002.200 Returns pet inventories by status ", + "name":"TC.002.002.200", "aux":{ "status":"200", "body":false, @@ -4427,7 +4427,7 @@ "name":"002.003.Find purchase order by ID", "item":[ { - "name":"TC.002.003.200 Find purchase order by ID ", + "name":"TC.002.003.200", "aux":{ "status":"200", "body":false, @@ -4553,7 +4553,7 @@ ] }, { - "name":"TC.002.003.404 Find purchase order by ID ", + "name":"TC.002.003.404", "aux":{ "status":"404", "body":false, @@ -4681,7 +4681,7 @@ "name":"002.004.Delete purchase order by ID", "item":[ { - "name":"TC.002.004.404 Delete purchase order by ID ", + "name":"TC.002.004.404", "aux":{ "status":"404", "body":false, @@ -4750,7 +4750,7 @@ "name":"003.001.Updated user", "item":[ { - "name":"TC.003.001.400 Updated user with.id.wrong", + "name":"TC.003.001.400 with.id.wrong", "aux":{ "status":"400", "body":{ @@ -4844,7 +4844,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.username.wrong", + "name":"TC.003.001.400 with.username.wrong", "aux":{ "status":"400", "body":{ @@ -4938,7 +4938,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.firstName.wrong", + "name":"TC.003.001.400 with.firstName.wrong", "aux":{ "status":"400", "body":{ @@ -5032,7 +5032,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.lastName.wrong", + "name":"TC.003.001.400 with.lastName.wrong", "aux":{ "status":"400", "body":{ @@ -5126,7 +5126,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.email.wrong", + "name":"TC.003.001.400 with.email.wrong", "aux":{ "status":"400", "body":{ @@ -5220,7 +5220,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.password.wrong", + "name":"TC.003.001.400 with.password.wrong", "aux":{ "status":"400", "body":{ @@ -5314,7 +5314,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.phone.wrong", + "name":"TC.003.001.400 with.phone.wrong", "aux":{ "status":"400", "body":{ @@ -5408,7 +5408,7 @@ ] }, { - "name":"TC.003.001.400 Updated user with.userStatus.wrong", + "name":"TC.003.001.400 with.userStatus.wrong", "aux":{ "status":"400", "body":{ @@ -5502,7 +5502,7 @@ ] }, { - "name":"TC.003.001.404 Updated user ", + "name":"TC.003.001.404", "aux":{ "status":"404", "body":{ @@ -5605,7 +5605,7 @@ "name":"003.002.Logs user into the system", "item":[ { - "name":"TC.003.002.200 Logs user into the system ", + "name":"TC.003.002.200", "aux":{ "status":"200", "body":false, @@ -5680,7 +5680,7 @@ ] }, { - "name":"TC.003.002.400 Logs user into the system .without.username", + "name":"TC.003.002.400 .without.username", "aux":{ "status":"400", "body":false, @@ -5748,7 +5748,7 @@ ] }, { - "name":"TC.003.002.400 Logs user into the system .without.password", + "name":"TC.003.002.400 .without.password", "aux":{ "status":"400", "body":false, @@ -5826,7 +5826,7 @@ "name":"003.003.Get user by user name", "item":[ { - "name":"TC.003.003.200 Get user by user name ", + "name":"TC.003.003.200", "aux":{ "status":"200", "body":false, @@ -5924,7 +5924,7 @@ ] }, { - "name":"TC.003.003.404 Get user by user name ", + "name":"TC.003.003.404", "aux":{ "status":"404", "body":false, @@ -6024,7 +6024,7 @@ "name":"003.004.Delete user", "item":[ { - "name":"TC.003.004.404 Delete user ", + "name":"TC.003.004.404", "aux":{ "status":"404", "body":false, diff --git a/seeds/generatorTestBodyResult.json b/seeds/generatorTestBodyResult.json index 30a5940..f06b93a 100644 --- a/seeds/generatorTestBodyResult.json +++ b/seeds/generatorTestBodyResult.json @@ -1 +1,99 @@ -{"name":"/pets/{id}-200","aux":{"status":"200","body":false,"consumes":false,"bodyResponse":{"200":{"type":"object","required":["name","id"],"properties":{"name":{"type":"string"},"tag":{"type":"string"},"id":{"type":"integer","format":"int64"}}},"500":{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}}},"response":[],"request":{"method":"GET","header":[],"body":{"mode":"raw","raw":""},"url":{"raw":"{{host}}{{port}}{{basePath}}/pets/{{id}}","host":["{{host}}{{port}}{{basePath}}"],"path":["/pets/{{id}}"]}},"event":[{"listen":"test","script":{"id":"63142ff5-28a4-40b2-8314-dcbeeaa89325","type":"text/javascript","exec":["pm.test(\"Status code is 200\", function () {"," pm.response.to.have.status(200);","});","","var json = JSON.parse(responseBody);","","var schema = {\"type\":\"object\",\"required\":[\"name\",\"id\"],\"properties\":{\"name\":{\"type\":\"string\"},\"tag\":{\"type\":\"string\"},\"id\":{\"type\":\"integer\",\"format\":\"int64\"}}};","","pm.test('Schema is valid', function() {"," pm.expect(tv4.validate(json, schema)).to.be.true;","});"]}}]} \ No newline at end of file +{ + "name": "/pets/{id}-200", + "aux": { + "status": "200", + "body": false, + "consumes": false, + "bodyResponse": { + "200": { + "type": "object", + "required": [ + "name", + "id" + ], + "properties": { + "name": { + "type": "string" + }, + "tag": { + "type": "string" + }, + "id": { + "type": "integer", + "format": "int64" + } + } + }, + "500": { + "type": "object", + "required": [ + "code", + "message" + ], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + } + } + } + }, + "response": [], + "request": { + "method": "GET", + "header": [], + "body": { + "mode": "raw", + "raw": "" + }, + "url": { + "raw": "{{host}}{{port}}{{basePath}}/pets/{{id}}", + "host": [ + "{{host}}{{port}}{{basePath}}" + ], + "path": [ + "/pets/{{id}}" + ] + } + }, + "event": [ + { + "listen": "test", + "script": { + "id": "63142ff5-28a4-40b2-8314-dcbeeaa89325", + "type": "text/javascript", + "exec": [ + "pm.test(\"Status code is 200\", function () {", + " pm.response.to.have.status(200);", + "});", + "", + "var json = null;", + "", + "try {", + "\tjson = pm.response.json();", + "\t// Guardar variables en environment", + "\t// pm.environment.set('nombreVariable', json.data.variable);", + "} catch (e) {", + "\tpm.test(\"A JSON response was expected\", function() {", + "\t\tpm.expect(json, \"responseBody no es un JSON\").not.to.be.null;", + "\t});", + "}", + "", + "var schema = {\"type\":\"object\",\"required\":[\"name\",\"id\"],\"properties\":{\"name\":{\"type\":\"string\"},\"tag\":{\"type\":\"string\"},\"id\":{\"type\":\"integer\",\"format\":\"int64\"}}};", + "", + "var Ajv = require(\"ajv\");", + "var ajv = new Ajv({ logger: console, allErrors: false, unknownFormats: ['int32', 'int64', 'float', 'double', 'password'] });", + "var validate = ajv.compile(schema);", + "var valid = validate(json);", + "pm.test(\"Schema is valid according to AJV\", function () {", + "\tpm.expect(valid, JSON.stringify(validate.errors, null, 2)).to.be.true;", + "});" + ] + } + } + ] +} \ No newline at end of file diff --git a/seeds/parserAuthorizationInitialOpenApi3.1.json b/seeds/parserAuthorizationInitialOpenApi3.1.json new file mode 100644 index 0000000..0c56c67 --- /dev/null +++ b/seeds/parserAuthorizationInitialOpenApi3.1.json @@ -0,0 +1,290 @@ +{ + "openapi": "3.1.0", + "jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "description": "A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.1 specification", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "Swagger API Team", + "email": "apiteam@swagger.io", + "url": "http://swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "https://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "servers": [ + { + "url": "http://petstore.swagger.io/api" + } + ], + "security": [ + { + "ApiKeyAuth": [] + } + ], + "paths": { + "/pets": { + "get": { + "security": [ + { + "OAuth2": ["admin"] + } + ], + "description": "Returns all pets from the system that the user has access to", + "operationId": "findPets", + "parameters": [ + { + "name": "tags", + "in": "query", + "description": "tags to filter by", + "required": false, + "style": "form", + "explode": true, + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + { + "name": "limit", + "in": "query", + "description": "maximum number of results to return", + "required": false, + "schema": { + "type": ["integer", "null"], + "format": "int32", + "minimum": 1 + } + } + ], + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Pet" + } + } + } + } + }, + "default": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + }, + "post": { + "description": "Creates a new pet in the store. Duplicates are allowed", + "operationId": "addPet", + "requestBody": { + "description": "Pet to add to the store", + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewPet" + } + } + } + }, + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + } + }, + "default": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + }, + "/pets/{id}": { + "get": { + "description": "Returns a pet based on a single ID", + "operationId": "findPetById", + "parameters": [ + { + "$ref": "#/components/parameters/PetId" + } + ], + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + } + }, + "404": { + "description": "pet not found", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + }, + "delete": { + "description": "Deletes a single pet based on the ID supplied", + "operationId": "deletePet", + "parameters": [ + { + "$ref": "#/components/parameters/PetId" + } + ], + "responses": { + "204": { + "description": "pet deleted" + }, + "default": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components": { + "parameters": { + "PetId": { + "name": "id", + "in": "path", + "description": "ID of pet", + "required": true, + "schema": { + "type": "integer", + "format": "int64", + "minimum": 1 + } + } + }, + "securitySchemes": { + "BasicAuth": { + "type": "http", + "scheme": "basic" + }, + "BearerAuth": { + "type": "http", + "scheme": "bearer" + }, + "ApiKeyAuth": { + "type": "apiKey", + "in": "header", + "name": "X-API-Key" + }, + "OpenID": { + "type": "openIdConnect", + "openIdConnectUrl": "https://example.com/.well-known/openid-configuration" + }, + "OAuth2": { + "type": "oauth2", + "flows": { + "authorizationCode": { + "authorizationUrl": "https://example.com/oauth/authorize", + "tokenUrl": "https://example.com/oauth/token", + "scopes": { + "read": "Grants read access", + "write": "Grants write access", + "admin": "Grants access to admin operations" + } + } + } + } + }, + "schemas": { + "Pet": { + "allOf": [ + { + "$ref": "#/components/schemas/NewPet" + }, + { + "type": "object", + "required": ["id"], + "properties": { + "id": { + "type": "integer", + "format": "int64" + } + }, + "unevaluatedProperties": false + } + ] + }, + "NewPet": { + "type": "object", + "required": ["name"], + "properties": { + "name": { + "type": "string", + "minLength": 1 + }, + "tag": { + "type": ["string", "null"], + "examples": ["dog", "cat", null] + }, + "status": { + "type": "string", + "const": "available" + } + }, + "unevaluatedProperties": false + }, + "Error": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + }, + "unevaluatedProperties": false + } + } + } +} \ No newline at end of file diff --git a/seeds/parserEndpointsGoodOpenApi3Result.json b/seeds/parserEndpointsGoodOpenApi3Result.json new file mode 100644 index 0000000..0c20008 --- /dev/null +++ b/seeds/parserEndpointsGoodOpenApi3Result.json @@ -0,0 +1,5 @@ +[ + { "verb": "GET", "path": "/pets" }, + { "verb": "POST", "path": "/pets" }, + { "verb": "GET", "path": "/pets/{petId}" } +] \ No newline at end of file diff --git a/seeds/parserEndpointsGoodSwagger2Result.json b/seeds/parserEndpointsGoodSwagger2Result.json new file mode 100644 index 0000000..329c8cd --- /dev/null +++ b/seeds/parserEndpointsGoodSwagger2Result.json @@ -0,0 +1,6 @@ +[ + { "verb": "GET", "path": "/pets" }, + { "verb": "POST", "path": "/pets" }, + { "verb": "GET", "path": "/pets/{id}" }, + { "verb": "DELETE", "path": "/pets/{id}" } +] \ No newline at end of file diff --git a/seeds/parserInitialGoodOpenApi3.1.1.json b/seeds/parserInitialGoodOpenApi3.1.1.json new file mode 100644 index 0000000..0df0a71 --- /dev/null +++ b/seeds/parserInitialGoodOpenApi3.1.1.json @@ -0,0 +1,178 @@ +{ + "openapi":"3.1.2", + "info":{ + "version":"1.0.0", + "title":"Swagger Petstore", + "license":{ + "name":"MIT" + } + }, + "servers":[ + { + "url":"http://petstore.swagger.io/v1" + } + ], + "paths":{ + "/pets":{ + "get":{ + "summary":"List all pets", + "operationId":"listPets", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"limit", + "in":"query", + "description":"How many items to return at one time (max 100)", + "required":false, + "schema":{ + "type":"integer", + "format":"int32" + } + } + ], + "responses":{ + "200":{ + "description":"A paged array of pets", + "headers":{ + "x-next":{ + "description":"A link to the next page of responses", + "schema":{ + "type":"string" + } + } + }, + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pets" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + }, + "post":{ + "summary":"Create a pet", + "operationId":"createPets", + "tags":[ + "pets" + ], + "responses":{ + "201":{ + "description":"Null response" + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + }, + "/pets/{petId}":{ + "get":{ + "summary":"Info for a specific pet", + "operationId":"showPetById", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"petId", + "in":"path", + "required":true, + "description":"The id of the pet to retrieve", + "schema":{ + "type":"string", + "example":"asdf" + } + } + ], + "responses":{ + "200":{ + "description":"Expected response to a valid request", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pet" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components":{ + "schemas":{ + "Pet":{ + "type":"object", + "required":[ + "id", + "name" + ], + "properties":{ + "id":{ + "type":"integer", + "format":"int64" + }, + "name":{ + "type":"string" + }, + "tag":{ + "type":"string" + } + } + }, + "Pets":{ + "type":"array", + "items":{ + "$ref":"#/components/schemas/Pet" + } + }, + "Error":{ + "type":"object", + "required":[ + "code", + "message" + ], + "properties":{ + "code":{ + "type":"integer", + "format":"int32" + }, + "message":{ + "type":"string" + } + } + } + } + } + } \ No newline at end of file diff --git a/seeds/parserInitialGoodOpenApi3.1.2.json b/seeds/parserInitialGoodOpenApi3.1.2.json new file mode 100644 index 0000000..58b4511 --- /dev/null +++ b/seeds/parserInitialGoodOpenApi3.1.2.json @@ -0,0 +1,178 @@ +{ + "openapi":"3.1.1", + "info":{ + "version":"1.0.0", + "title":"Swagger Petstore", + "license":{ + "name":"MIT" + } + }, + "servers":[ + { + "url":"http://petstore.swagger.io/v1" + } + ], + "paths":{ + "/pets":{ + "get":{ + "summary":"List all pets", + "operationId":"listPets", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"limit", + "in":"query", + "description":"How many items to return at one time (max 100)", + "required":false, + "schema":{ + "type":"integer", + "format":"int32" + } + } + ], + "responses":{ + "200":{ + "description":"A paged array of pets", + "headers":{ + "x-next":{ + "description":"A link to the next page of responses", + "schema":{ + "type":"string" + } + } + }, + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pets" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + }, + "post":{ + "summary":"Create a pet", + "operationId":"createPets", + "tags":[ + "pets" + ], + "responses":{ + "201":{ + "description":"Null response" + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + }, + "/pets/{petId}":{ + "get":{ + "summary":"Info for a specific pet", + "operationId":"showPetById", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"petId", + "in":"path", + "required":true, + "description":"The id of the pet to retrieve", + "schema":{ + "type":"string", + "example":"asdf" + } + } + ], + "responses":{ + "200":{ + "description":"Expected response to a valid request", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pet" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components":{ + "schemas":{ + "Pet":{ + "type":"object", + "required":[ + "id", + "name" + ], + "properties":{ + "id":{ + "type":"integer", + "format":"int64" + }, + "name":{ + "type":"string" + }, + "tag":{ + "type":"string" + } + } + }, + "Pets":{ + "type":"array", + "items":{ + "$ref":"#/components/schemas/Pet" + } + }, + "Error":{ + "type":"object", + "required":[ + "code", + "message" + ], + "properties":{ + "code":{ + "type":"integer", + "format":"int32" + }, + "message":{ + "type":"string" + } + } + } + } + } + } \ No newline at end of file diff --git a/seeds/parserInitialGoodOpenApi3.1.json b/seeds/parserInitialGoodOpenApi3.1.json new file mode 100644 index 0000000..325ee25 --- /dev/null +++ b/seeds/parserInitialGoodOpenApi3.1.json @@ -0,0 +1,178 @@ +{ + "openapi":"3.1.0", + "info":{ + "version":"1.0.0", + "title":"Swagger Petstore", + "license":{ + "name":"MIT" + } + }, + "servers":[ + { + "url":"https://petstore.swagger.io/v1" + } + ], + "paths":{ + "/pets":{ + "get":{ + "summary":"List all pets", + "operationId":"listPets", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"limit", + "in":"query", + "description":"How many items to return at one time (max 100)", + "required":false, + "schema":{ + "type":"integer", + "format":"int32" + } + } + ], + "responses":{ + "200":{ + "description":"A paged array of pets", + "headers":{ + "x-next":{ + "description":"A link to the next page of responses", + "schema":{ + "type":"string" + } + } + }, + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pets" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + }, + "post":{ + "summary":"Create a pet", + "operationId":"createPets", + "tags":[ + "pets" + ], + "responses":{ + "201":{ + "description":"Null response" + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + }, + "/pets/{petId}":{ + "get":{ + "summary":"Info for a specific pet", + "operationId":"showPetById", + "tags":[ + "pets" + ], + "parameters":[ + { + "name":"petId", + "in":"path", + "required":true, + "description":"The id of the pet to retrieve", + "schema":{ + "type":"string", + "example":"asdf" + } + } + ], + "responses":{ + "200":{ + "description":"Expected response to a valid request", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Pet" + } + } + } + }, + "default":{ + "description":"unexpected error", + "content":{ + "application/json":{ + "schema":{ + "$ref":"#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components":{ + "schemas":{ + "Pet":{ + "type":"object", + "required":[ + "id", + "name" + ], + "properties":{ + "id":{ + "type":"integer", + "format":"int64" + }, + "name":{ + "type":"string" + }, + "tag":{ + "type":"string" + } + } + }, + "Pets":{ + "type":"array", + "items":{ + "$ref":"#/components/schemas/Pet" + } + }, + "Error":{ + "type":"object", + "required":[ + "code", + "message" + ], + "properties":{ + "code":{ + "type":"integer", + "format":"int32" + }, + "message":{ + "type":"string" + } + } + } + } + } + } \ No newline at end of file diff --git a/seeds/parserInitialGoodOpenApiExpanded3.1.json b/seeds/parserInitialGoodOpenApiExpanded3.1.json new file mode 100644 index 0000000..31e4567 --- /dev/null +++ b/seeds/parserInitialGoodOpenApiExpanded3.1.json @@ -0,0 +1,214 @@ +{ + "openapi": "3.1.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore" + }, + "servers": [ + { + "url": "http://petstore.swagger.io/api" + } + ], + "paths": { + "/pets": { + "get": { + "operationId": "findPets", + "parameters": [ + { + "name": "tags", + "in": "query", + "required": false, + "schema": { + "type": "array", + "items": { + "type": "string" + } + } + }, + { + "name": "limit", + "in": "query", + "required": true, + "schema": { + "type": "integer", + "format": "int32" + } + } + ], + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Pet" + } + } + } + } + }, + "500": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + }, + "post": { + "operationId": "addPet", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewPet" + } + } + } + }, + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + } + }, + "500": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + }, + "/pets/{id}": { + "get": { + "operationId": "findPetById", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + } + }, + "500": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + }, + "delete": { + "operationId": "deletePet", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "204": { + "description": "pet deleted" + }, + "500": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Pet": { + "allOf": [ + { + "$ref": "#/components/schemas/NewPet" + }, + { + "type": "object", + "required": ["id"], + "properties": { + "id": { + "type": "integer", + "format": "int64" + } + } + } + ] + }, + "NewPet": { + "type": "object", + "required": ["name"], + "properties": { + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + }, + "Error": { + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + } + } + } + } +} \ No newline at end of file diff --git a/seeds/parserInitialGoodOpenApi3Expanded.json b/seeds/parserInitialGoodOpenApiExpanded3.json similarity index 100% rename from seeds/parserInitialGoodOpenApi3Expanded.json rename to seeds/parserInitialGoodOpenApiExpanded3.json diff --git a/seeds/parserInitialGoodResultAllOfLoop.json b/seeds/parserInitialGoodResultAllOfLoop.json new file mode 100644 index 0000000..edbdab3 --- /dev/null +++ b/seeds/parserInitialGoodResultAllOfLoop.json @@ -0,0 +1,17 @@ +{ + "type": "object", + "required": ["red", "orange"], + "properties": { + "red": { "type": "string", "maxLength": 50 }, + "orange": { "type": "number", "maxLength": 9 }, + "water": { + "type": "object", + "required": ["paramAiden"], + "properties": { + "paramAiden": { "type": "string" }, + "paramBiden": { "type": "string" }, + "paramCiden": { "type": "string" } + } + } + } +} \ No newline at end of file diff --git a/seeds/parserInitialGoodResultAllOfString.json b/seeds/parserInitialGoodResultAllOfString.json new file mode 100644 index 0000000..e39b116 --- /dev/null +++ b/seeds/parserInitialGoodResultAllOfString.json @@ -0,0 +1,6 @@ +{ + "type": "object", + "properties": { + "red": { "type": "string", "maxLength": 50 } + } +} \ No newline at end of file diff --git a/seeds/parserInitialGoodResultPetRequest.json b/seeds/parserInitialGoodResultPetRequest.json new file mode 100644 index 0000000..b7852ad --- /dev/null +++ b/seeds/parserInitialGoodResultPetRequest.json @@ -0,0 +1,8 @@ +{ + "type": "object", + "required": ["name"], + "properties": { + "name": { "type": "string" }, + "tag": { "type": "string" } + } +} \ No newline at end of file diff --git a/seeds/parserInitialGoodResultRespArray.json b/seeds/parserInitialGoodResultRespArray.json new file mode 100644 index 0000000..bff51d8 --- /dev/null +++ b/seeds/parserInitialGoodResultRespArray.json @@ -0,0 +1,22 @@ +{ + "200": { + "type": "array", + "items": { + "type": "object", + "required": ["name", "id"], + "properties": { + "name": { "type": "string" }, + "tag": { "type": "string" }, + "id": { "type": "integer", "format": "int64" } + } + } + }, + "500": { + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { "type": "integer", "format": "int32" }, + "message": { "type": "string" } + } + } +} \ No newline at end of file diff --git a/seeds/parserInitialGoodResultRespObject.json b/seeds/parserInitialGoodResultRespObject.json new file mode 100644 index 0000000..48d8053 --- /dev/null +++ b/seeds/parserInitialGoodResultRespObject.json @@ -0,0 +1,19 @@ +{ + "200": { + "type": "object", + "required": ["name", "id"], + "properties": { + "name": { "type": "string" }, + "tag": { "type": "string" }, + "id": { "type": "integer", "format": "int64" } + } + }, + "500": { + "type": "object", + "required": ["code", "message"], + "properties": { + "code": { "type": "integer", "format": "int32" }, + "message": { "type": "string" } + } + } +} \ No newline at end of file diff --git a/seeds/parserMicroksinitialGood3.1.json b/seeds/parserMicroksinitialGood3.1.json new file mode 100644 index 0000000..1abbcdb --- /dev/null +++ b/seeds/parserMicroksinitialGood3.1.json @@ -0,0 +1,117 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "Swagger Petstore 2.0", + "description": "This is a sample server Petstore server.", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "email": "apiteam@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0.0" + }, + "servers": [ + { + "url": "https://petstore.swagger.io/v2" + }, + { + "url": "http://petstore.swagger.io/v2" + } + ], + "paths": { + "/user": { + "post": { + "tags": ["user"], + "summary": "Create user", + "operationId": "createUser", + "requestBody": { + "required": true, + "content": { + "*/*": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + }, + "responses": { + "default": { + "description": "successful operation" + } + } + } + }, + "/user/{username}": { + "get": { + "tags": ["user"], + "summary": "Get user by user name", + "operationId": "getUserByName", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/User" + } + } + } + } + }, + "x-microcks-operation": { + "delay": 100, + "dispatcher": "SCRIPT", + "dispatcherRules": "def headers = mockRequest.getRequestHeaders(); if (headers.hasValues(\"X-Microcks-Response-Name\")) { return headers.get(\"X-Microcks-Response-Name\", \"null\") }" + } + } + } + }, + "components": { + "schemas": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "username": { + "type": "string" + }, + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "email": { + "type": "string" + }, + "password": { + "type": "string" + }, + "phone": { + "type": "string" + }, + "userStatus": { + "type": "integer", + "format": "int32" + } + } + } + } + } +} \ No newline at end of file diff --git a/seeds/parserMicroksinitialGood.json b/seeds/parserMicroksinitialGood3.json similarity index 100% rename from seeds/parserMicroksinitialGood.json rename to seeds/parserMicroksinitialGood3.json diff --git a/seeds/parserSummaryOpenApi3.1.json b/seeds/parserSummaryOpenApi3.1.json new file mode 100644 index 0000000..93a9397 --- /dev/null +++ b/seeds/parserSummaryOpenApi3.1.json @@ -0,0 +1,117 @@ +{ + "openapi": "3.1.0", + "jsonSchemaDialect": "https://spec.openapis.org/oas/3.1/dialect/base", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "description": "A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.1 specification", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "Swagger API Team", + "email": "apiteam@swagger.io", + "url": "http://swagger.io" + }, + "license": { + "name": "Apache 2.0", + "identifier": "Apache-2.0" + } + }, + "servers": [ + { + "url": "http://petstore.swagger.io/api" + } + ], + "security": [ + { + "ApiKeyAuth": [] + } + ], + "paths": { + "/pets": { + "get": { + "summary": "Pets list", + "operationId": "findPets", + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Pet" + } + } + } + } + } + } + }, + "post": { + "summary": "Create pet", + "operationId": "addPet", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NewPet" + } + } + } + }, + "responses": { + "200": { + "description": "pet response", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pet" + } + } + } + } + } + } + } + }, + "components": { + "securitySchemes": { + "ApiKeyAuth": { + "type": "apiKey", + "in": "header", + "name": "X-API-Key" + } + }, + "schemas": { + "Pet": { + "allOf": [ + { + "$ref": "#/components/schemas/NewPet" + }, + { + "type": "object", + "required": ["id"], + "properties": { + "id": { + "type": "integer" + } + } + } + ] + }, + "NewPet": { + "type": "object", + "required": ["name"], + "properties": { + "name": { + "type": "string" + }, + "tag": { + "type": ["string", "null"] + } + } + } + } + } +} \ No newline at end of file diff --git a/src/generator/endpoints.js b/src/generator/endpoints.js index 7b66d12..05cd6c8 100644 --- a/src/generator/endpoints.js +++ b/src/generator/endpoints.js @@ -10,11 +10,11 @@ module.exports = function() { const items = []; _.forEach(endpoints, function(endpoint) { let path = endpoint.path; - let pathParameterSaved = false + let pathParameterSaved = false; _.forEach(endpoint.pathParameters, function(pathParameter) { pathParameterSaved = pathParameter; path = _.replace(path, '{'+pathParameter.name+'}', '{{'+pathParameter.name+'}}') - global.environmentVariables[endpoint.verb+endpoint.path.slice(1)+pathParameter.name] = require('../utils/exampleForField.js')(pathParameter,false) + globalThis.environmentVariables[endpoint.verb+endpoint.path.slice(1)+pathParameter.name] = require('../utils/exampleForField.js')(pathParameter,false) }); _.forEach(endpoint.status,function(response) { let item = { @@ -28,8 +28,8 @@ module.exports = function() { authorization: endpoint.authorization ? endpoint.authorization : false, summary: endpoint.summary ? endpoint.summary : false, queryParams: endpoint.queryParams ? endpoint.queryParams : false, - pathParameter: pathParameterSaved.name, - pathParameterExample: pathParameterSaved.example || '', + pathParameter: pathParameterSaved?.name || null, + pathParameterExample: pathParameterSaved?.example || '', }, count: countRequest(endpoint), response: [], @@ -66,7 +66,7 @@ module.exports = function() { } // Duplicar los endpoints para cada queryParameter dependiendo si el minimal endpoint está en true o false - if(!global.configurationFile.minimal_endpoints || global.configurationFile.minimal_endpoints === false){ + if(!globalThis.configurationFile?.minimal_endpoints || globalThis.configurationFile?.minimal_endpoints === false){ if (item.aux.status >= 200 && item.aux.status < 400 && item.aux.queryParams.length > 0) { addQueryParamEndpoint(item, items); } @@ -114,12 +114,14 @@ module.exports = function() { } function countRequest(endpoint){ - if(!global.configurationFile.generate_oneOf_anyOf){ + if (!globalThis.configurationFile?.generate_oneOf_anyOf) { return 1; } - if (endpoint.body) { - const found = _.find(endpoint.body, (value, key) => key === 'oneOf' || key === 'anyOf'); - if (found) { + + if (endpoint?.body && typeof endpoint.body === 'object') { + const found = endpoint.body.oneOf || endpoint.body.anyOf; + + if (Array.isArray(found)) { return found.length; } } diff --git a/src/generator/examples.js b/src/generator/examples.js index 917d839..6323b81 100644 --- a/src/generator/examples.js +++ b/src/generator/examples.js @@ -4,7 +4,11 @@ const _ = require("lodash"); module.exports = (function () { return function get(path, method, status) { - if (!_.isObject(global.definition.paths)) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return null; require('../utils/error.js')('paths is required'); } @@ -12,12 +16,12 @@ module.exports = (function () { path = '/' + path; } - if (!global.definition.paths[path]) { + if (!globalThis.definition.paths[path]) { return null; } const methodLower = _.toLower(method); - const endpoint = global.definition.paths[path][methodLower]; + const endpoint = globalThis.definition.paths[path][methodLower]; if (!endpoint) { return null; diff --git a/src/generator/folders.js b/src/generator/folders.js index 0d501cf..7e1fd8a 100644 --- a/src/generator/folders.js +++ b/src/generator/folders.js @@ -68,10 +68,10 @@ module.exports = function() { } for (let i in collection){ let countItem = 1 - let numerateRoot = _.padStart(countRoot, 2, '0') + '.' + let numerateRoot = _.padStart(countRoot, 3, '0') + '.' collection[i].name = numerateRoot + collection[i].name for (let j in collection[i].item) { - let numerateItem = numerateRoot + _.padStart(countItem, 2, '0') + '.' + let numerateItem = numerateRoot + _.padStart(countItem, 3, '0') + '.' collection[i].item[j].name = numerateItem + collection[i].item[j].name if (!collection[i].item[j].aux){ collection[i].item[j].aux = {} diff --git a/src/parser/endpoints.js b/src/parser/endpoints.js index 3df216f..2563283 100644 --- a/src/parser/endpoints.js +++ b/src/parser/endpoints.js @@ -4,23 +4,27 @@ const _ = require('lodash'); -module.exports = function() { - - return function get(){ - if (!_.isObject(global.definition.paths)) { - require('../utils/error.js')('paths is required'); - } +module.exports = function () { - const items = []; - _.forEach(global.definition.paths, function(pathInfo,path) { - _.forEach(pathInfo, function(verbInfo,verb) { - items.push({ - 'verb' : _.toUpper(verb), - 'path' : path - }); - }); - }); - return items; + return function get() { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return []; + require('../utils/error.js')('paths is required'); + } + + const items = []; + _.forEach(globalThis.definition.paths, function (pathInfo, path) { + _.forEach(pathInfo, function (verbInfo, verb) { + items.push({ + 'verb': _.toUpper(verb), + 'path': path + }); + }); + }); + return items; }; }() \ No newline at end of file diff --git a/src/parser/openapi3/body.js b/src/parser/openapi3/body.js index fcff2e3..db696f6 100644 --- a/src/parser/openapi3/body.js +++ b/src/parser/openapi3/body.js @@ -3,36 +3,55 @@ 'use strict' const _ = require('lodash') +const checkCircularReferences = require('../../utils/circularRef.js'); -module.exports = function() { +module.exports = function () { const MAX_DEPTH_LEVEL = 20; let seenSchemas = new WeakSet(); - + return function get(verb, path, bodyResponse) { - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') + seenSchemas = new WeakSet(); + + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return bodyResponse ? {} : undefined; + require('../../utils/error.js')('paths is required'); } - const endpoint = global.definition.paths[path][_.toLower(verb)] - if (!bodyResponse){ + const endpoint = globalThis.definition.paths[path][_.toLower(verb)] + if (!endpoint) return undefined; + + if (!bodyResponse) { let body = endpoint['requestBody']; - if (!body){ - return undefined; - } + if (!body) return undefined; + if (body['$ref']) { let componentType = body['$ref'].split('/')[2] - const ref = _.replace(body['$ref'], '#/components/'+componentType+'/', '') - body = global.definition.components[componentType][ref] + const ref = _.replace(body['$ref'], '#/components/' + componentType + '/', '') + body = globalThis.definition.components[componentType][ref] } if (!body.content['application/json']) { const contentsArray = Object.keys(body.content); - let schema = undefined; + let schema; let properContent = false; let index = 0; - // Búsqueda de un content type soportado por la aplicación while (!properContent && index < contentsArray.length) { - const withoutRefs = replaceRefs(body.content[contentsArray[index]].schema, 1); - switch(contentsArray[index]) { + const mediaType = body.content[contentsArray[index]]; + + if (!mediaType.schema) { + if (contentsArray[index] === 'application/octet-stream') { + return { + type: 'string', + format: 'binary' + }; + } + continue; + } + + const withoutRefs = replaceRefs(mediaType.schema, 1); + switch (contentsArray[index]) { case 'application/x-www-form-urlencoded': schema = replaceAllOfs(withoutRefs); properContent = true; @@ -48,6 +67,16 @@ module.exports = function() { index++; } + if (!properContent && contentsArray.length > 0) { + const firstContentType = contentsArray[0]; + const mediaType = body.content[firstContentType]; + + if (mediaType?.schema) { + const withoutRefs = replaceRefs(mediaType.schema, 1); + return replaceAllOfs(withoutRefs); + } + } + if (!properContent) console.warn('\x1b[33m%s\x1b[0m', `Warning: cannot create body due to unknown content type (Endpoint: ${verb} ${path})`); return schema; } @@ -55,15 +84,15 @@ module.exports = function() { return replaceAllOfs(withOutRefs); } const bodyResponses = {} - _.forEach(endpoint['responses'], function(response, status) { - if (response && response.content && response.content['application/json'] && response.content['application/json'].schema) { + _.forEach(endpoint['responses'], function (response, status) { + if (response?.content?.['application/json']?.schema) { const withOutRefs = replaceRefs(response.content['application/json'].schema, 1) bodyResponses[status] = replaceAllOfs(withOutRefs) if (bodyResponses[status].hasOwnProperty('required')) { const requiredWtihoutDuplicates = bodyResponses[status].required.filter((value, index, arr) => { return arr.indexOf(value) === index; }); - bodyResponses[status].required = requiredWtihoutDuplicates; + bodyResponses[status].required = requiredWtihoutDuplicates; } } }) @@ -71,37 +100,41 @@ module.exports = function() { } function replaceRefs(schema, depthLevel) { + if (!_.isObject(schema)) { + return schema; + } let result = {} if (depthLevel < MAX_DEPTH_LEVEL) { for (const i in schema) { if (i === '$ref') { let componentType = schema[i].split('/')[2] - const ref = _.replace(schema[i], '#/components/'+componentType+'/', '') - if (checkCircularReferences(ref,3,2) || checkCircularReferences(ref,3,3) || checkCircularReferences(ref,3,4)){ - return { type: 'string', - description: 'Circular REF solved swagger2postman' + const ref = _.replace(schema[i], '#/components/' + componentType + '/', '') + if (checkCircularReferences(ref, 3, 2) || checkCircularReferences(ref, 3, 3) || checkCircularReferences(ref, 3, 4)) { + return { + type: 'string', + description: 'Circular REF solved swagger2postman' } } - let entity = global.definition.components[componentType][ref] + + let entity = globalThis.definition.components[componentType][ref] if (!entity) { - require('../../utils/error.js')('ref '+ref+' is not defined') + require('../../utils/error.js')('ref ' + ref + ' is not defined') } - entity = replaceRefs(entity,global.definition, ++depthLevel) - result = _.merge(result, entity) - } else if ( _.isArray(schema[i]) && i !== 'required') { + + entity = replaceRefs(entity, depthLevel + 1); + result = _.merge({}, result, entity) + } else if (_.isArray(schema[i]) && i !== 'required') { const arrayResult = [] - if (i === 'example'){ - continue + if (i === 'example' || i === 'examples') { + result[i] = schema[i]; + continue; } for (const k in schema[i]) { - // arrayResult.push(replaceRefs(schema[i][k], global.definition, ++depthLevel)) - arrayResult.push(schema[i][k]) + arrayResult.push(replaceRefs(schema[i][k], depthLevel + 1)) } result[i] = arrayResult - } else if ( _.isObject(schema[i]) && i !== 'required') { - ++depthLevel; - result[i] = schema[i]; - // result[i] = replaceRefs(schema[i],global.definition); + } else if (_.isObject(schema[i]) && i !== 'required') { + result[i] = replaceRefs(schema[i], depthLevel + 1); } else { result[i] = schema[i]; } @@ -110,10 +143,9 @@ module.exports = function() { return result } - function replaceAllOfs(schema){ + function replaceAllOfs(schema) { if (!_.isObject(schema)) return schema; - // Detectar ciclos por identidad if (seenSchemas.has(schema)) { return { type: "string", @@ -123,96 +155,90 @@ module.exports = function() { seenSchemas.add(schema); let result = {} + + if (Array.isArray(schema.type)) { + const nonNullType = schema.type.find(t => t !== 'null'); + result.type = nonNullType || schema.type[0]; + } + for (let i in schema) { - if (i === 'allOf' && _.isArray(schema[i])){ - let merged = {'required':[],'properties':{},'type':'object'} + if (i === 'type' && Array.isArray(schema.type)) { + continue; + } + if (i === 'allOf' && _.isArray(schema[i])) { + let merged = { 'required': [], 'properties': {}, 'type': 'object' } for (let t in schema[i]) { if (schema[i][t]['type'] === 'string') { merged = schema[i][t] } else { - + for (let k in schema[i][t]) { - if (k === 'type'){ + if (k === 'type') { merged['type'] = schema[i][t][k] - } else if (k === 'required'){ - merged['required'] = _.concat(merged['required'],schema[i][t]['required']) - } else if (k === 'properties'){ - for (let z in schema[i][t]['properties']){ + } else if (k === 'required') { + merged['required'] = _.concat(merged['required'], schema[i][t]['required']) + } else if (k === 'properties') { + for (let z in schema[i][t]['properties']) { merged['properties'][z] = replaceAllOfs(schema[i][t]['properties'][z]) } - } else if (k === 'allOf'){ + } else if (k === 'allOf') { let downSchema = replaceAllOfs(schema[k]) if (downSchema['0']) { downSchema = downSchema['0'] } - merged['required'] = _.concat(merged['required'],downSchema['required']) - merged['properties'] = _.merge(merged['properties'],downSchema['properties']) + merged['required'] = _.concat(merged['required'], downSchema['required']) + merged['properties'] = _.merge(merged['properties'], downSchema['properties']) continue } else if (k === 'description') { continue } else if (k === 'items') { continue - }else { - console.warn('the property '+k+' of allOf is not implemented') + } else { + console.warn('the property ' + k + ' of allOf is not implemented') } } } } - result = _.merge(result, merged) - } else if ( _.isArray(schema[i]) && i !== 'required') { + result = _.merge({}, result, merged) + } else if (_.isArray(schema[i]) && i !== 'required') { if (schema[i].every(v => !_.isObject(v))) { result[i] = [...schema[i]]; } else { result[i] = schema[i].map(item => replaceAllOfs(item)); } - } else if ( _.isObject(schema[i]) && i !== 'required') { - // result.type = 'object'; - result[i] = _.merge(result[i],replaceAllOfs(schema[i])) + } else if (_.isObject(schema[i]) && i !== 'required') { + const value = replaceAllOfs(schema[i]); + + if (_.isPlainObject(value) && _.isPlainObject(result[i])) { + result[i] = _.merge({}, result[i], value); + } else { + result[i] = value; + } } else { result[i] = schema[i] } } - return result - } - - function checkCircularReferences(reference,depthLevel,patternNumber){ - if (!global.circularTail){ - global.circularTail = [] - } - if (! global.circularTail[patternNumber] ){ - global.circularTail[patternNumber] = [reference] - return false - } - if (global.circularTail[patternNumber].length < (depthLevel * patternNumber) ){ - global.circularTail[patternNumber].push(reference) - return false + if (typeof result.exclusiveMinimum === 'number') { + result.minimum = result.exclusiveMinimum; + result.exclusiveMinimum = true; } - const groups = _.chunk(global.circularTail[patternNumber], patternNumber) - global.circularTail[patternNumber].shift() - global.circularTail[patternNumber].push(reference) - - let areEquals = true - let lastArray = groups[0] - comparation: - for (let i = 1; i < groups.length; i++) { - for (let k in groups[i]){ - if (groups[i][k] !== groups[i - 1][k]){ - areEquals = false - break comparation - } - } + if (typeof result.exclusiveMaximum === 'number') { + result.maximum = result.exclusiveMaximum; + result.exclusiveMaximum = true; } - if (areEquals){ - global.circularTail = [] + if (schema.contentEncoding) { + result.contentEncoding = schema.contentEncoding; } - return areEquals - + if (schema.contentMediaType) { + result.contentMediaType = schema.contentMediaType; + } + return result } }() \ No newline at end of file diff --git a/src/parser/openapi3/consumes.js b/src/parser/openapi3/consumes.js index 854257d..4499c66 100644 --- a/src/parser/openapi3/consumes.js +++ b/src/parser/openapi3/consumes.js @@ -4,18 +4,22 @@ const _ = require('lodash'); -module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required'); - } +module.exports = function () { - const request = global.definition.paths[path][_.toLower(verb)] - if (request.requestBody && request.requestBody.content){ - return _.keys(request.requestBody.content)[0] - } - return 'application/json'; + return function get(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return 'application/json'; + require('../../utils/error.js')('paths is required'); + } + + const request = globalThis.definition.paths[path][_.toLower(verb)] + if (request.requestBody?.content) { + return _.keys(request.requestBody.content)[0] + } + return 'application/json'; }; }() \ No newline at end of file diff --git a/src/parser/openapi3/headers.js b/src/parser/openapi3/headers.js index f810947..50a321c 100644 --- a/src/parser/openapi3/headers.js +++ b/src/parser/openapi3/headers.js @@ -4,22 +4,25 @@ const _ = require('lodash'); -module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') +module.exports = function () { + + return function get(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return []; + require('../../utils/error.js')('paths is required'); } - let parameters = global.definition.paths[path][_.toLower(verb)]['parameters']; - // parameters = replaceRefs(parameters); + let parameters = globalThis.definition.paths[path][_.toLower(verb)]['parameters']; let headers = _.filter(parameters, ['in', 'header']) const result = [] - _.forEach(headers, function(header) { - result.push({ - key: header.name, - type: header.schema.type, - required : header.required, + _.forEach(headers, function (header) { + result.push({ + key: header.name, + type: header.schema.type, + required: header.required, value: getExamples(header) }); }); @@ -27,16 +30,18 @@ module.exports = function() { }; function getExamples(header) { - if (header.example) { - return header.example - } - else { - if (header.hasOwnProperty('examples')) { - const value = header.examples[Object.keys(header.examples)[0]]; - return value[Object.keys(value)[0]]; - } - return header.example; - } - } + if (header.example) return header.example; + + if (header.schema?.examples) { + return header.schema.examples[0]; + } + + if (header.examples) { + const firstKey = Object.keys(header.examples)[0]; + return header.examples[firstKey]?.value; + } + + return undefined; + } }() \ No newline at end of file diff --git a/src/parser/openapi3/microcks.js b/src/parser/openapi3/microcks.js index eb55082..aa12e65 100644 --- a/src/parser/openapi3/microcks.js +++ b/src/parser/openapi3/microcks.js @@ -1,11 +1,15 @@ const _ = require('lodash'); -module.exports = (function (verb, path) { - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required'); - } - - const endpoint = global.definition.paths[path][_.toLower(verb)] - - return !!endpoint["x-microcks-operation"] +module.exports = (function hasMicrocksOperation(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return false; + require('../../utils/error.js')('paths is required'); + } + + const endpoint = globalThis.definition.paths[path][_.toLower(verb)] + + return !!endpoint["x-microcks-operation"] }) \ No newline at end of file diff --git a/src/parser/openapi3/pathParameters.js b/src/parser/openapi3/pathParameters.js index d74bd1c..39bebaa 100644 --- a/src/parser/openapi3/pathParameters.js +++ b/src/parser/openapi3/pathParameters.js @@ -4,21 +4,25 @@ const _ = require('lodash') -module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') - } - - const parameters = global.definition.paths[path][_.toLower(verb)]['parameters'] - const parametersPath = _.filter(parameters, ['in', 'path']) +module.exports = function () { + + return function get(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return []; + require('../../utils/error.js')('paths is required'); + } + + const parameters = globalThis.definition.paths[path][_.toLower(verb)]['parameters'] + const parametersPath = _.filter(parameters, ['in', 'path']) const result = [] - _.forEach(parametersPath, function(parameterPath) { - result.push( { name : parameterPath.name , type: parameterPath.schema.type, example: parameterPath.schema.example} ) - }) + _.forEach(parametersPath, function (parameterPath) { + result.push({ name: parameterPath.name, type: parameterPath.schema.type, example: parameterPath.schema.example || parameterPath.schema.examples?.[0] }) + }) - return result + return result } }() \ No newline at end of file diff --git a/src/parser/openapi3/queryParams.js b/src/parser/openapi3/queryParams.js index afc529e..650c2df 100644 --- a/src/parser/openapi3/queryParams.js +++ b/src/parser/openapi3/queryParams.js @@ -5,75 +5,63 @@ const _ = require('lodash'); module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') - } - let parameters = global.definition.paths[path][_.toLower(verb)]['parameters']; - // parameters = replaceRefs(parameters); + return function get(verb,path){ + + let parameters = globalThis.definition.paths[path][_.toLower(verb)]['parameters']; let queryParams = _.filter(parameters, ['in', 'query']) const result = [] _.forEach(queryParams, function(queryParam) { const param = queryParam.schema ? queryParam : getContentProperty(queryParam); - result.push({ + + const obj = { name: queryParam.name, type: param.schema.type, required : queryParam.required, - example: getExamples(param) - }); + }; + + const example = getExamples(param); + + if (example !== undefined && example !== null) { + obj.example = example; + } + + result.push(obj); }); + return result - }; - - function replaceRefs(schema){ - let result = {} - for (let i in schema) { - if (i === '$ref'){ - const ref = _.replace(schema[i], '#/components/parameters/', ''); - const schemaRef = _.replace(schema[i], '#/components/schemas/', ''); - let entity = global.definition.components.parameters[ref] || global.definition.components.schemas[schemaRef]; - if (!entity){ - require('../../utils/error.js')('ref '+ref+' is not defined') - } - entity = replaceRefs(entity,global.definition) - result = _.merge(result, entity) - } else if ( _.isArray(schema[i]) && i !== 'required'){ - const arrayResult = [] - if (i === 'example'){ - continue; - } - for (let k in schema[i]) { - arrayResult.push(replaceRefs(schema[i][k],global.definition)) - } - result[i] = arrayResult - } else if ( _.isObject(schema[i]) && i !== 'required'){ - result[i] = replaceRefs(schema[i],global.definition) - } else { - result[i] = schema[i]; - } - } - return result; - } + }; function getExamples(queryParam) { + if (!queryParam?.schema) return undefined; + if (queryParam.schema.type === 'array') { - return queryParam.example - } else { - if (queryParam.hasOwnProperty('examples')) { - const value = queryParam.examples[Object.keys(queryParam.examples)[0]]; - return value[Object.keys(value)[0]]; + return queryParam.example; + } + + if (queryParam.schema?.examples) { + return queryParam.schema.examples[0]; + } + + if (queryParam.hasOwnProperty('examples')) { + const firstKey = Object.keys(queryParam.examples)[0]; + const value = queryParam.examples[firstKey]; + if (value?.hasOwnProperty('value')) { + return value.value; } - return queryParam.example; - } + return value; + } + + if (queryParam.example === undefined) { + return queryParam.schema.example; + } + + return queryParam.example; } function getContentProperty(query){ const queryContent = query.content; - for (const key in queryContent) { - return queryContent[key]; - } + return queryContent ? Object.values(queryContent)[0] : undefined; } }() \ No newline at end of file diff --git a/src/parser/openapi3/refs.js b/src/parser/openapi3/refs.js index 8f4cbc3..8029f0e 100644 --- a/src/parser/openapi3/refs.js +++ b/src/parser/openapi3/refs.js @@ -3,20 +3,16 @@ 'use strict' function eachRecursive(obj) { - for (var k in obj) { + for (let k in obj) { if (typeof obj[k] == "object" && obj[k] !== null) { eachRecursive(obj[k]); - } else { - if (k == '$ref') { - let property = obj[k] - property = property.replace('#/', '') - let propertiesArray = property.split('/') - let refObject = findObject(global.definition, propertiesArray) - // Clear ref property - delete obj[k] - // Assign properties refOcject - Object.assign(obj, refObject) - } + } else if (k == '$ref') { + let property = obj[k] + property = property.replace('#/', '') + let propertiesArray = property.split('/') + let refObject = findObject(globalThis.definition, propertiesArray) + delete obj[k] + Object.assign(obj, refObject) } } } @@ -32,7 +28,7 @@ function findObject(obj, propertiesArray) { module.exports = function() { return function get() { - eachRecursive(global.definition) - return global.definition + eachRecursive(globalThis.definition) + return globalThis.definition } }() \ No newline at end of file diff --git a/src/parser/openapi3/schemaHostBasePath.js b/src/parser/openapi3/schemaHostBasePath.js index fe1fe6e..a8d14c7 100644 --- a/src/parser/openapi3/schemaHostBasePath.js +++ b/src/parser/openapi3/schemaHostBasePath.js @@ -5,12 +5,12 @@ const _ = require('lodash'); module.exports = function() { - + return function get(){ - if (!global.definition.servers || !_.isArray(global.definition.servers) || !global.definition.servers[0] || !global.definition.servers[0].url || !_.isString(global.definition.servers[0].url)) { + if (!globalThis.definition.servers || !_.isArray(globalThis.definition.servers) || !globalThis.definition.servers[0]?.url || !_.isString(globalThis.definition.servers[0].url)) { require('../../utils/error.js')('servers is required') } - const all = global.definition.servers[0].url + const all = globalThis.definition.servers[0].url const allArray = all.split( '/' ) const protocol = allArray[0] if (protocol !== 'http:' && protocol !== 'https:' && protocol !== '{scheme}:') { diff --git a/src/parser/status.js b/src/parser/status.js index b3dcf86..c2e12e2 100644 --- a/src/parser/status.js +++ b/src/parser/status.js @@ -4,22 +4,26 @@ const _ = require('lodash'); -module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') - } +module.exports = function () { - const responses = global.definition.paths[path][_.toLower(verb)]['responses'] - const keys = _.keys(responses) - for (const index in keys) { - keys[index] = _.toInteger(keys[index]) - if (keys[index] < 200) { - keys[index] = 500 - } - } - return keys + return function get(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return []; + require('../../utils/error.js')('paths is required'); + } + + const responses = globalThis.definition.paths[path][_.toLower(verb)]['responses'] + const keys = _.keys(responses) + for (const index in keys) { + keys[index] = _.toInteger(keys[index]) + if (keys[index] < 200) { + keys[index] = 500 + } + } + return keys } }() \ No newline at end of file diff --git a/src/parser/summary.js b/src/parser/summary.js index 93294d3..4c5a669 100644 --- a/src/parser/summary.js +++ b/src/parser/summary.js @@ -4,19 +4,23 @@ const _ = require('lodash') -module.exports = function() { - - return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { - require('../../utils/error.js')('paths is required') - } - - const endpoint = global.definition.paths[path][_.toLower(verb)] - if (_.has(endpoint,'summary')){ - return endpoint.summary - } - - return false +module.exports = function () { + + return function get(verb, path) { + const hasPaths = _.isObject(globalThis.definition.paths); + const hasWebhooks = _.isObject(globalThis.definition.webhooks); + + if (!hasPaths) { + if (hasWebhooks) return false; + require('../../utils/error.js')('paths is required'); + } + + const endpoint = globalThis.definition.paths[path][_.toLower(verb)] + if (_.has(endpoint, 'summary')) { + return endpoint.summary + } + + return false } }() \ No newline at end of file diff --git a/src/parser/swagger2/body.js b/src/parser/swagger2/body.js index a28544d..bd4370e 100644 --- a/src/parser/swagger2/body.js +++ b/src/parser/swagger2/body.js @@ -3,18 +3,19 @@ 'use strict' const _ = require('lodash'); +const checkCircularReferences = require('../../utils/circularRef.js'); module.exports = function() { let seenSchemas = new WeakSet(); - return function get(verb, path, bodyResponse) { - if (!_.isObject(global.definition.paths)) { + seenSchemas = new WeakSet(); + if (!_.isObject(globalThis.definition.paths)) { require('../../utils/error.js')('paths is required'); } - const endpoint = global.definition.paths[path][_.toLower(verb)]; + const endpoint = globalThis.definition.paths[path][_.toLower(verb)]; if (!bodyResponse){ let bodyParameter = replaceRefs(endpoint['parameters']); const body = _.find(bodyParameter, ['in', 'body']); @@ -62,14 +63,14 @@ module.exports = function() { description: 'Circular REF solved swagger2postman' } } - let entity = global.definition.definitions[ref]; + let entity = globalThis.definition.definitions[ref]; if (!entity){ - entity = global.definition.parameters[ref]; + entity = globalThis.definition.parameters[ref]; if (!entity) { require('../../utils/error.js')('ref ' + ref + ' is not defined'); } } - entity = replaceRefs(entity, global.definition); + entity = replaceRefs(entity); result = _.merge(result, entity); } else if ( _.isArray(schema[i]) && i !== 'required'){ const arrayResult = []; @@ -77,11 +78,11 @@ module.exports = function() { continue; } for (let k in schema[i]) { - arrayResult.push(replaceRefs(schema[i][k],global.definition)); + arrayResult.push(replaceRefs(schema[i][k])); } result[i] = arrayResult; } else if ( _.isObject(schema[i]) && i !== 'required'){ - result[i] = replaceRefs(schema[i],global.definition); + result[i] = replaceRefs(schema[i]); } else { result[i] = schema[i]; } @@ -99,13 +100,13 @@ module.exports = function() { if (schema[i][t]['type'] === 'string') { merged = schema[i][t]; } else { - for (let k in schema[i][t]) { if (k === 'type'){ merged['type'] = schema[i][t][k]; } else if (k === 'required'){ merged['required'] = _.concat(merged['required'],schema[i][t]['required']); } else if (k === 'properties'){ + if (!merged['properties']) merged['properties'] = {}; for (let z in schema[i][t]['properties']){ merged['properties'][z] = replaceAllOfs(schema[i][t]['properties'][z]); } @@ -145,41 +146,4 @@ module.exports = function() { return result; } - function checkCircularReferences(reference, depthLevel, patternNumber){ - if (!global.circularTail){ - global.circularTail = [] - } - if (! global.circularTail[patternNumber] ){ - global.circularTail[patternNumber] = [reference] - return false - } - if (global.circularTail[patternNumber].length < (depthLevel * patternNumber) ){ - global.circularTail[patternNumber].push(reference) - return false - } - - const groups = _.chunk(global.circularTail[patternNumber], patternNumber) - global.circularTail[patternNumber].shift() - global.circularTail[patternNumber].push(reference) - - let areEquals = true - let lastArray = groups[0] - comparation: - for (let i = 1; i < groups.length; i++) { - for (let k in groups[i]){ - if (groups[i][k] !== groups[i - 1][k]){ - areEquals = false - break comparation - } - } - } - - if (areEquals){ - global.circularTail = [] - } - - return areEquals - - } - }() \ No newline at end of file diff --git a/src/parser/swagger2/queryParams.js b/src/parser/swagger2/queryParams.js index 4620d90..5a2fa4f 100644 --- a/src/parser/swagger2/queryParams.js +++ b/src/parser/swagger2/queryParams.js @@ -5,70 +5,55 @@ const _ = require('lodash'); module.exports = function() { - + return function get(verb,path){ - if (!_.isObject(global.definition.paths)) { + if (!_.isObject(globalThis.definition.paths)) { require('../../utils/error.js')('paths is required') } - let parameters = global.definition.paths[path][_.toLower(verb)]['parameters']; - // parameters = replaceRefs(parameters); + let parameters = globalThis.definition.paths[path][_.toLower(verb)]['parameters']; const queryParams = _.filter(parameters, ['in', 'query']) const result = [] - _.forEach(queryParams, function(queryParam) { - result.push({ + _.forEach(queryParams, function(queryParam) { + const obj = { name : queryParam.name, type : queryParam.type, required : queryParam.required, - example: getExamples(queryParam) - }); - }) - - return result - }; + }; - function replaceRefs(schema){ - let result = {}; - for (let i in schema) { - if (i === '$ref'){ - const ref = _.replace(schema[i], '#/parameters/', ''); - let entity = global.definition.parameters[ref]; - if (!entity){ - continue; - } - entity = replaceRefs(entity, global.definition); - result = _.merge(result, entity); - } else if (_.isArray(schema[i]) && i !== 'required'){ - const arrayResult = []; - if (i === 'example'){ - continue; - } - for (let k in schema[i]) { - arrayResult.push(replaceRefs(schema[i][k],global.definition)); - } - result[i] = arrayResult; - } else if (_.isObject(schema[i]) && i !== 'required'){ - result[i] = replaceRefs(schema[i],global.definition); - } else { - result[i] = schema[i]; - } + const example = getExamples(queryParam); + if (example !== undefined && example !== null) { + obj.example = example; } - return result; - } + + result.push(obj); + }); + + return result; + }; function getExamples(queryParam) { - if (queryParam.type === 'array') { - return queryParam.example; - } else { - if (queryParam.hasOwnProperty('examples')) { - const value = queryParam.examples[Object.keys(queryParam.examples)[0]]; - return value[Object.keys(value)[0]]; - } else if (queryParam.hasOwnProperty('default')) { - return queryParam.default; - } else { - return queryParam.example !== undefined ? queryParam.example : ''; + if (queryParam.hasOwnProperty('example')) { + return queryParam.example; + } + + if (queryParam.hasOwnProperty('x-example')) { + return queryParam['x-example']; + } + + if (queryParam.hasOwnProperty('default')) { + return queryParam.default; + } + + if (queryParam.hasOwnProperty('examples')) { + const examples = queryParam.examples; + const firstKey = Object.keys(examples)[0]; + if (typeof examples[firstKey] === 'object') { + return examples[firstKey][Object.keys(examples[firstKey])[0]]; } - } - } + return examples[firstKey]; + } + return undefined; + } }() \ No newline at end of file diff --git a/src/parser/version.js b/src/parser/version.js index 0563ab1..534bd7f 100644 --- a/src/parser/version.js +++ b/src/parser/version.js @@ -5,17 +5,19 @@ module.exports = function() { return function get(){ - const versions = { - swagger : ['2.0', '2.0.0'], - openapi : ['3.0', '3.0.0', '3.0.1','3.0.2' , '3.0.3'] - } + const versions = { + swagger : ['2.0', '2.0.0'], + openapi3 : ['3.0', '3.0.0', '3.0.1','3.0.2', '3.0.3'], + openapi31 : ['3.1', '3.1.0', '3.1.1', '3.1.2'] + } - if (versions.swagger.includes(global.definition.swagger)) { - return 'swagger2' - } else if (versions.openapi.includes(global.definition.openapi)){ - return 'openapi3' - } - require('../utils/error.js')('Specification is not supported') + if (versions.swagger.includes(globalThis.definition.swagger)) { + return 'swagger2' + } else if (versions.openapi3.includes(globalThis.definition.openapi) || versions.openapi31.includes(globalThis.definition.openapi)) { + return 'openapi3' + } else { + require('../utils/error.js')('Specification is not supported') + } }; }() \ No newline at end of file diff --git a/src/swagger2json/index.js b/src/swagger2json/index.js index 5d2e630..646dcde 100644 --- a/src/swagger2json/index.js +++ b/src/swagger2json/index.js @@ -4,12 +4,25 @@ const _ = require('lodash'); -module.exports = function() { - - return function get(swagger, name, parent,index){ -if (typeof swagger !== 'object' || swagger === null) { - swagger = { type: 'string' }; - } +module.exports = function () { + + return function get(swagger, name, parent, index) { + if (Array.isArray(swagger.type)) { + + swagger.type = swagger.type.map(t => { + if (typeof t === 'object' && !Array.isArray(t)) { + return Object.values(t).join('') + } + return t + }) + + const nonNullTypes = swagger.type.find(t => t !== 'null'); + swagger.type = nonNullTypes[0] || 'string'; + } + + if (typeof swagger !== 'object' || swagger === null) { + swagger = { type: 'string' }; + } if (!swagger.type) { if (swagger.properties) { @@ -26,9 +39,9 @@ if (typeof swagger !== 'object' || swagger === null) { } let wrongParam = false; - if (parent && global.wrongParamsCatch && _.indexOf(global.wrongParams, parent) === -1 && parent !== 'with') { - global.wrongParams.push(parent); - global.wrongParamsCatch = false; + if (parent && globalThis.wrongParamsCatch && _.indexOf(globalThis.wrongParams, parent) === -1 && parent !== 'with') { + globalThis.wrongParams.push(parent); + globalThis.wrongParamsCatch = false; wrongParam = true; } @@ -36,30 +49,30 @@ if (typeof swagger !== 'object' || swagger === null) { case 'object': if (wrongParam) { - global.environmentVariables[global.currentId+name+'_wrong'] = require('../utils/exampleForField.js')(swagger,true) - return '{{'+name+'_wrong}}' + globalThis.environmentVariables[globalThis.currentId + name + '_wrong'] = require('../utils/exampleForField.js')(swagger, true) + return '{{' + name + '_wrong}}' } - - return require('./object.js')(swagger,parent) + + return require('./object.js')(swagger, parent) case 'array': if (wrongParam) { - global.environmentVariables[global.currentId+name+'_wrong'] = require('../utils/exampleForField.js')(swagger,true) - return '{{'+name+'_wrong}}' + globalThis.environmentVariables[globalThis.currentId + name + '_wrong'] = require('../utils/exampleForField.js')(swagger, true) + return '{{' + name + '_wrong}}' } - - return require('./array.js')(swagger,name,parent); + + return require('./array.js')(swagger, name, parent); case 'string': case 'number': case 'integer': case 'boolean': - + if (wrongParam) { - global.environmentVariables[global.currentId+name+'_wrong'] = require('../utils/exampleForField.js')(swagger,true) - return '{{'+name+'_wrong}}' + globalThis.environmentVariables[globalThis.currentId + name + '_wrong'] = require('../utils/exampleForField.js')(swagger, true) + return '{{' + name + '_wrong}}' } - global.environmentVariables[global.currentId+name] = require('../utils/exampleForField.js')(swagger,false) - return '{{'+name+'}}' + globalThis.environmentVariables[globalThis.currentId + name] = require('../utils/exampleForField.js')(swagger, false) + return '{{' + name + '}}' case 'oneOf': let schemaOne = swagger.oneOf[index]; return anyOfOneOfChoice(schemaOne, name, parent); @@ -67,7 +80,7 @@ if (typeof swagger !== 'object' || swagger === null) { let schemaAny = swagger.anyOf[index]; return anyOfOneOfChoice(schemaAny, name, parent); default: - require('../utils/error.js')('The type '+swagger.type+' is not implemented'); + require('../utils/error.js')('The type ' + swagger.type + ' is not implemented'); } } @@ -78,8 +91,8 @@ if (typeof swagger !== 'object' || swagger === null) { case 'array': return require('./array.js')(schema, name, parent); default: - global.environmentVariables[global.currentId+name] = require('../utils/exampleForField.js')(schema, false); - return '{{'+name+'}}'; + globalThis.environmentVariables[globalThis.currentId + name] = require('../utils/exampleForField.js')(schema, false); + return '{{' + name + '}}'; } } diff --git a/src/utils/circularRef.js b/src/utils/circularRef.js new file mode 100644 index 0000000..a1179b1 --- /dev/null +++ b/src/utils/circularRef.js @@ -0,0 +1,43 @@ +/** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ + +'use strict' + + +const _ = require('lodash'); + +module.exports = function checkCircularReferences(reference, depthLevel, patternNumber) { + if (!globalThis.circularTail) { + globalThis.circularTail = [] + } + if (!globalThis.circularTail[patternNumber]) { + globalThis.circularTail[patternNumber] = [reference] + return false + } + if (globalThis.circularTail[patternNumber].length < (depthLevel * patternNumber)) { + globalThis.circularTail[patternNumber].push(reference) + return false + } + + const groups = _.chunk(globalThis.circularTail[patternNumber], patternNumber) + globalThis.circularTail[patternNumber].shift() + globalThis.circularTail[patternNumber].push(reference) + + let areEquals = true + + for (let i = 1; i < groups.length; i++) { + for (let k in groups[i]) { + if (groups[i][k] !== groups[i - 1][k]) { + areEquals = false + break + } + } + if (!areEquals) break + } + + if (areEquals) { + globalThis.circularTail = [] + } + + return areEquals + +} \ No newline at end of file diff --git a/test/generator-endpoints.js b/test/generator-endpoints.js index bd8ac4d..3d57b59 100644 --- a/test/generator-endpoints.js +++ b/test/generator-endpoints.js @@ -3,7 +3,7 @@ const assert = require('assert'); describe('generator-endpoints', () => { - + it('good', () => { const definitionInitial = require('../seeds/generatorEndpointsInitial.json'); diff --git a/test/generator-examples.js b/test/generator-examples.js index 081f57f..93c5310 100644 --- a/test/generator-examples.js +++ b/test/generator-examples.js @@ -7,7 +7,7 @@ describe('generator-examples', () => { it('no examples', () => { global.definition = require('../seeds/generatorExamplesInitial.json') const examples = require('../src/generator/examples.js')('/user/{username}','get','400') - assert.strictEqual(examples, false); + assert.strictEqual(examples, null); }) // Check if the function returns an example when there are examples it('with examples', () => { diff --git a/test/generator-folders.js b/test/generator-folders.js index 034bde1..2f10b94 100644 --- a/test/generator-folders.js +++ b/test/generator-folders.js @@ -3,7 +3,7 @@ const assert = require('assert'); describe('generator-folders', () => { - + it('good', () => { const input = require('../seeds/generatorFoldersInitial.json') diff --git a/test/parser-authorization.js b/test/parser-authorization.js index 1e3ca96..6c81a19 100644 --- a/test/parser-authorization.js +++ b/test/parser-authorization.js @@ -1,42 +1,26 @@ /** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ -const assert = require('assert'); +const assert = require('node:assert'); +const getAuthorization = require('../src/parser/authorization.js'); describe('parser-authorization', () => { - - it('good swagger2', () => { - global.definition = require('../seeds/parserAuthorizationInitial.json') - const authorization = require('../src/parser/authorization.js')('POST','/apple') - assert.deepStrictEqual(authorization, 'user_token') - }) + const checkAuth = (name, seed, method, path, expected) => { + it(name, () => { + globalThis.definition = require(`../seeds/${seed}`); + const authorization = getAuthorization(method, path); + assert.deepStrictEqual(authorization, expected); + }); + }; - it('endpoint swagger2', () => { + checkAuth('auth good sw2', 'parserAuthorizationInitial.json', 'POST', '/apple', 'user_token'); + checkAuth('auth endpoint sw2', 'parserAuthorizationInitialEndpoint.json', 'POST', '/apple', 'OAuth2'); + checkAuth('auth general sw2', 'parserAuthorizationInitialGeneral.json', 'POST', '/apple', 'BasicAuth'); - global.definition = require('../seeds/parserAuthorizationInitialEndpoint.json') - const authorization = require('../src/parser/authorization.js')('POST','/apple') - assert.deepStrictEqual(authorization, 'OAuth2') - }) + checkAuth('auth general oa3.0','parserAuthorizationInitialOpenApi3.json', 'POST', '/pets', 'ApiKeyAuth'); + checkAuth('auth endpoint oa3.0','parserAuthorizationInitialOpenApi3.json', 'GET', '/pets', 'OAuth2'); - it('general swagger2', () => { + checkAuth('auth general oa3.1', 'parserAuthorizationInitialOpenApi3.1.json','POST', '/pets', 'ApiKeyAuth'); + checkAuth('auth endpoint oa3.1', 'parserAuthorizationInitialOpenApi3.1.json','GET', '/pets', 'OAuth2'); - global.definition = require('../seeds/parserAuthorizationInitialGeneral.json') - const authorization = require('../src/parser/authorization.js')('POST','/apple') - assert.deepStrictEqual(authorization, 'BasicAuth') - }) - - it('general openapi3', () => { - - global.definition = require('../seeds/parserAuthorizationInitialOpenApi3.json') - const authorization = require('../src/parser/authorization.js')('POST','/pets') - assert.deepStrictEqual(authorization, 'ApiKeyAuth') - }) - - it('endpoint openapi3', () => { - - global.definition = require('../seeds/parserAuthorizationInitialOpenApi3.json') - const authorization = require('../src/parser/authorization.js')('GET','/pets') - assert.deepStrictEqual(authorization, 'OAuth2') - }) - -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/test/parser-body.js b/test/parser-body.js index e957752..026d338 100644 --- a/test/parser-body.js +++ b/test/parser-body.js @@ -1,97 +1,52 @@ /** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ -const assert = require('assert') +const assert = require('node:assert'); describe('parser-body', () => { - - it('void request swagger2', () => { + const checkBody = (name, parser, seed, method, path, isResp, expectedFile) => { + it(name, () => { + globalThis.definition = require(`../seeds/${seed}`); - global.definition = require('../seeds/parserInitialGood.json') + if (isResp) globalThis.circularTail = []; - const body = require('../src/parser/swagger2/body.js')('GET','/pets') - assert.deepStrictEqual(body, undefined) - }) + const expected = expectedFile ? require(`../seeds/${expectedFile}`) : undefined; + const body = require(`../src/parser/${parser}/body.js`)(method, path, isResp); - it('void request openapi3', () => { + assert.deepStrictEqual(body, expected); + }); + }; - global.definition = require('../seeds/parserInitialGoodOpenApi3.json') + checkBody('body void request swagger2','swagger2','parserInitialGood.json','GET','/pets',false,null); - const body = require('../src/parser/openapi3/body.js')('GET','/pets') - assert.deepStrictEqual(body, undefined) - }) + checkBody('body void request openapi3.0','openapi3','parserInitialGoodOpenApi3.json','GET','/pets',false,null); - it('void request openapi3 2', () => { + checkBody('body void request openapi3.0 2','openapi3','parserInitialGoodOpenApi3.json','POST','/pets',false,null); - global.definition = require('../seeds/parserInitialGoodOpenApi3.json') + checkBody('body void request openapi3.1','openapi3','parserInitialGoodOpenApi3.1.json','GET','/pets',false,null); - const body = require('../src/parser/openapi3/body.js')('POST','/pets') - assert.deepStrictEqual(body, undefined) - }) - it('fill request swagger2', () => { + checkBody('body fill request swagger2','swagger2','parserInitialGood.json','POST','/pets',false,'parserInitialGoodResultPetRequest.json'); - global.definition = require('../seeds/parserInitialGood.json') + checkBody('body fill request openapi3.0','openapi3','parserInitialGoodOpenApiExpanded3.json','POST','/pets',false,'parserInitialGoodResultPetRequest.json'); - const body = require('../src/parser/swagger2/body.js')('POST','/pets') - assert.deepStrictEqual(body, {"type":"object","required":["name"],"properties":{"name":{"type":"string"},"tag":{"type":"string"}}}) - }) + checkBody('body fill request openapi3.1','openapi3','parserInitialGoodOpenApiExpanded3.1.json','POST','/pets',false,'parserInitialGoodResultPetRequest.json'); - it('fill request openapi3', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json') + checkBody('body items,ref and allof response swagger2','swagger2','parserInitialGood.json','GET','/pets',true,'parserInitialGoodResultRespArray.json'); - const body = require('../src/parser/openapi3/body.js')('POST','/pets') - assert.deepStrictEqual(body, {"type":"object","required":["name"],"properties":{"name":{"type":"string"},"tag":{"type":"string"}}}) - }) + checkBody('body items,ref and allof response openapi3.0','openapi3','parserInitialGoodOpenApiExpanded3.json','GET','/pets',true,'parserInitialGoodResultRespArray.json'); - it('items, ref and allof response swagger2', () => { + checkBody('body items,ref and allof response openapi3.1','openapi3','parserInitialGoodOpenApiExpanded3.1.json','GET','/pets',true,'parserInitialGoodResultRespArray.json'); - global.definition = require('../seeds/parserInitialGood.json') - const body = require('../src/parser/swagger2/body.js')('GET','/pets',true) - assert.deepStrictEqual(body, {"200":{"type":"array","items":{"type":"object","required":["name","id"],"properties":{"name":{"type":"string"},"tag":{"type":"string"},"id":{"type":"integer","format":"int64"}}}},"500":{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}}) - }) + checkBody('body ref and allof response swagger2','swagger2','parserInitialGood.json','POST','/pets',true,'parserInitialGoodResultRespObject.json'); - it('items, ref and allof response openapi3', () => { + checkBody('body ref and allof response openapi3.0','openapi3','parserInitialGoodOpenApiExpanded3.json','POST','/pets',true,'parserInitialGoodResultRespObject.json'); - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json') + checkBody('body ref and allof response openapi3.1','openapi3','parserInitialGoodOpenApiExpanded3.1.json','POST','/pets',true,'parserInitialGoodResultRespObject.json'); - const body = require('../src/parser/openapi3/body.js')('GET','/pets',true) - assert.deepStrictEqual(body, {"200":{"type":"array","items":{"type":"object","required":["name","id"],"properties":{"name":{"type":"string"},"tag":{"type":"string"},"id":{"type":"integer","format":"int64"}}}},"500":{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}}) - }) - it('ref and allof response swagger2', () => { + checkBody('body allof loop swagger2','swagger2','parserBodyAllofLoopInitial.json','POST','/apple',false,'parserInitialGoodResultAllOfLoop.json'); - global.definition = require('../seeds/parserInitialGood.json') - global.circularTail = [] - - const body = require('../src/parser/swagger2/body.js')('POST','/pets',true) - assert.deepStrictEqual(body, {"200":{"type":"object","required":["name","id"],"properties":{"name":{"type":"string"},"tag":{"type":"string"},"id":{"type":"integer","format":"int64"}}},"500":{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}}) - }) - - it('ref and allof response openapi3', () => { - - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json') - global.circularTail = [] - - const body = require('../src/parser/openapi3/body.js')('POST','/pets',true) - assert.deepStrictEqual(body, {"200":{"type":"object","required":["name","id"],"properties":{"name":{"type":"string"},"tag":{"type":"string"},"id":{"type":"integer","format":"int64"}}},"500":{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}}) - }) - - it('allof loop swagger2', () => { - - global.definition = require('../seeds/parserBodyAllofLoopInitial.json') - - const body = require('../src/parser/swagger2/body.js')('POST','/apple') - assert.deepStrictEqual(body, {"required":["red","orange"],"properties":{"red":{"type":"string","maxLength":50},"orange":{"type":"number","maxLength":9},"water":{"required":["paramAiden"],"properties":{"paramAiden":{"type":"string"},"paramBiden":{"type":"string"},"paramCiden":{"type":"string"}},"type":"object"}},"type":"object"}) - }) - - it('allof string swagger2', () => { - - global.definition = require('../seeds/parserBodyAllofString.json') - - const body = require('../src/parser/swagger2/body.js')('POST','/apple') - assert.deepStrictEqual(body, {"type":"object","properties":{"red":{"type":"string","maxLength":50}}}) - }) - -}) \ No newline at end of file + checkBody('body allof string swagger2','swagger2','parserBodyAllofString.json','POST','/apple',false,'parserInitialGoodResultAllOfString.json'); +}); \ No newline at end of file diff --git a/test/parser-consumes.js b/test/parser-consumes.js index 77cf001..769d1ea 100644 --- a/test/parser-consumes.js +++ b/test/parser-consumes.js @@ -3,26 +3,34 @@ const assert = require('assert') describe('parser-consumes', () => { - + + it('general swagger2', () => { + + globalThis.definition = require('../seeds/parserConsumesInitial.json') + + const consumes = require('../src/parser/swagger2/consumes.js')('POST','/pets/{id}') + assert.equal(consumes, 'application/json') + }) + it('specifies swagger2', () => { - global.definition = require('../seeds/parserConsumesInitial.json') + globalThis.definition = require('../seeds/parserConsumesInitial.json') const consumes = require('../src/parser/swagger2/consumes.js')('POST','/pets') assert.equal(consumes, 'application/xml') }) - it('general swagger2', () => { + it('specifies openapi3.0', () => { - global.definition = require('../seeds/parserConsumesInitial.json') + globalThis.definition = require('../seeds/parserInitialGoodOpenApiExpanded3.json'); - const consumes = require('../src/parser/swagger2/consumes.js')('POST','/pets/{id}') - assert.equal(consumes, 'application/json') + const consumes = require('../src/parser/openapi3/consumes.js')('POST','/pets'); + assert.equal(consumes, 'application/json'); }) - it('specifies openapi3', () => { + it('specifies openapi3.1', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json'); + globalThis.definition = require('../seeds/parserInitialGoodOpenApiExpanded3.1.json'); const consumes = require('../src/parser/openapi3/consumes.js')('POST','/pets'); assert.equal(consumes, 'application/json'); diff --git a/test/parser-endpoints.js b/test/parser-endpoints.js index 3e51c2a..7085240 100644 --- a/test/parser-endpoints.js +++ b/test/parser-endpoints.js @@ -1,52 +1,47 @@ -/** Part of APIAddicts. See LICENSE fileor full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ +/** Part of APIAddicts. See LICENSE file or full copyright and licensing details. Supported by Madrid Digital and CloudAPPi **/ -const assert = require('assert'); +const assert = require('node:assert'); const sinon = require('sinon'); +const getEndpoints = require('../src/parser/endpoints.js'); describe('parser-endpoints', () => { - + before(() => { sinon.stub(process, 'exit'); - }) + sinon.stub(console, 'log'); + sinon.stub(console, 'error'); + }); + + afterEach(() => { + process.exit.resetHistory(); + }); after(() => { process.exit.restore(); - }) - - it('bad swagger2', () => { - - global.definition = require('../seeds/parserEndpointsInitialBad.json'); - - require('../src/parser/endpoints.js')(); - - sinon.assert.called(process.exit); - sinon.assert.calledWith(process.exit, 1); - }) - - it('good swagger2', () => { + console.log.restore(); + console.error.restore(); + }); - global.definition = require('../seeds/parserEndpointsInitialGood.json'); + const checkEndpoints = (name, seed, expectedFile, shouldExit = false) => { + it(name, () => { + globalThis.definition = require(`../seeds/${seed}`); + const endpoints = getEndpoints(); - const endpoints = require('../src/parser/endpoints.js')(); - assert.deepEqual(endpoints, [ - { verb: 'GET', path: '/pets' }, - { verb: 'POST', path: '/pets' }, - { verb: 'GET', path: '/pets/{id}' }, - { verb: 'DELETE', path: '/pets/{id}' } - ]) - }) + if (shouldExit) { + sinon.assert.calledWith(process.exit, 1); + } else { + const expected = require(`../seeds/${expectedFile}`); + assert.deepStrictEqual(endpoints, expected); + } + }); + }; - it('good openapi3', () => { + checkEndpoints('endpoints bad swagger2', 'parserEndpointsInitialBad.json', null, true); - global.definition = require('../seeds/parserInitialGoodOpenApi3.json'); + checkEndpoints('endpoints good swagger2', 'parserEndpointsInitialGood.json', 'parserEndpointsGoodSwagger2Result.json'); - const endpoints = require('../src/parser/endpoints.js')(); - assert.deepEqual(endpoints, [ - { verb: 'GET', path: '/pets' }, - { verb: 'POST', path: '/pets' }, - { verb: 'GET', path: '/pets/{petId}' } - ]) - }) + checkEndpoints('endpoints good openapi3.0', 'parserInitialGoodOpenApi3.json', 'parserEndpointsGoodOpenApi3Result.json'); + checkEndpoints('endpoints good openapi3.1', 'parserInitialGoodOpenApi3.1.json', 'parserEndpointsGoodOpenApi3Result.json'); -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/test/parser-microcks.js b/test/parser-microcks.js index 0c72556..edf57ce 100644 --- a/test/parser-microcks.js +++ b/test/parser-microcks.js @@ -3,34 +3,39 @@ const assert = require('assert') describe('parser-microcks', () => { - + //If the document is not openapi3, it should return false because it is not supported it('specifies swagger2', () => { - - global.definition = require('../seeds/parserMicroksInitial.json') - + + globalThis.definition = require('../seeds/parserMicroksInitial.json') + const microcks = require('../src/parser/openapi3/microcks.js')('POST','/user') assert.equal(microcks, false) }) //If the document is not openapi3, it should return false because it is not supported it('general swagger2', () => { - - global.definition = require('../seeds/parserMicroksInitial.json') - + + globalThis.definition = require('../seeds/parserMicroksInitial.json') + const microcks = require('../src/parser/openapi3/microcks.js')('GET','/user/{username}') assert.equal(microcks, false) }) //If the document is openapi3, it should return true because it is supported - it('specifies openapi3', () => { - - global.definition = require('../seeds/parserMicroksinitialGood.json'); - + it('specifies openapi3.0', () => { + + globalThis.definition = require('../seeds/parserMicroksinitialGood3.json'); + const microcks = require('../src/parser/openapi3/microcks.js')('GET','/user/{username}'); assert.equal(microcks, true); }) - + it('specifies openapi3.1', () => { + + globalThis.definition = require('../seeds/parserMicroksinitialGood3.1.json'); + const microcks = require('../src/parser/openapi3/microcks.js')('GET','/user/{username}'); + assert.equal(microcks, true); + }) }) diff --git a/test/parser-pathParameters.js b/test/parser-pathParameters.js index 6415713..5a0dbee 100644 --- a/test/parser-pathParameters.js +++ b/test/parser-pathParameters.js @@ -6,15 +6,23 @@ describe('parser-pathParameters', () => { it('good swagger2', () => { - global.definition = require('../seeds/parserInitialGood.json') + globalThis.definition = require('../seeds/parserInitialGood.json') const pathParameters = require('../src/parser/swagger2/pathParameters.js')('DELETE','/pets/{id}') assert.deepStrictEqual(pathParameters, [ { name: 'id', type: 'integer', example: undefined } ]) }) - it('good openapi3', () => { + it('good openapi3.0', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3.json') + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.json') + + const status = require('../src/parser/openapi3/pathParameters.js')('GET','/pets/{petId}') + assert.deepStrictEqual(status, [ { name: 'petId', type: 'string', example: 'asdf' } ]) + }) + + it('good openapi3.1', () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.json') const status = require('../src/parser/openapi3/pathParameters.js')('GET','/pets/{petId}') assert.deepStrictEqual(status, [ { name: 'petId', type: 'string', example: 'asdf' } ]) diff --git a/test/parser-queryParams.js b/test/parser-queryParams.js index 3806fb4..f38fc2f 100644 --- a/test/parser-queryParams.js +++ b/test/parser-queryParams.js @@ -3,55 +3,30 @@ const assert = require('assert'); describe('parser-queryParams', () => { - - it('fill swagger2', () => { - - global.definition = require('../seeds/parserQueryParamsGood.json') - - const queryParams = require('../src/parser/swagger2/queryParams.js')('GET','/pets') - assert.deepStrictEqual(queryParams, [{ - "name": "tags", - "required": false, - "type": "array" - }, - { - "name": "limit", - "required": false, - "type": "integer" - } ]) - }) - - it('void swagger2', () => { - - global.definition = require('../seeds/parserQueryParamsGood.json') - - const queryParams = require('../src/parser/swagger2/queryParams.js')('POST','/pets') - assert.deepStrictEqual(queryParams, []) - }) - - it('fill openapi3', () => { - - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json') - - const queryParams = require('../src/parser/openapi3/queryParams.js')('GET','/pets') - assert.deepStrictEqual(queryParams, [{ - "name": "tags", - "required": false, - "type": "array" - }, - { - "name": "limit", - "required": true, - "type": "integer" - } ]) - }) - - it('void openapi3', () => { - - global.definition = require('../seeds/parserInitialGoodOpenApi3Expanded.json') - - const queryParams = require('../src/parser/openapi3/queryParams.js')('POST','/pets') - assert.deepStrictEqual(queryParams, []) - }) - -}) \ No newline at end of file + + const testQuery = (name, parserPath, seed, method, path, expected) => { + it(name, () => { + globalThis.definition = require(`../seeds/${seed}`); + const queryParams = require(`../src/parser/${parserPath}/queryParams.js`)(method, path); + assert.deepStrictEqual(queryParams, expected); + }); + }; + + const results = { + empty: [], + tagsLimit: (reqLimit) => [ + { "name": "tags", "required": false, "type": "array" }, + { "name": "limit", "required": reqLimit, "type": "integer" } + ] + }; + + testQuery('queryParams fill swagger2', 'swagger2', 'parserQueryParamsGood.json', 'GET', '/pets', results.tagsLimit(false)); + testQuery('queryParams void swagger2', 'swagger2', 'parserQueryParamsGood.json', 'POST', '/pets', results.empty); + + testQuery('queryParams fill openapi3.0', 'openapi3', 'parserInitialGoodOpenApiExpanded3.json', 'GET', '/pets', results.tagsLimit(true)); + testQuery('queryParams void openapi3.0', 'openapi3', 'parserInitialGoodOpenApiExpanded3.json', 'POST', '/pets', results.empty); + + testQuery('queryParams fill openapi3.1', 'openapi3', 'parserInitialGoodOpenApiExpanded3.1.json', 'GET', '/pets', results.tagsLimit(true)); + testQuery('queryParams void openapi3.1', 'openapi3', 'parserInitialGoodOpenApiExpanded3.1.json', 'POST', '/pets', results.empty); + +}); \ No newline at end of file diff --git a/test/parser-schemaHostBasePath.js b/test/parser-schemaHostBasePath.js index 55d283c..073aad3 100644 --- a/test/parser-schemaHostBasePath.js +++ b/test/parser-schemaHostBasePath.js @@ -3,20 +3,28 @@ const assert = require('assert'); describe('parser-schemaHostBasePath', () => { - + it('good swagger2', () => { - global.definition = require('../seeds/parserSchemaHostBasePathInitialGood.json'); + globalThis.definition = require('../seeds/parserSchemaHostBasePathInitialGood.json'); const schemaHostBasePath = require('../src/parser/swagger2/schemaHostBasePath.js')(); assert.deepStrictEqual(schemaHostBasePath, { host: 'https://myserver.com', basePath: '/first/second' }); }) - it('good openapi3', () => { + it('good openapi3.0', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3.json'); + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.json'); const schemaHostBasePath = require('../src/parser/openapi3/schemaHostBasePath.js')(); assert.deepStrictEqual(schemaHostBasePath, { host: 'http://petstore.swagger.io', basePath: '/v1' }); }) + + it('good openapi3.1', () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.json'); + + const schemaHostBasePath = require('../src/parser/openapi3/schemaHostBasePath.js')(); + assert.deepStrictEqual(schemaHostBasePath, { host: 'https://petstore.swagger.io', basePath: '/v1' }); + }) }); \ No newline at end of file diff --git a/test/parser-status.js b/test/parser-status.js index b0d83c4..28b7009 100644 --- a/test/parser-status.js +++ b/test/parser-status.js @@ -3,18 +3,26 @@ const assert = require('assert'); describe('parser-status', () => { - + it('good swagger2', () => { - global.definition = require('../seeds/parserInitialGood.json') + globalThis.definition = require('../seeds/parserInitialGood.json') const status = require('../src/parser/status.js')('DELETE','/pets/{id}') assert.deepStrictEqual(status, [ 204, 500 ]) }) - it('good openapi3', () => { + it('good openapi3.0', () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.json') + + const status = require('../src/parser/status.js')('POST','/pets') + assert.deepStrictEqual(status, [ 201, 500 ]) + }) + + it('good openapi3.1', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3.json') + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.json') const status = require('../src/parser/status.js')('POST','/pets') assert.deepStrictEqual(status, [ 201, 500 ]) diff --git a/test/parser-summary.js b/test/parser-summary.js index f8b1211..4f6a74f 100644 --- a/test/parser-summary.js +++ b/test/parser-summary.js @@ -6,15 +6,23 @@ describe('parser-summary', () => { it('good swagger2', () => { - global.definition = require('../seeds/parserSummaryInitial.json') + globalThis.definition = require('../seeds/parserSummaryInitial.json') const summary = require('../src/parser/summary.js')('POST','/pets') assert.equal(summary, "Create a pet") }) - it('good swagger2', () => { + it('good openapi3.0', () => { + + globalThis.definition = require('../seeds/parserSummaryOpenApi3.json') + + const summary = require('../src/parser/summary.js')('POST','/pets') + assert.equal(summary, "Create pet") + }) + + it('good openapi3.1', () => { - global.definition = require('../seeds/parserSummaryOpenApi3.json') + globalThis.definition = require('../seeds/parserSummaryOpenApi3.1.json') const summary = require('../src/parser/summary.js')('POST','/pets') assert.equal(summary, "Create pet") diff --git a/test/parser-version.js b/test/parser-version.js index a240676..b466e06 100644 --- a/test/parser-version.js +++ b/test/parser-version.js @@ -6,32 +6,56 @@ describe('parser-version', () => { it('swagger2', () => { - global.definition = require('../seeds/parserInitialGood.json') + globalThis.definition = require('../seeds/parserInitialGood.json') const version = require('../src/parser/version.js')() assert.equal(version, 'swagger2') }); - it('openapi3', () => { + it('openapi3.0', () => { - global.definition = require('../seeds/parserInitialGoodOpenApi3.json') + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.json') const version = require('../src/parser/version.js')() assert.equal(version, 'openapi3') }); - it('openapi versio 3.0.2' , () => { - - global.definition = require('../seeds/parserInitialGoodOpenApi3.0.2.json') - + it('openapi version 3.0.2' , () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.0.2.json') + const version = require('../src/parser/version.js')() assert.equal(version, 'openapi3') }) - it('openapi versio 3.0.3' , () => { - - global.definition = require('../seeds/parserInitialGoodOpenApi3.0.3.json') - + it('openapi version 3.0.3' , () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.0.3.json') + + const version = require('../src/parser/version.js')() + assert.equal(version, 'openapi3') + }) + + it('openapi3.1', () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.json') + + const version = require('../src/parser/version.js')() + assert.equal(version, 'openapi3') + }); + + it('openapi version 3.1.1' , () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.1.json') + + const version = require('../src/parser/version.js')() + assert.equal(version, 'openapi3') + }) + + it('openapi version 3.1.2' , () => { + + globalThis.definition = require('../seeds/parserInitialGoodOpenApi3.1.2.json') + const version = require('../src/parser/version.js')() assert.equal(version, 'openapi3') })