Skip to content

Commit 360935f

Browse files
fix: internal schema apprears as object on logout (#2705)
* 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]>
1 parent dbe06b5 commit 360935f

File tree

5 files changed

+205
-6
lines changed

5 files changed

+205
-6
lines changed

packages/elements-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements-core",
3-
"version": "8.4.5",
3+
"version": "8.4.6",
44
"sideEffects": [
55
"web-components.min.js",
66
"src/web-components/**",

packages/elements-core/src/utils/ref-resolving/resolvedObject.test.ts

+176
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,180 @@ describe('createResolvedObject', () => {
236236

237237
expect(resolvedObject).toEqual(originalObject);
238238
});
239+
240+
// If the schema is internal, the object contains 'x-sl-error-message' on logout.
241+
it('removes object if contains an error for oneOf', () => {
242+
const originalObject = {
243+
oneOf: [
244+
{ $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } },
245+
{
246+
'x-sl-error-message': 'You do not have permission to view this reference',
247+
'x-stoplight': { 'error-message': 'You do not have permission to view this reference', id: 'nezai0hyj4yak' },
248+
},
249+
{ $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } },
250+
],
251+
'x-stoplight': { id: 'b73ff5df9864f' },
252+
};
253+
254+
const filteredObject = {
255+
oneOf: [
256+
{ $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } },
257+
{ $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } },
258+
],
259+
'x-stoplight': { id: 'b73ff5df9864f' },
260+
};
261+
const resolvedObject = getOriginalObject(originalObject);
262+
263+
expect(resolvedObject).toEqual(filteredObject);
264+
});
265+
266+
it('removes object if contains an error for anyeOf', () => {
267+
const originalObject = {
268+
anyOf: [
269+
{ $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } },
270+
{
271+
'x-sl-error-message': 'You do not have permission to view this reference',
272+
'x-stoplight': { 'error-message': 'You do not have permission to view this reference', id: 'nezai0hyj4yak' },
273+
},
274+
{ $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } },
275+
],
276+
'x-stoplight': { id: 'b73ff5df9864f' },
277+
};
278+
279+
const filteredObject = {
280+
anyOf: [
281+
{ $ref: '#/__bundled__/0mui9s02880hl', 'x-stoplight': { id: '19c178fc05d4a' } },
282+
{ $ref: '#/__bundled__/iq2mwk8jvthd2', 'x-stoplight': { id: 'ovj32wmpxpg7p' } },
283+
],
284+
'x-stoplight': { id: 'b73ff5df9864f' },
285+
};
286+
const resolvedObject = getOriginalObject(originalObject);
287+
288+
expect(resolvedObject).toEqual(filteredObject);
289+
});
290+
291+
it('show the error if all schemas are internal for anyOf', () => {
292+
const originalObject = {
293+
anyOf: [
294+
[
295+
{
296+
'x-sl-error-message': 'You do not have permission to view this reference',
297+
'x-stoplight': {
298+
'error-message': 'You do not have permission to view this reference',
299+
id: 'nezai0hyj4yak',
300+
},
301+
},
302+
{
303+
'x-sl-error-message': 'You do not have permission to view this reference',
304+
'x-stoplight': {
305+
'error-message': 'You do not have permission to view this reference',
306+
id: 'nezai0hyj4yak',
307+
},
308+
},
309+
{
310+
'x-sl-error-message': 'You do not have permission to view this reference',
311+
'x-stoplight': {
312+
'error-message': 'You do not have permission to view this reference',
313+
id: 'nezai0hyj4yak',
314+
},
315+
},
316+
],
317+
],
318+
'x-stoplight': { id: 'b73ff5df9864f' },
319+
};
320+
321+
const filteredObject = {
322+
anyOf: [
323+
[
324+
{
325+
'x-sl-error-message': 'You do not have permission to view this reference',
326+
'x-stoplight': {
327+
'error-message': 'You do not have permission to view this reference',
328+
id: 'nezai0hyj4yak',
329+
},
330+
},
331+
{
332+
'x-sl-error-message': 'You do not have permission to view this reference',
333+
'x-stoplight': {
334+
'error-message': 'You do not have permission to view this reference',
335+
id: 'nezai0hyj4yak',
336+
},
337+
},
338+
{
339+
'x-sl-error-message': 'You do not have permission to view this reference',
340+
'x-stoplight': {
341+
'error-message': 'You do not have permission to view this reference',
342+
id: 'nezai0hyj4yak',
343+
},
344+
},
345+
],
346+
],
347+
'x-stoplight': { id: 'b73ff5df9864f' },
348+
};
349+
const resolvedObject = getOriginalObject(originalObject);
350+
351+
expect(resolvedObject).toEqual(filteredObject);
352+
});
353+
it('show the error if all schemas are internal for oneOf', () => {
354+
const originalObject = {
355+
oneOf: [
356+
[
357+
{
358+
'x-sl-error-message': 'You do not have permission to view this reference',
359+
'x-stoplight': {
360+
'error-message': 'You do not have permission to view this reference',
361+
id: 'nezai0hyj4yak',
362+
},
363+
},
364+
{
365+
'x-sl-error-message': 'You do not have permission to view this reference',
366+
'x-stoplight': {
367+
'error-message': 'You do not have permission to view this reference',
368+
id: 'nezai0hyj4yak',
369+
},
370+
},
371+
{
372+
'x-sl-error-message': 'You do not have permission to view this reference',
373+
'x-stoplight': {
374+
'error-message': 'You do not have permission to view this reference',
375+
id: 'nezai0hyj4yak',
376+
},
377+
},
378+
],
379+
],
380+
'x-stoplight': { id: 'b73ff5df9864f' },
381+
};
382+
383+
const filteredObject = {
384+
oneOf: [
385+
[
386+
{
387+
'x-sl-error-message': 'You do not have permission to view this reference',
388+
'x-stoplight': {
389+
'error-message': 'You do not have permission to view this reference',
390+
id: 'nezai0hyj4yak',
391+
},
392+
},
393+
{
394+
'x-sl-error-message': 'You do not have permission to view this reference',
395+
'x-stoplight': {
396+
'error-message': 'You do not have permission to view this reference',
397+
id: 'nezai0hyj4yak',
398+
},
399+
},
400+
{
401+
'x-sl-error-message': 'You do not have permission to view this reference',
402+
'x-stoplight': {
403+
'error-message': 'You do not have permission to view this reference',
404+
id: 'nezai0hyj4yak',
405+
},
406+
},
407+
],
408+
],
409+
'x-stoplight': { id: 'b73ff5df9864f' },
410+
};
411+
const resolvedObject = getOriginalObject(originalObject);
412+
413+
expect(resolvedObject).toEqual(filteredObject);
414+
});
239415
});

