diff --git a/packages/elements-core/package.json b/packages/elements-core/package.json index 27dff13a4..3648d9328 100644 --- a/packages/elements-core/package.json +++ b/packages/elements-core/package.json @@ -1,6 +1,6 @@ { "name": "@stoplight/elements-core", - "version": "8.4.5", + "version": "8.4.6", "sideEffects": [ "web-components.min.js", "src/web-components/**", diff --git a/packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts b/packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts index 8144b0454..5d075c0ca 100644 --- a/packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts +++ b/packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts @@ -236,4 +236,180 @@ describe('createResolvedObject', () => { expect(resolvedObject).toEqual(originalObject); }); + + // If the schema is internal, the object contains 'x-sl-error-message' on logout. + it('removes object if contains an error for oneOf', () => { + const originalObject = { + oneOf: [ + { $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { 'error-message': 'You do not have permission to view this reference', id: 'nezai0hyj4yak' }, + }, + { $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } }, + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + + const filteredObject = { + oneOf: [ + { $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } }, + { $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } }, + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + const resolvedObject = getOriginalObject(originalObject); + + expect(resolvedObject).toEqual(filteredObject); + }); + + it('removes object if contains an error for anyeOf', () => { + const originalObject = { + anyOf: [ + { $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { 'error-message': 'You do not have permission to view this reference', id: 'nezai0hyj4yak' }, + }, + { $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } }, + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + + const filteredObject = { + anyOf: [ + { $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } }, + { $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } }, + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + const resolvedObject = getOriginalObject(originalObject); + + expect(resolvedObject).toEqual(filteredObject); + }); + + it('show the error if all schemas are internal for anyOf', () => { + const originalObject = { + anyOf: [ + [ + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + ], + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + + const filteredObject = { + anyOf: [ + [ + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + ], + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + const resolvedObject = getOriginalObject(originalObject); + + expect(resolvedObject).toEqual(filteredObject); + }); + it('show the error if all schemas are internal for oneOf', () => { + const originalObject = { + oneOf: [ + [ + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + ], + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + + const filteredObject = { + oneOf: [ + [ + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + { + 'x-sl-error-message': 'You do not have permission to view this reference', + 'x-stoplight': { + 'error-message': 'You do not have permission to view this reference', + id: 'nezai0hyj4yak', + }, + }, + ], + ], + 'x-stoplight': { id: 'b73ff5df9864f' }, + }; + const resolvedObject = getOriginalObject(originalObject); + + expect(resolvedObject).toEqual(filteredObject); + }); }); diff --git a/packages/elements-core/src/utils/ref-resolving/resolvedObject.ts b/packages/elements-core/src/utils/ref-resolving/resolvedObject.ts index cb2406eb3..5cc071667 100644 --- a/packages/elements-core/src/utils/ref-resolving/resolvedObject.ts +++ b/packages/elements-core/src/utils/ref-resolving/resolvedObject.ts @@ -78,7 +78,30 @@ export const isResolvedObjectProxy = (someObject: object): boolean => { }; export const getOriginalObject = (resolvedObject: object): object => { - return (resolvedObject as Record)[originalObjectSymbol] || resolvedObject; + const originalObject: any = (resolvedObject as Record)[originalObjectSymbol] || resolvedObject; + if (!originalObject) { + return resolvedObject; + } + + const hasAllSchemaErrors = (array: any[]) => { + return array.every(item => item['x-sl-error-message'] !== undefined); + }; + + if (originalObject.anyOf) { + if (hasAllSchemaErrors(originalObject.anyOf)) { + return { ...originalObject, anyOf: [originalObject.anyOf] }; + } + const filteredArray = originalObject.anyOf.filter((item: { [x: string]: any }) => !item['x-sl-error-message']); + return { ...originalObject, anyOf: filteredArray }; + } else if (originalObject.oneOf) { + if (hasAllSchemaErrors(originalObject.oneOf)) { + return { ...originalObject, oneOf: [originalObject.oneOf] }; + } + const filteredArray = originalObject.oneOf.filter((item: { [x: string]: any }) => !item['x-sl-error-message']); + return { ...originalObject, oneOf: filteredArray }; + } + + return originalObject; }; export const isReference = hasRef; diff --git a/packages/elements-dev-portal/package.json b/packages/elements-dev-portal/package.json index 49372e5d7..1d10577b9 100644 --- a/packages/elements-dev-portal/package.json +++ b/packages/elements-dev-portal/package.json @@ -1,6 +1,6 @@ { "name": "@stoplight/elements-dev-portal", - "version": "2.4.6", + "version": "2.4.7", "description": "UI components for composing beautiful developer documentation.", "keywords": [], "sideEffects": [ @@ -66,7 +66,7 @@ "dependencies": { "@stoplight/markdown-viewer": "^5.7.1", "@stoplight/mosaic": "^1.53.4", - "@stoplight/elements-core": "^8.4.5", + "@stoplight/elements-core": "^8.4.6", "@stoplight/path": "^1.3.2", "@stoplight/types": "^14.0.0", "classnames": "^2.2.6", diff --git a/packages/elements/package.json b/packages/elements/package.json index d7a3439db..3a45c853e 100644 --- a/packages/elements/package.json +++ b/packages/elements/package.json @@ -1,6 +1,6 @@ { "name": "@stoplight/elements", - "version": "8.4.5", + "version": "8.4.6", "description": "UI components for composing beautiful developer documentation.", "keywords": [], "sideEffects": [ @@ -63,7 +63,7 @@ ] }, "dependencies": { - "@stoplight/elements-core": "^8.4.5", + "@stoplight/elements-core": "^8.4.6", "@stoplight/http-spec": "^7.1.0", "@stoplight/json": "^3.18.1", "@stoplight/mosaic": "^1.53.4",