diff --git a/__tests__/__fixtures__/invalid-ref-oas/external-components.json b/__tests__/__fixtures__/invalid-ref-oas/external-components.json new file mode 100644 index 000000000..0a87758ce --- /dev/null +++ b/__tests__/__fixtures__/invalid-ref-oas/external-components.json @@ -0,0 +1,179 @@ +{ + "requestBodies": { + "Pet": { + "content": { + "application/json": { + "schema": { + "$ref": "#/schemas/Pet" + } + }, + "application/xml": { + "schema": { + "$ref": "#/schemas/Pet" + } + } + }, + "description": "Pet object that needs to be added to the store", + "required": true + } + }, + "schemas": { + "Order": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "petId": { + "type": "integer", + "format": "int64" + }, + "quantity": { + "type": "integer", + "format": "int32" + }, + "shipDate": { + "type": "string", + "format": "date-time" + }, + "status": { + "type": "string", + "description": "Order Status", + "enum": [ + "placed", + "approved", + "delivered" + ] + }, + "complete": { + "type": "boolean", + "default": false + } + }, + "xml": { + "name": "Order" + } + }, + "Category": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + } + }, + "xml": { + "name": "Category" + } + }, + "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", + "description": "User Status" + } + }, + "xml": { + "name": "User" + } + }, + "Tag": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + } + }, + "xml": { + "name": "Tag" + } + }, + "Pet": { + "type": "object", + "required": [ + "name", + "photoUrls" + ], + "properties": { + "id": { + "type": "integer", + "format": "int64", + "default": 40, + "example": 25 + }, + "category": { + "$ref": "#/schemas/Category" + }, + "name": { + "type": "string", + "example": "doggie" + }, + "photoUrls": { + "type": "array", + "xml": { + "name": "photoUrl", + "wrapped": true + }, + "items": { + "type": "string", + "example": "https://example.com/photo.png" + } + }, + "tags": { + "type": "array", + "xml": { + "name": "tag", + "wrapped": true + }, + "items": { + "$ref": "#/schemas/Tag" + } + }, + "status": { + "type": "string", + "description": "pet status in the store", + "enum": [ + "available", + "pending", + "sold" + ] + } + }, + "xml": { + "name": "Pet" + } + } + } +} diff --git a/__tests__/__fixtures__/invalid-ref-oas/petstore.json b/__tests__/__fixtures__/invalid-ref-oas/petstore.json new file mode 100644 index 000000000..a6df77458 --- /dev/null +++ b/__tests__/__fixtures__/invalid-ref-oas/petstore.json @@ -0,0 +1,132 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "Example petstore to demo our handling of external $ref pointers" + }, + "servers": [ + { + "url": "http://petstore.swagger.io/v2" + } + ], + "paths": { + "/pet": { + "post": { + "tags": ["pet"], + "summary": "Add a new pet to the store", + "description": "", + "operationId": "addPet", + "requestBody": { + "$ref": "__tests__/__fixtures__/ref-oas/external-components.json#/requestBodies/Petty" + }, + "responses": { + "405": { + "description": "Invalid input" + } + }, + "security": [ + { + "petstore_auth": ["write:pets", "read:pets"] + } + ] + }, + "put": { + "tags": ["pet"], + "summary": "Update an existing pet", + "description": "", + "operationId": "updatePet", + "requestBody": { + "$ref": "__tests__/__fixtures__/ref-oas/external-components.json#/requestBodies/Petty" + }, + "responses": { + "400": { + "description": "Invalid ID supplied" + }, + "404": { + "description": "Pet not found" + }, + "405": { + "description": "Validation exception" + } + }, + "security": [ + { + "petstore_auth": ["write:pets", "read:pets"] + } + ] + } + }, + "/pet/{petId}": { + "get": { + "tags": ["pet"], + "summary": "Find pet by ID", + "description": "Returns a single pet", + "operationId": "getPetById", + "parameters": [ + { + "name": "petId", + "in": "path", + "description": "ID of pet to return", + "required": true, + "schema": { + "type": "integer", + "format": "int64" + } + } + ], + "responses": { + "200": { + "description": "successful operation", + "content": { + "application/xml": { + "schema": { + "$ref": "__tests__/__fixtures__/ref-oas/external-components.json#/schemas/Petty" + } + }, + "application/json": { + "schema": { + "$ref": "__tests__/__fixtures__/ref-oas/external-components.json#/schemas/Petty" + } + } + } + }, + "400": { + "description": "Invalid ID supplied" + }, + "404": { + "description": "Pet not found" + }, + "default": { + "description": "successful response" + } + }, + "security": [ + { + "api_key": [] + } + ] + } + } + }, + "components": { + "securitySchemes": { + "petstore_auth": { + "type": "oauth2", + "flows": { + "implicit": { + "authorizationUrl": "http://petstore.swagger.io/oauth/dialog", + "scopes": { + "write:pets": "modify pets in your account", + "read:pets": "read your pets" + } + } + } + }, + "api_key": { + "type": "apiKey", + "name": "api_key", + "in": "header" + } + } + } +} diff --git a/__tests__/cmds/__snapshots__/openapi.test.js.snap b/__tests__/cmds/__snapshots__/openapi.test.js.snap index 5ac7da20e..0435abd2d 100644 --- a/__tests__/cmds/__snapshots__/openapi.test.js.snap +++ b/__tests__/cmds/__snapshots__/openapi.test.js.snap @@ -14,6 +14,8 @@ REQUIRED must have required property 'name' 29 | }] `; +exports[`rdme openapi error handling should throw an error if an invalid ref is supplied 1`] = `[MissingPointerError: Token "Petty" does not exist.]`; + exports[`rdme openapi upload should bundle and upload the expected content 1`] = ` Object { "components": Object { diff --git a/__tests__/cmds/openapi.test.js b/__tests__/cmds/openapi.test.js index cf56519c8..6e06b304c 100644 --- a/__tests__/cmds/openapi.test.js +++ b/__tests__/cmds/openapi.test.js @@ -387,6 +387,12 @@ describe('rdme openapi', () => { ).rejects.toMatchSnapshot(); }); + it.only('should throw an error if an invalid ref is supplied', () => { + return expect( + openapi.run({ spec: './__tests__/__fixtures__/invalid-ref-oas/petstore.json', key, id, version }) + ).rejects.toMatchSnapshot(); + }); + it('should throw an error if an invalid Swagger definition is supplied (create)', async () => { const errorObject = { error: 'INTERNAL_ERROR', diff --git a/src/lib/prepareOas.js b/src/lib/prepareOas.js index 0d2158f9a..1d144c4f8 100644 --- a/src/lib/prepareOas.js +++ b/src/lib/prepareOas.js @@ -34,15 +34,10 @@ module.exports = async function prepare(path, bundle = false) { let bundledSpec = ''; if (bundle) { - bundledSpec = await oas - .bundle() - .then(res => { - return JSON.stringify(res); - }) - .catch(err => { - debug(`raw bundling error object: ${JSON.stringify(err)}`); - throw err; - }); + bundledSpec = await oas.bundle().then(res => { + return JSON.stringify(res); + }); + debug('spec bundled'); }