From 1518d3c711ae8e0e3300457fcbebad72ece5d72a Mon Sep 17 00:00:00 2001 From: Bernd Hufmann Date: Fri, 30 Aug 2024 15:31:04 -0400 Subject: [PATCH] Add support for json-schema to configuration types The json-schema is stored as a json object. See eclipse-cdt-cloud/trace-server-protocol#103 Signed-off-by: Bernd Hufmann --- .../fetch-configuration-sources-0.json | 29 ++++++++++++- .../src/models/configuration-source.ts | 12 +++++- .../src/protocol/tsp-client.test.ts | 43 +++++++++++++------ 3 files changed, 68 insertions(+), 16 deletions(-) diff --git a/tsp-typescript-client/fixtures/tsp-client/fetch-configuration-sources-0.json b/tsp-typescript-client/fixtures/tsp-client/fetch-configuration-sources-0.json index 165e8e3..3fc829b 100644 --- a/tsp-typescript-client/fixtures/tsp-client/fetch-configuration-sources-0.json +++ b/tsp-typescript-client/fixtures/tsp-client/fetch-configuration-sources-0.json @@ -14,5 +14,32 @@ "keyName": "test1" } ] + }, + { + "id": "my-source-type-2-id", + "name": "My configuration source 2", + "description": "My configuration source 2 description", + "schema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://org.eclipse.tracecompass/custom-execution-analysis.json", + "title": "Custom Execution Analysis", + "description": "Custom Execution Analysis schema", + "type": "object", + "properties": { + "cpus": { + "description": "array of integer", + "type": "array", + "items": { + "type": "number" + } + }, + "phone": { + "description": "Phone number", + "type": "string", + "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" + } + }, + "required": ["cpus"] + } } -] \ No newline at end of file +] diff --git a/tsp-typescript-client/src/models/configuration-source.ts b/tsp-typescript-client/src/models/configuration-source.ts index a3f58bc..e1986f1 100644 --- a/tsp-typescript-client/src/models/configuration-source.ts +++ b/tsp-typescript-client/src/models/configuration-source.ts @@ -20,9 +20,17 @@ export interface ConfigurationSourceType { /** * A list of query parameter keys to be passed when creating - * configuration instance of this type + * configuration instance of this type. Use this instead of + * schema. Omit if not used. */ - parameterDescriptors: ConfigurationParameterDescriptor[]; + parameterDescriptors?: ConfigurationParameterDescriptor[]; + + /** + * a JSON schema that describes the parameters that the front-end + * needs to provide with corresponding values. Use this for complex + * parameter descriptions instead of parameterDescriptors. + */ + schema?: object; } /** diff --git a/tsp-typescript-client/src/protocol/tsp-client.test.ts b/tsp-typescript-client/src/protocol/tsp-client.test.ts index 7f4cacd..2794e36 100644 --- a/tsp-typescript-client/src/protocol/tsp-client.test.ts +++ b/tsp-typescript-client/src/protocol/tsp-client.test.ts @@ -4,6 +4,7 @@ import { HttpRequest, HttpResponse, RestClient } from './rest-client'; import { FixtureSet } from './test-utils'; import { HttpTspClient } from './http-tsp-client'; import { DataType } from '../models/data-type'; +import { ConfigurationParameterDescriptor } from '../models/configuration-source'; describe('HttpTspClient Deserialization', () => { @@ -405,22 +406,38 @@ describe('HttpTspClient Deserialization', () => { const response = await client.fetchConfigurationSourceTypes(); const sourceTypes = response.getModel()!; - expect(sourceTypes).toHaveLength(1); + expect(sourceTypes).toHaveLength(2); expect(sourceTypes[0].name).toEqual('My configuration source 1'); expect(sourceTypes[0].description).toEqual('My configuration source 1 description'); expect(sourceTypes[0].id).toEqual('my-source-type-1-id'); - console.log(sourceTypes[0]); - expect(sourceTypes[0].parameterDescriptors).toHaveLength(2); - - expect(sourceTypes[0].parameterDescriptors[0].keyName).toEqual('path'); - expect(sourceTypes[0].parameterDescriptors[0].description).toEqual('path description'); - expect(sourceTypes[0].parameterDescriptors[0].dataType).toEqual('STRING'); - expect(sourceTypes[0].parameterDescriptors[0].isRequired).toBeTruthy(); - - expect(sourceTypes[0].parameterDescriptors[1].keyName).toEqual('test1'); - expect(sourceTypes[0].parameterDescriptors[1].description).toBeUndefined(); - expect(sourceTypes[0].parameterDescriptors[1].dataType).toBeUndefined(); - expect(sourceTypes[0].parameterDescriptors[1].isRequired).toBeUndefined(); + + expect(sourceTypes[0].parameterDescriptors).toBeDefined(); + const paramDesc: ConfigurationParameterDescriptor[] = sourceTypes[0].parameterDescriptors ?? []; + expect(paramDesc).toHaveLength(2); + + expect(paramDesc[0].keyName).toEqual('path'); + expect(paramDesc[0].description).toEqual('path description'); + expect(paramDesc[0].dataType).toEqual('STRING'); + expect(paramDesc[0].isRequired).toBeTruthy(); + + expect(paramDesc[1].keyName).toEqual('test1'); + expect(paramDesc[1].description).toBeUndefined(); + expect(paramDesc[1].dataType).toBeUndefined(); + expect(paramDesc[1].isRequired).toBeUndefined(); + expect(sourceTypes[0].schema).toBeUndefined(); + + expect(sourceTypes[1].parameterDescriptors).toBeUndefined(); + expect(sourceTypes[1].schema).toBeDefined(); + const schema: {} = sourceTypes[1].schema ?? {} + const schemaString: string = JSON.stringify(schema); + expect(schemaString).toBeDefined(); + + // check some scheme content + expect(schema['$schema']).toBeDefined(); + expect(schema['$schema']).toEqual('https://json-schema.org/draft/2020-12/schema') + + expect(schema['$id']).toBeDefined(); + expect(schema['$id']).toEqual('https://org.eclipse.tracecompass/custom-execution-analysis.json') }); it('configurations', async () => {