Skip to content

Commit 3309a70

Browse files
authored
fix(apidom): fix refract structures hydration (#227)
- fix security problem in axios https://npmjs.com/advisories/1594 Closes #226
1 parent d5ecbfc commit 3309a70

File tree

15 files changed

+61
-33
lines changed

15 files changed

+61
-33
lines changed

apidom/package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apidom/packages/@types/minim.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ declare module 'minim' {
4343

4444
toRefract(element: Element): JSON;
4545

46-
fromRefract(doc: JSON): Element;
46+
toElement(value: any): Element;
47+
48+
fromRefract(doc: Record<string, any> | Array<any>): Element;
4749

4850
register(name: string, elementClass: any): Namespace;
4951

apidom/packages/apidom-ns-asyncapi-2-0/src/elements/AsyncApi2-0.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ChannelsElement from './Channels';
88
class AsyncApi2_0 extends ObjectElement {
99
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
1010
super(content, meta, attributes);
11-
this.element = 'asyncApi2-0';
11+
this.element = 'asyncApi2_0';
1212
this.classes.push('api');
1313
}
1414

apidom/packages/apidom-ns-openapi-3-1/src/elements/OpenApi3-1.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import ComponentsElement from './Components';
77
class OpenApi3_1 extends ObjectElement {
88
constructor(content?: Record<string, unknown>, meta?: Meta, attributes?: Attributes) {
99
super(content, meta, attributes);
10-
this.element = 'openApi3-1';
10+
this.element = 'openApi3_1';
1111
this.classes.push('api');
1212
}
1313

apidom/packages/apidom-parser-adapter-asyncapi-json-2-0/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ describe('apidom-parser-adapter-asyncapi-json-2-0', function () {
1414

1515
const parseResult = await adapter.parse(spec, { sourceMap: true });
1616
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
17-
// console.log (JSON.stringify(apiDOM.toJSON(namespace, parseResult), null, null));
17+
// console.log (JSON.stringify(apiDOM.dehydrate(parseResult, namespace), null, null));
1818
});
1919
});

apidom/packages/apidom-parser-adapter-asyncapi-yaml-2-0/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('apidom-parser-adapter-asyncapi-yaml-2-0', function () {
1313

1414
const parseResult = await adapter.parse(spec, { sourceMap: true });
1515
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
16-
// console.log(JSON.stringify(apiDOM.toJSON(adapter.namespace, parseResult), null, null));
16+
// console.log(JSON.stringify(apiDOM.dehydrate(parseResult, adapter.namespace), null, null));
1717
});
1818
});

apidom/packages/apidom-parser-adapter-json/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('apidom-parser-adapter-json', function () {
1313

1414
const parseResult = await adapter.parse(spec, { sourceMap: true });
1515
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
16-
// console.log(JSON.stringify(apiDOM.toJSON(adapter.namespace, parseResult), null, null));
16+
// console.log(JSON.stringify(apiDOM.toJSON(parseResult, adapter.namespace), null, null));
1717
});
1818
});

apidom/packages/apidom-parser-adapter-openapi-json-3-1/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ describe('apidom-parser-adapter-openapi-json-3-1', function () {
1414

1515
const parseResult = await adapter.parse(spec, { sourceMap: true });
1616
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
17-
// console.log (JSON.stringify(apiDOM.toJSON(namespace, parseResult), null, null));
17+
// console.log (JSON.stringify(apiDOM.dehydrate(parseResult, namespace), null, null));
1818
});
1919
});

apidom/packages/apidom-parser-adapter-openapi-yaml-3-1/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('apidom-parser-adapter-openapi-yaml-3-1', function () {
1313

1414
const parseResult = await adapter.parse(spec, { sourceMap: true });
1515
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
16-
// console.log(JSON.stringify(apiDOM.toJSON(adapter.namespace, parseResult), null, null));
16+
// console.log(JSON.stringify(apiDOM.toJSON(parseResult, adapter.namespace), null, null));
1717
});
1818
});

apidom/packages/apidom-parser-adapter-yaml-1-2/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ describe('apidom-parser-adapter-yaml-1-2', function () {
1313

1414
const parseResult = await adapter.parse(spec, { sourceMap: true });
1515
console.log(JSON.stringify(apiDOM.toValue(parseResult), null, 2));
16-
// console.log(JSON.stringify(apiDOM.toJSON(adapter.namespace, parseResult), null, null));
16+
// console.log(JSON.stringify(apiDOM.dehydrate(parseResult, adapter.namespace), null, null));
1717
});
1818
});