packages/elements-core/src/utils/ref-resolving/resolvedObject.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,30 @@ export const isResolvedObjectProxy = (someObject: object): boolean => {
7878
};
7979

8080
export const getOriginalObject = (resolvedObject: object): object => {
81-
return (resolvedObject as Record<symbol, object>)[originalObjectSymbol] || resolvedObject;
81+
const originalObject: any = (resolvedObject as Record<symbol, object>)[originalObjectSymbol] || resolvedObject;
82+
if (!originalObject) {
83+
return resolvedObject;
84+
}
85+
86+
const hasAllSchemaErrors = (array: any[]) => {
87+
return array.every(item => item['x-sl-error-message'] !== undefined);
88+
};
89+
90+
if (originalObject.anyOf) {
91+
if (hasAllSchemaErrors(originalObject.anyOf)) {
92+
return { ...originalObject, anyOf: [originalObject.anyOf] };
93+
}
94+
const filteredArray = originalObject.anyOf.filter((item: { [x: string]: any }) => !item['x-sl-error-message']);
95+
return { ...originalObject, anyOf: filteredArray };
96+
} else if (originalObject.oneOf) {
97+
if (hasAllSchemaErrors(originalObject.oneOf)) {
98+
return { ...originalObject, oneOf: [originalObject.oneOf] };
99+
}
100+
const filteredArray = originalObject.oneOf.filter((item: { [x: string]: any }) => !item['x-sl-error-message']);
101+
return { ...originalObject, oneOf: filteredArray };
102+
}
103+
104+
return originalObject;
82105
};
83106

84107
export const isReference = hasRef;

packages/elements-dev-portal/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements-dev-portal",
3-
"version": "2.4.6",
3+
"version": "2.4.7",
44
"description": "UI components for composing beautiful developer documentation.",
55
"keywords": [],
66
"sideEffects": [
@@ -66,7 +66,7 @@
6666
"dependencies": {
6767
"@stoplight/markdown-viewer": "^5.7.1",
6868
"@stoplight/mosaic": "^1.53.4",
69-
"@stoplight/elements-core": "^8.4.5",
69+
"@stoplight/elements-core": "^8.4.6",
7070
"@stoplight/path": "^1.3.2",
7171
"@stoplight/types": "^14.0.0",
7272
"classnames": "^2.2.6",

packages/elements/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stoplight/elements",
3-
"version": "8.4.5",
3+
"version": "8.4.6",
44
"description": "UI components for composing beautiful developer documentation.",
55
"keywords": [],
66
"sideEffects": [
@@ -63,7 +63,7 @@
6363
]
6464
},
6565
"dependencies": {
66-
"@stoplight/elements-core": "^8.4.5",
66+
"@stoplight/elements-core": "^8.4.6",
6767
"@stoplight/http-spec": "^7.1.0",
6868
"@stoplight/json": "^3.18.1",
6969
"@stoplight/mosaic": "^1.53.4",

0 commit comments

Comments
 (0)