Skip to content

Commit

Permalink
fix: internal schema apprears as object on logout (#2705)
Browse files Browse the repository at this point in the history
* fix: internal schema apprears as object

* bump elements versions

* reverted elements version

* Added unit test case

* elements versions updated

* Added test cases to show error if all object has error

* Added comment for test case and test description updated

* Formated the testcases

---------

Co-authored-by: Nauman <[email protected]>
  • Loading branch information
SB-harshitajadhav and mnaumanali94 authored Oct 10, 2024
1 parent dbe06b5 commit 360935f
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/elements-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-core",
"version": "8.4.5",
"version": "8.4.6",
"sideEffects": [
"web-components.min.js",
"src/web-components/**",
Expand Down
176 changes: 176 additions & 0 deletions packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
25 changes: 24 additions & 1 deletion packages/elements-core/src/utils/ref-resolving/resolvedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,30 @@ export const isResolvedObjectProxy = (someObject: object): boolean => {
};

export const getOriginalObject = (resolvedObject: object): object => {
return (resolvedObject as Record<symbol, object>)[originalObjectSymbol] || resolvedObject;
const originalObject: any = (resolvedObject as Record<symbol, object>)[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;
4 changes: 2 additions & 2 deletions packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 360935f

Please sign in to comment.