diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 698be5c..c1027e2 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -17,13 +17,13 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-20.04] + os: [ubuntu-22.04] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: - node-version: 16 + node-version: 18 - uses: microsoft/playwright-github-action@v1 - uses: actions/cache@v3 with: @@ -42,7 +42,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: - node-version: 16 + node-version: 18 - uses: microsoft/playwright-github-action@v1 - uses: actions/cache@v3 with: @@ -68,7 +68,7 @@ jobs: fetch-depth: 0 - uses: actions/setup-node@v2 with: - node-version: '16.x' + node-version: '18.x' registry-url: 'https://registry.npmjs.org' - uses: actions/cache@v3 with: diff --git a/demo/W-18830464/W-18830464.json b/demo/W-18830464/W-18830464.json new file mode 100644 index 0000000..a46d0bd --- /dev/null +++ b/demo/W-18830464/W-18830464.json @@ -0,0 +1,118 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "test-otr-eapi" + }, + "components": { + "securitySchemes": { + "basicAuth": { + "type": "http", + "scheme": "basic" + } + } + }, + "security": [ + { + "basicAuth": [] + } + ], + "paths": { + "/asset": { + "post": { + "description": "Create a span", + "parameters": [ + { + "$ref": "common/common-header.json#components/parameters/SourceHeader" + }, + { + "$ref": "common/common-header.json#components/parameters/TargetHeader" + }, + { + "$ref": "common/common-header.json#components/parameters/correlationId" + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "schema/request/test-otr-eapi-request-schema.json#/components/schemas/createAsset" + }, + "examples": { + "createAsset": { + "$ref": "examples/request/test-otr-eapi-request.json#/components/examples/createAsset" + } + } + } + } + }, + "responses": { + "200": { + "description": "Span created", + "content": { + "application/json": { + "schema": { + "$ref": "schema/response/test-otr-eapi-response-schema.json#/components/schemas/createAsset" + }, + "examples": { + "createAsset": { + "$ref": "examples/response/test-otr-eapi-response.json#/components/examples/createAsset" + } + } + } + } + } + } + } + }, + "/preferences": { + "post": { + "description": "Create a preferences", + "parameters": [ + { + "$ref": "common/common-header.json#components/parameters/SourceHeader" + }, + { + "$ref": "common/common-header.json#components/parameters/TargetHeader" + }, + { + "$ref": "common/common-header.json#components/parameters/correlationId" + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "schema/request/test-otr-eapi-request-schema.json#/components/schemas/createPreferences" + }, + "examples": { + "createPreferences": { + "$ref": "examples/request/test-otr-eapi-request.json#/components/examples/createPreferences" + } + } + } + } + }, + "responses": { + "200": { + "description": "Preferences created", + "content": { + "application/json": { + "schema": { + "$ref": "schema/response/test-otr-eapi-response-schema.json#/components/schemas/createPreferences" + }, + "examples": { + "CreateSpanRes": { + "$ref": "examples/response/test-otr-eapi-response.json#/components/examples/createPreferences" + } + } + } + } + } + } + } + } + } + } diff --git a/demo/W-18830464/common/common-header.json b/demo/W-18830464/common/common-header.json new file mode 100644 index 0000000..e5dc35d --- /dev/null +++ b/demo/W-18830464/common/common-header.json @@ -0,0 +1,40 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "common-header" + }, + "components": { + "parameters": { + "SourceHeader": { + "description": "Source-System", + "in": "header", + "name": "Source-System", + "schema": { + "type": "string" + }, + "required": true, + "example": "SAP" + }, + "TargetHeader": { + "description": "Target-System", + "in": "header", + "name": "Target-System", + "schema": { + "type": "string" + }, + "required": true, + "example": "Salesforce" + }, + "correlationId": { + "in": "header", + "name": "X-Correlation-Id", + "schema": { + "type": "string" + }, + "required": true, + "example": "1065fe6c-9121-4b05-b6e3-df5c0cb42461" + } + } + } +} \ No newline at end of file diff --git a/demo/W-18830464/examples/request/test-otr-eapi-request.json b/demo/W-18830464/examples/request/test-otr-eapi-request.json new file mode 100644 index 0000000..98df778 --- /dev/null +++ b/demo/W-18830464/examples/request/test-otr-eapi-request.json @@ -0,0 +1,45 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "test-otr-eapi-request" + }, + "components": { + "examples": { + "createAsset" : { + "value" : [ + { + "spanID": "ENGYALERT", + "name": "EMAIL1", + "description": "CCB_IDL_Provider" + + } + ] + }, + "createPreferences" : { + "value" : [ + { + "programID": "ENERGYALERT", + "channelID": "EMAIL", + "userID": "CCB_IDL_Provider", + "customProperties": [ + { + "link": 0 + } + ] + }, + { + "programID": "ENERGYALERT", + "channelID": "EMAIL", + "userID": "CCB_IDL_Provider", + "customProperties": [ + { + "link": 0 + } + ] + } + ] + } + } + } +} \ No newline at end of file diff --git a/demo/W-18830464/examples/response/test-otr-eapi-response.json b/demo/W-18830464/examples/response/test-otr-eapi-response.json new file mode 100644 index 0000000..7badd5c --- /dev/null +++ b/demo/W-18830464/examples/response/test-otr-eapi-response.json @@ -0,0 +1,24 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "test-otr-eapi-response" + }, + "components": { + "examples" : { + "createAsset" : { + "value": { + "id": "", + "msg" : "" + } + }, + "createPreferences" : { + "value": { + "id": "", + "msg" : "" + } + } + } + } + +} \ No newline at end of file diff --git a/demo/W-18830464/schema/request/test-otr-eapi-request-schema.json b/demo/W-18830464/schema/request/test-otr-eapi-request-schema.json new file mode 100644 index 0000000..965795c --- /dev/null +++ b/demo/W-18830464/schema/request/test-otr-eapi-request-schema.json @@ -0,0 +1,62 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "test-otr-eapi-request-schema" + }, + "components": { + "schemas": { + "createAsset": { + "type": "array", + "items": { + "type": "object", + "properties": { + "spanId": { + "type": "string" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + } + } + } + }, + "createPreferences": { + "type": "array", + "items": { + "type": "object", + "properties": { + "programId": { + "type": "string" + }, + "channelId": { + "type": "string" + }, + "userId": { + "type": "string" + }, + "customProperties" : { + "type" : "array", + "items": { + "type": "object", + "properties": { + "link": { + "type": "integer" + }, + "key": { + "type": "string" + }, + "value": { + "type": "string" + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/demo/W-18830464/schema/response/test-otr-eapi-response-schema.json b/demo/W-18830464/schema/response/test-otr-eapi-response-schema.json new file mode 100644 index 0000000..512a8b3 --- /dev/null +++ b/demo/W-18830464/schema/response/test-otr-eapi-response-schema.json @@ -0,0 +1,33 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0.0", + "title": "test-otr-eapi-response-schema" + }, + "components": { + "schemas" : { + "createAsset" : { + "type" : "object", + "properties": { + "id" : { + "type" : "string" + }, + "msg" : { + "type": "string" + } + } + }, + "createPreferences" : { + "type" : "object", + "properties": { + "id" : { + "type" : "string" + }, + "msg" : { + "type": "string" + } + } + } + } +} +} \ No newline at end of file diff --git a/demo/index.js b/demo/index.js index 02e49cf..81dc5ce 100644 --- a/demo/index.js +++ b/demo/index.js @@ -126,7 +126,8 @@ class ComponentDemo extends ApiDemoPage { ['W-11843862', 'W-11843862'], ['W-17309546', 'W-17309546'], ['W-17413312', 'W-17413312'], - ['v4_0_0_api_specs', 'v4_0_0_api_specs'] + ['v4_0_0_api_specs', 'v4_0_0_api_specs'], + ['W-18830464', 'W-18830464'] ].map( ([file, label]) => html` { - const shapeFragment = this._computeReferences(item) - const externalFragments = this._computeReferences(shapeFragment) + const encodesOfExternalFragments = rootReferences.flat().map((item) => { + const shapeFragment = this._computeReferences(item); + const externalFragments = this._computeReferences(shapeFragment[0]); // Get second element from externalFragments - const externalFragmentExample = externalFragments[1] - return this._computeEncodes(externalFragmentExample) - }) + const externalFragmentExample = externalFragments[1]; + return this._computeEncodes(externalFragmentExample); + }); // It finds an element in encodesOfExternalFragments where // the @id property matches referenceId and assigns @@ -733,13 +733,13 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { if (hasRaw) { if (isJson) { try { - const res = JSON.parse(raw); + const res = JSON.parse(String(raw)); const type = typeof res; if (type === 'string' || type === 'number' || type === 'boolean') { throw new Error(''); } result.hasRaw = false; - result.value = raw; + result.value = JSON.stringify(res, null, 2); result.isScalar = false; return result; } catch (_) { @@ -747,15 +747,16 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { } } if (isXml) { - if (raw.trim()[0] === '<') { + const rawValue = String(raw); + if (rawValue.trim()[0] === '<') { result.hasRaw = false; - result.value = raw; + result.value = rawValue; result.isScalar = false; return result; } } result.hasRaw = true; - result.raw = raw; + result.raw = String(raw); } const sKey = this._getAmfKey( this.ns.aml.vocabularies.document.structuredValue @@ -794,11 +795,13 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { let data; if (isJson) { data = this._jsonFromStructure(member); - } else if (isXml) { - data = this._xmlFromStructure(member, { ...opts, ignoreXmlHeader: true }); + data = this._xmlFromStructure(member, { + ...opts, + ignoreXmlHeader: true, + }); } - if (data) { + if (data !== undefined) { parts.push(data); } }); @@ -807,7 +810,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { // if the parse process fails then use parts to build example value if (result.raw) { try { - result.value = this.computeRaw(raw) + result.value = this.computeRaw(raw); return result } catch (_) { // ... @@ -881,7 +884,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { * @param {String} raw * @returns string JSON formatted */ - computeRaw(raw) { + computeRawOld(raw) { const accountEntries = raw.split('-\n'); const parsed = this.parseToJSON(accountEntries) // Ensure the parsed result is always an array @@ -890,6 +893,49 @@ export class ExampleGenerator extends AmfHelperMixin(Object) { } + computeRaw(raw) { + if (typeof raw !== 'string') { + return JSON.stringify(raw); + } + // A very basic check for a simple YAML list. + const trimmed = raw.trim(); + if (trimmed.startsWith('-') && trimmed.indexOf(':') === -1) { + const items = trimmed + .split('\n') + .map((v) => v.trim().replace(/^-/, '').trim()); + const values = items.map((v) => { + const n = Number(v); + return isNaN(n) ? v : n; + }); + return JSON.stringify(values, null, 2); + } + + // Split the string into blocks separated by hyphens + const blocks = raw.split('-\n').filter((block) => block.trim() !== ''); + + // Process each block to convert it into an object + const sanitized = blocks.map((block) => { + const lines = block.split('\n').filter((line) => line.trim() !== ''); + const obj = {}; + + lines.forEach((line) => { + const i = line.indexOf(':'); + const key = line.slice(0, i).trim().replace(/^"|"$/g, ''); + const value = line.slice(i + 1).trim(); + if (value.startsWith('"') && value.endsWith('"')) { + obj[key] = value.slice(1, -1); + } else { + const numericValue = Number(value); + obj[key] = isNaN(numericValue) ? value : numericValue; + } + }); + + return obj; + }); + + // Convert to clean JSON string + return JSON.stringify(sanitized, null, 2); + } /** * Computes list of examples for an array shape. * @param {Object} schema The AMF's array shape diff --git a/test/W-18830464.test.js b/test/W-18830464.test.js new file mode 100644 index 0000000..4cc6180 --- /dev/null +++ b/test/W-18830464.test.js @@ -0,0 +1,63 @@ +import { fixture, assert, html } from '@open-wc/testing'; +import { AmfLoader } from './amf-loader.js'; +import '../api-example-generator.js'; + +describe('W-18830464', () => { + async function basicFixture(amf) { + return (await fixture(html``)); + } + + const apiFile = 'W-18830464'; + + [ + ['json+ld data model', false], + ['Compact data model', true] + ].forEach(([label, compact]) => { + describe(label, () => { + let element; + let amf; + + before(async () => { + amf = await AmfLoader.load(compact, apiFile); + }); + + beforeEach(async () => { + element = await basicFixture(amf); + }); + + it('renders examples right', () => { + const payloads = AmfLoader.lookupPayload( + amf, + '/preferences', + 'post' + ); + const result = element.generatePayloadsExamples( + payloads, + 'application/json' + ); + const item = result[0]; + assert.equal(item.value, `[ + { + "programID": "ENERGYALERT", + "channelID": "EMAIL", + "userID": "CCB_IDL_Provider", + "customProperties": 0 + }, + { + "link": 0 + }, + { + "programID": "ENERGYALERT", + "channelID": "EMAIL", + "userID": "CCB_IDL_Provider", + "customProperties": 0 + }, + { + "link": 0 + } +]`); + }); + }); + }); +});