From 373626ed598447bb670ac67e97ca8ed1bf32f28f Mon Sep 17 00:00:00 2001 From: kpears201 Date: Thu, 9 Nov 2023 06:11:41 -0800 Subject: [PATCH] fix: Provider openrpc response and error methods have a wrapper object (#148) --- src/macrofier/engine.mjs | 2 +- src/shared/modules.mjs | 45 +++++++++++++++----------------- test/openrpc/simple.json | 55 +++++++++++++++++++++++++++++++++++++++ test/suite/simple.test.js | 38 ++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 26 deletions(-) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 73a805fd..543e9a57 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -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` : '' diff --git a/src/shared/modules.mjs b/src/shared/modules.mjs index 927092c2..f1efc1bc 100644 --- a/src/shared/modules.mjs +++ b/src/shared/modules.mjs @@ -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 } ] @@ -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: { @@ -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: { diff --git a/test/openrpc/simple.json b/test/openrpc/simple.json index 0d7f41c7..7d009546 100644 --- a/test/openrpc/simple.json +++ b/test/openrpc/simple.json @@ -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": { diff --git a/test/suite/simple.test.js b/test/suite/simple.test.js index e9ca5fd7..c969afce 100644 --- a/test/suite/simple.test.js +++ b/test/suite/simple.test.js @@ -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)) +});