apidom/packages/apidom-parser/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ describe('apidom-parser', function () {
1616
it('test', async function () {
1717
const parseResult = await parser.parse(spec);
1818
console.log(parseResult);
19-
console.log(apiDOM.toJSON(namespace, parseResult));
19+
console.log(apiDOM.dehydrate(parseResult, namespace));
2020
});
2121
});

apidom/packages/apidom-reference/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"license": "Apache-2.0",
2929
"dependencies": {
3030
"@babel/runtime-corejs3": "=7.12.5",
31-
"axios": "=0.21.0",
31+
"axios": "=0.21.1",
3232
"ramda": "=0.27.0",
3333
"@types/ramda": "=0.27.6",
3434
"ramda-adjunct": "=2.27.0",

apidom/packages/apidom/src/index.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { NamespacePlugin, Element } from 'minim';
2-
import { isPlainObject } from 'ramda-adjunct';
1+
import { has } from 'ramda';
2+
import { isPlainObject, isString } from 'ramda-adjunct';
3+
import { NamespacePlugin, Element, Namespace as INamespace } from 'minim';
34

45
import { Namespace as ApiDOMNamespace } from './namespace';
56

@@ -56,19 +57,44 @@ export const createNamespace = (namespacePlugin?: NamespacePlugin): ApiDOMNamesp
5657
return namespace;
5758
};
5859

59-
export const toJSON = (namespace: ApiDOMNamespace, element: Element): JSON =>
60-
namespace.toRefract(element);
61-
62-
export const toJSONString = (namespace: ApiDOMNamespace, element: Element): string =>
63-
JSON.stringify(toJSON(namespace, element));
60+
/**
61+
* Transforms data to an Element from a particular namespace.
62+
*/
63+
export const from = (data: any, namespace: INamespace): Element => {
64+
if (isString(data)) {
65+
// JSON serialized refract
66+
return namespace.fromRefract(JSON.parse(data));
67+
}
68+
if (isPlainObject(data) && has('element', data)) {
69+
// refract javascript structure
70+
return namespace.fromRefract(data);
71+
}
72+
if (isPlainObject(data)) {
73+
// javascript POJO
74+
return namespace.toElement(data);
75+
}
76+
throw new Error('Data was not recognized');
77+
};
6478

65-
export const fromJSON = (namespace: ApiDOMNamespace, json: JSON): Element =>
66-
namespace.fromRefract(json);
79+
/**
80+
* Reconstructs the ApiDOM into JavaScript POJO.
81+
* This POJO would be the result of parsing the original
82+
* JSON string with JSON.parse function.
83+
*/
84+
export const toValue = (element: Element): any => element.toValue();
6785

68-
export const fromJSONString = (namespace: ApiDOMNamespace, jsonString: string): Element =>
69-
fromJSON(namespace, JSON.parse(jsonString));
86+
/**
87+
* Create a refract representation of Element.
88+
* https://github.com/refractproject/refract-spec
89+
*/
90+
export const dehydrate = (element: Element, namespace: INamespace): Record<string, any> => {
91+
return namespace.toRefract(element);
92+
};
7093

71-
// Reconstructs the ApiDOM into JavaScript POJO.
72-
// This POJO would be the result of parsing the original
73-
// JSON string with JSON.parse function.
74-
export const toValue = (element: Element): any => element.toValue();
94+
/**
95+
* Create a string representation of Element.
96+
*/
97+
export const toString = (element: Element, namespace: INamespace): string => {
98+
const refract = dehydrate(element, namespace);
99+
return JSON.stringify(refract);
100+
};

apidom/packages/apidom/src/namespace.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import minim from 'minim';
1+
import { Namespace as MinimNamespace } from 'minim';
22

33
import ParseResult from './elements/ParseResult';
44
import Annotation from './elements/Annotation';
55
import SourceMap from './elements/SourceMap';
66
import Comment from './elements/Comment';
77

8-
export class Namespace extends minim.Namespace {
8+
export class Namespace extends MinimNamespace {
99
constructor() {
1010
super();
1111

apidom/packages/apidom/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ describe('apidom', function () {
1515
it('test', async function () {
1616
const parseResult = await parser.parse(spec);
1717

18-
console.dir(apiDOM.toJSON(namespace, parseResult));
18+
console.dir(apiDOM.dehydrate(parseResult, namespace));
1919
});
2020
});

0 commit comments

Comments
 (0)