Skip to content

Commit

Permalink
fix: Provider openrpc response and error methods have a wrapper object (
Browse files Browse the repository at this point in the history
  • Loading branch information
kpears201 authored Nov 9, 2023
1 parent c274ac4 commit 373626e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
}
}

const paramDelimiter = config.operators ? config.operators.paramDelimiter : ', '
const paramDelimiter = config.operators ? config.operators.paramDelimiter : ''

const temporalItemName = isTemporalSetMethod(methodObj) ? methodObj.result.schema.items && methodObj.result.schema.items.title || 'Item' : ''
const temporalAddName = isTemporalSetMethod(methodObj) ? `on${temporalItemName}Available` : ''
Expand Down
45 changes: 21 additions & 24 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -625,21 +625,16 @@ const createResponseFromProvider = (provider, type, json) => {
if (provider.tags.find(t => t[`x-${type.toLowerCase()}`])) {
response.params = [
{
name: type.toLowerCase(),
required: true,
name: "correlationId",
schema: {
allOf: [
{
"$ref": "https://meta.comcast.com/firebolt/types#/definitions/ProviderResponse" // use this schema for both Errors and Results
},
{
"type": "object",
"properties": {
"result": provider.tags.find(t => t[`x-${type.toLowerCase()}`])[`x-${type.toLowerCase()}`]
}
}
]
}
type: "string"
},
required: true
},
{
name: type === 'Error' ? 'error' : "result",
schema: provider.tags.find(t => t[`x-${type.toLowerCase()}`])[`x-${type.toLowerCase()}`],
required: true
}
]

Expand Down Expand Up @@ -680,11 +675,12 @@ const createResponseFromProvider = (provider, type, json) => {
name: schema.examples.length === 1 ? "Example" : `Example #${n++}`,
params: [
{
name: `${type.toLowerCase()}`,
value: {
correlationId: "123",
result: param
}
name: 'correlationId',
value: '123'
},
{
name: 'result',
value: param
}
],
result: {
Expand Down Expand Up @@ -723,11 +719,12 @@ const createResponseFromProvider = (provider, type, json) => {
name: 'Example 1',
params: [
{
name: `${type.toLowerCase()}`,
value: {
correlationId: "123",
result: value
}
name: 'correlationId',
value: '123'
},
{
name: type === 'Error' ? 'error' : 'result',
value
}
],
result: {
Expand Down
55 changes: 55 additions & 0 deletions test/openrpc/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,61 @@
}
}
]
},
{
"name": "methodWithMultipleParams",
"summary": "A method that takes two parameters",
"description": "A method that takes two parameters",
"tags": [
{
"name": "capabilities",
"x-uses": ["xrn:firebolt:capability:test:test"]
}
],
"params": [
{
"name": "id",
"required": true,
"schema": {
"type": "number"
},
"summary": "A test number"
},
{
"name": "title",
"required": true,
"schema": {
"type": "string"
},
"summary": "A string test parameter."
}
],
"result": {
"name": "result",
"summary": "A result for testing basic method generation.",
"schema": {
"type": "null"
}
},
"examples": [
{
"name": "Default Example",
"params": [
{
"name": "id",
"value": 42
},
{
"name": "title",
"value": "test"
}
],
"result": {
"name": "Default Result",
"value": null
}
}
]
}
],
"components": {
Expand Down
38 changes: 37 additions & 1 deletion test/suite/simple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,47 @@
*/

import { Simple } from '../../build/sdk/javascript/src/sdk.mjs'
import Setup from '../Setup'
import { expect } from '@jest/globals';


class TransportSpy {

constructor(spy) {
this.spy = spy
this.responder = null
}

async send(msg) {
let parsed = JSON.parse(msg)
this.spy(parsed)
this.responder(JSON.stringify({
jsonrpc: '2.0',
id: parsed.id,
result: {}
}))
}

receive(callback) {
this.responder = callback
}
}

test('Basic', () => {
return Simple.method(true).then(result => {
expect(result.foo).toBe("here's foo")
})
});

test('Multiple Parameters', async () => {
let cb = null;
let promise = new Promise((resolve, reject) => {
cb = resolve
})
window['__firebolt'].setTransportLayer(new TransportSpy(cb))
await Simple.methodWithMultipleParams(5, 'foo')
let msg = await promise
expect(msg.method).toBe('simple.methodWithMultipleParams')
expect(msg.params.id).toBe(5)
expect(msg.params.title).toBe('foo')
console.log(JSON.stringify(msg))
});

0 comments on commit 373626e

Please sign in to comment.