From 8172c46427840b99a01aa077f7eb1c2f834de23b Mon Sep 17 00:00:00 2001 From: Nora Date: Thu, 16 May 2019 09:49:18 -0400 Subject: [PATCH] fixup! apply feedback --- .../controller-spec.integration.ts | 316 +++++++++++------- 1 file changed, 187 insertions(+), 129 deletions(-) diff --git a/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts b/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts index 22ec185fe52c..aafbde53fad9 100644 --- a/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts +++ b/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts @@ -208,21 +208,23 @@ describe('controller spec', () => { }); }); - it('allows operations to provide definition of referenced models through #/components/schema', () => { - class MyController { - @get('/todos', { - responses: { - '200': { - description: 'Array of Category model instances', - content: { - 'application/json': { - schema: { - $ref: '#/components/schemas/Todo', - definitions: { - Todo: { - title: 'Todo', - properties: { - title: {type: 'string'}, + context('reference models via spec', () => { + it('allows operations to provide definitions of referenced models through #/components/schema', () => { + class MyController { + @get('/todos', { + responses: { + '200': { + description: 'Array of Category model instances', + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Todo', + definitions: { + Todo: { + title: 'Todo', + properties: { + title: {type: 'string'}, + }, }, }, }, @@ -230,48 +232,48 @@ describe('controller spec', () => { }, }, }, - }, - }) - async find(): Promise { - return []; // dummy implementation, it's never called + }) + async find(): Promise { + return []; // dummy implementation, it's never called + } } - } - const spec = getControllerSpec(MyController); - const opSpec: OperationObject = spec.paths['/todos'].get; - const responseSpec = opSpec.responses['200'].content['application/json']; - expect(responseSpec.schema).to.deepEqual({ - $ref: '#/components/schemas/Todo', - }); + const spec = getControllerSpec(MyController); + const opSpec: OperationObject = spec.paths['/todos'].get; + const responseSpec = opSpec.responses['200'].content['application/json']; + expect(responseSpec.schema).to.deepEqual({ + $ref: '#/components/schemas/Todo', + }); - const globalSchemas = (spec.components || {}).schemas; - expect(globalSchemas).to.deepEqual({ - Todo: { - title: 'Todo', - properties: { - title: { - type: 'string', + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.deepEqual({ + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, }, }, - }, + }); }); - }); - it('allows operations to provide definition of referenced models through #/definitions', () => { - class MyController { - @get('/todos', { - responses: { - '200': { - description: 'Array of Category model instances', - content: { - 'application/json': { - schema: { - $ref: '#/definitions/Todo', - definitions: { - Todo: { - title: 'Todo', - properties: { - title: {type: 'string'}, + it('allows operations to provide definitions of referenced models through #/definitions', () => { + class MyController { + @get('/todos', { + responses: { + '200': { + description: 'Array of Category model instances', + content: { + 'application/json': { + schema: { + $ref: '#/definitions/Todo', + definitions: { + Todo: { + title: 'Todo', + properties: { + title: {type: 'string'}, + }, }, }, }, @@ -279,73 +281,129 @@ describe('controller spec', () => { }, }, }, - }, - }) - async find(): Promise { - return []; // dummy implementation, it's never called + }) + async find(): Promise { + return []; // dummy implementation, it's never called + } } - } - const spec = getControllerSpec(MyController); - const opSpec: OperationObject = spec.paths['/todos'].get; - const responseSpec = opSpec.responses['200'].content['application/json']; - expect(responseSpec.schema).to.deepEqual({ - $ref: '#/definitions/Todo', - }); + const spec = getControllerSpec(MyController); + const opSpec: OperationObject = spec.paths['/todos'].get; + const responseSpec = opSpec.responses['200'].content['application/json']; + expect(responseSpec.schema).to.deepEqual({ + $ref: '#/definitions/Todo', + }); - const globalSchemas = (spec.components || {}).schemas; - expect(globalSchemas).to.deepEqual({ - Todo: { - title: 'Todo', - properties: { - title: { - type: 'string', + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.deepEqual({ + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, }, }, - }, + }); }); - }); - it('returns an empty object when it cannot find definition of referenced model', () => { - class MyController { - @get('/todos', { - responses: { - '200': { - description: 'Array of Category model instances', - content: { - 'application/json': { - schema: { - $ref: '#/definitions/Todo', - definitions: {}, + it('returns undefined when it cannot find definition of referenced model', () => { + class MyController { + @get('/todos', { + responses: { + '200': { + description: 'Array of Category model instances', + content: { + 'application/json': { + schema: { + $ref: '#/definitions/Todo', + }, + }, + }, + }, + }, + }) + async find(): Promise { + return []; // dummy implementation, it's never called + } + } + + const spec = getControllerSpec(MyController); + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.be.undefined(); + }); + + it('gets definition from outside the method decorator when it is not provided', () => { + @api({ + paths: {}, + components: { + schemas: { + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', }, }, }, }, }, }) - async find(): Promise { - return []; // dummy implementation, it's never called + class MyController { + @get('/todos', { + responses: { + '200': { + description: 'Array of Category model instances', + content: { + 'application/json': { + schema: { + $ref: '#/definitions/Todo', + definitions: {}, + }, + }, + }, + }, + }, + }) + async find(): Promise { + return []; // dummy implementation, it's never called + } } - } - const spec = getControllerSpec(MyController); - const globalSchemas = (spec.components || {}).schemas; - expect(globalSchemas).to.be.empty(); - }); + const spec = getControllerSpec(MyController); + const opSpec: OperationObject = spec.paths['/todos'].get; + const responseSpec = opSpec.responses['200'].content['application/json']; + expect(responseSpec.schema).to.deepEqual({ + $ref: '#/definitions/Todo', + }); - it('allows a class to provide definitions of referenced models through #/components/schemas', () => { - @api({ - paths: { - '/todos': { - get: { - 'x-operation-name': 'find', - 'x-controller-name': 'MyController', - responses: { - '200': { - content: { - 'application/json': { - schema: { - $ref: '#/components/schemas/Todo', + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.deepEqual({ + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, + }, + }, + }); + }); + + it('allows a class to reference schemas at @api level', () => { + @api({ + paths: { + '/todos': { + get: { + 'x-operation-name': 'find', + 'x-controller-name': 'MyController', + responses: { + '200': { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Todo', + }, }, }, }, @@ -353,43 +411,43 @@ describe('controller spec', () => { }, }, }, - }, - components: { - schemas: { - Todo: { - title: 'Todo', - properties: { - title: { - type: 'string', + components: { + schemas: { + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, }, }, }, }, - }, - }) - class MyController { - async find(): Promise { - return []; // dummy implementation, it's never called + }) + class MyController { + async find(): Promise { + return []; // dummy implementation, it's never called + } } - } - const spec = getControllerSpec(MyController); - const opSpec: OperationObject = spec.paths['/todos'].get; - const responseSpec = opSpec.responses['200'].content['application/json']; - expect(responseSpec.schema).to.deepEqual({ - $ref: '#/components/schemas/Todo', - }); + const spec = getControllerSpec(MyController); + const opSpec: OperationObject = spec.paths['/todos'].get; + const responseSpec = opSpec.responses['200'].content['application/json']; + expect(responseSpec.schema).to.deepEqual({ + $ref: '#/components/schemas/Todo', + }); - const globalSchemas = (spec.components || {}).schemas; - expect(globalSchemas).to.deepEqual({ - Todo: { - title: 'Todo', - properties: { - title: { - type: 'string', + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.deepEqual({ + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, }, }, - }, + }); }); });