-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add translate callback support (#211)
* fix: add translate callback test * fix: add callback context to generate id * fix: updates based on pr feedback
- Loading branch information
1 parent
bf82030
commit 382dcff
Showing
6 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { idGenerators } from '../generators'; | ||
|
||
describe('idGenerators', () => { | ||
it('httpOperation ids should be unique', () => { | ||
const operation1 = { parentId: '12345', method: 'post', path: '/test/path/{id}' }; | ||
const operation2 = { parentId: '12345', method: 'post', path: '/test/path/id' }; | ||
const id1 = idGenerators.httpOperation(operation1); | ||
const id2 = idGenerators.httpOperation(operation2); | ||
expect(id1).toEqual('http_operation-12345-post-/test/path/{}'); | ||
expect(id2).toEqual('http_operation-12345-post-/test/path/id'); | ||
expect(id1).not.toEqual(id2); | ||
}); | ||
it('httpOperation ids should be data resilient', () => { | ||
const operation1 = { parentId: '12345', method: 'post', path: '/test/path/{id}' }; | ||
const operation2 = { parentId: '12345', method: 'post', path: '/test/path/{name}' }; | ||
const id1 = idGenerators.httpOperation(operation1); | ||
const id2 = idGenerators.httpOperation(operation2); | ||
expect(id1).toEqual('http_operation-12345-post-/test/path/{}'); | ||
expect(id2).toEqual('http_operation-12345-post-/test/path/{}'); | ||
expect(id1).toEqual(id2); | ||
}); | ||
it('httpCallbackOperation ids should be unique', () => { | ||
const operation1 = { parentId: '12345', method: 'post', path: '{$request.body#/returnedPetAdoptedUrl}' }; | ||
const operation2 = { parentId: '12345', method: 'post', path: '{$request.body#/newPetAvailableUrl}' }; | ||
const id1 = idGenerators.httpCallbackOperation(operation1); | ||
const id2 = idGenerators.httpCallbackOperation(operation2); | ||
expect(id1).toEqual('http_callback-12345-post-{$request.body#/returnedPetAdoptedUrl}'); | ||
expect(id2).toEqual('http_callback-12345-post-{$request.body#/newPetAvailableUrl}'); | ||
expect(id1).not.toEqual(id2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
import { DeepPartial } from '@stoplight/types'; | ||
import { OpenAPIObject } from 'openapi3-ts'; | ||
|
||
import { createContext } from '../../../oas/context'; | ||
import { translateToCallbacks as _translateToCallbacks } from '../callbacks'; | ||
|
||
const translateToCallbacks = (document: DeepPartial<OpenAPIObject>, callbacks: unknown) => | ||
_translateToCallbacks.call(createContext(document), callbacks); | ||
|
||
describe('translateToCallbacks', () => { | ||
it('given multiple callback entries, it should translate', () => { | ||
const callbackEntries = { | ||
newPetWebhook: { | ||
'{$request.body#/newPetAvailableUrl}': { | ||
post: { | ||
requestBody: { | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
message: { | ||
type: 'string', | ||
example: 'A new pet has arrived', | ||
}, | ||
}, | ||
required: ['message'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
'200': { | ||
description: 'Your server returns this code if it accepts the callback', | ||
}, | ||
}, | ||
}, | ||
}, | ||
'{$request.body#/returnedPetAvailableUrl}': { | ||
post: { | ||
requestBody: { | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
message: { | ||
type: 'string', | ||
example: 'A pet has been returned', | ||
}, | ||
}, | ||
required: ['message'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
'200': { | ||
description: 'Your server returns this code if it accepts the callback', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
petAdopted: { | ||
'{$request.body#/adoptedUrl}': { | ||
post: { | ||
requestBody: { | ||
required: true, | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
message: { | ||
type: 'string', | ||
example: 'A pet has been adopted', | ||
}, | ||
}, | ||
required: ['message'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
'200': { | ||
description: 'Your server returns this code if it accepts the callback', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
expect(translateToCallbacks({}, callbackEntries)).toStrictEqual([ | ||
{ | ||
callbackName: 'newPetWebhook', | ||
extensions: {}, | ||
id: '3245690b6a7fc', | ||
method: 'post', | ||
path: '{$request.body#/newPetAvailableUrl}', | ||
request: { | ||
body: { | ||
contents: [ | ||
{ | ||
encodings: [], | ||
examples: [], | ||
id: expect.any(String), | ||
mediaType: 'application/json', | ||
schema: { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
message: { | ||
examples: ['A new pet has arrived'], | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['message'], | ||
type: 'object', | ||
'x-stoplight': { | ||
id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
], | ||
id: expect.any(String), | ||
required: true, | ||
}, | ||
cookie: [], | ||
headers: [], | ||
path: [], | ||
query: [], | ||
}, | ||
responses: [ | ||
{ | ||
code: '200', | ||
contents: [], | ||
description: 'Your server returns this code if it accepts the callback', | ||
headers: [], | ||
id: expect.any(String), | ||
}, | ||
], | ||
security: [], | ||
servers: [], | ||
tags: [], | ||
}, | ||
{ | ||
callbackName: 'newPetWebhook', | ||
extensions: {}, | ||
id: '07041d5723f4a', | ||
method: 'post', | ||
path: '{$request.body#/returnedPetAvailableUrl}', | ||
request: { | ||
body: { | ||
contents: [ | ||
{ | ||
encodings: [], | ||
examples: [], | ||
id: expect.any(String), | ||
mediaType: 'application/json', | ||
schema: { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
message: { | ||
examples: ['A pet has been returned'], | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['message'], | ||
type: 'object', | ||
'x-stoplight': { | ||
id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
], | ||
id: expect.any(String), | ||
required: true, | ||
}, | ||
cookie: [], | ||
headers: [], | ||
path: [], | ||
query: [], | ||
}, | ||
responses: [ | ||
{ | ||
code: '200', | ||
contents: [], | ||
description: 'Your server returns this code if it accepts the callback', | ||
headers: [], | ||
id: expect.any(String), | ||
}, | ||
], | ||
security: [], | ||
servers: [], | ||
tags: [], | ||
}, | ||
{ | ||
callbackName: 'petAdopted', | ||
extensions: {}, | ||
id: '2333951a518f9', | ||
method: 'post', | ||
path: '{$request.body#/adoptedUrl}', | ||
request: { | ||
body: { | ||
contents: [ | ||
{ | ||
encodings: [], | ||
examples: [], | ||
id: expect.any(String), | ||
mediaType: 'application/json', | ||
schema: { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
message: { | ||
examples: ['A pet has been adopted'], | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['message'], | ||
type: 'object', | ||
'x-stoplight': { | ||
id: expect.any(String), | ||
}, | ||
}, | ||
}, | ||
], | ||
id: expect.any(String), | ||
required: true, | ||
}, | ||
cookie: [], | ||
headers: [], | ||
path: [], | ||
query: [], | ||
}, | ||
responses: [ | ||
{ | ||
code: '200', | ||
contents: [], | ||
description: 'Your server returns this code if it accepts the callback', | ||
headers: [], | ||
id: expect.any(String), | ||
}, | ||
], | ||
security: [], | ||
servers: [], | ||
tags: [], | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters