Skip to content

Commit 00dbd1a

Browse files
[+] embedded models
1 parent c9e1dd5 commit 00dbd1a

21 files changed

+7854
-28
lines changed

dist/index.js

Lines changed: 7557 additions & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"validate": "tsc --project tsconfig.json --noEmit",
5050
"run": "node ./test/index.js",
5151
"test": "jest --selectProjects UNIT",
52+
"test:index": "jest test/index.spec.ts",
5253
"test:update": "jest --selectProjects UNIT --updateSnapshot",
5354
"test:watch": "jest --selectProjects UNIT --watch",
5455
"test:coverage": "jest --selectProjects UNIT --coverage",

src/client/interfaces/Model.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import type { Schema } from './Schema';
33

44
export interface Model extends Schema {
55
name: string;
6+
isEmbedded?: boolean,
7+
isNodeModule?: boolean,
8+
filePath?: string,
69
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of';
710
type: string;
811
base: string;
@@ -11,7 +14,7 @@ export interface Model extends Schema {
1114
description: string | null;
1215
deprecated?: boolean;
1316
default?: string;
14-
imports: string[];
17+
imports: Array<{typeName: string, path: string, nodeModule?: boolean}|string>;
1518
enum: Enum[];
1619
enums: Model[];
1720
properties: Model[];

src/client/interfaces/ModelComposition.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Model } from './Model';
22

33
export interface ModelComposition {
44
type: 'one-of' | 'any-of' | 'all-of';
5-
imports: string[];
5+
imports: Array<{typeName: string, path: string}|string>;
66
enums: Model[];
77
properties: Model[];
88
}

src/client/interfaces/OperationParameters.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { OperationParameter } from './OperationParameter';
22

33
export interface OperationParameters {
4-
imports: string[];
4+
imports: Array<{typeName: string, path: string}|string>;
55
parameters: OperationParameter[];
66
parametersPath: OperationParameter[];
77
parametersQuery: OperationParameter[];

src/client/interfaces/Service.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import type { Operation } from './Operation';
33
export interface Service {
44
name: string;
55
operations: Operation[];
6-
imports: string[];
6+
imports: Array<{typeName: string, path: string}|string>;
77
}

src/client/interfaces/Type.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export interface Type {
22
type: string;
33
base: string;
44
template: string | null;
5-
imports: string[];
5+
imports: Array<{typeName: string, path: string, nodeModule?: boolean}|string>;
66
isNullable: boolean;
77
}

src/openApi/v3/parser/getModels.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,49 @@ import type { OpenApi } from '../interfaces/OpenApi';
33
import { getModel } from './getModel';
44
import { getType } from './getType';
55

6+
7+
function isObject(value: any): value is Record<string,any>{
8+
return value !== null && typeof value === 'object'
9+
}
10+
11+
12+
613
export const getModels = (openApi: OpenApi): Model[] => {
14+
if (!openApi.components) return [];
15+
716
const models: Model[] = [];
8-
if (openApi.components) {
9-
for (const definitionName in openApi.components.schemas) {
10-
if (openApi.components.schemas.hasOwnProperty(definitionName)) {
11-
const definition = openApi.components.schemas[definitionName];
12-
const definitionType = getType(definitionName);
13-
const model = getModel(openApi, definition, true, definitionType.base);
14-
models.push(model);
17+
function collectModel(definition: any, definitionName: string){
18+
if (definition.type){
19+
const definitionType = getType(definitionName);
20+
const model = getModel(openApi, definition, true, definitionType.base);
21+
if (isObject(definitionType.imports[0])){
22+
model.filePath = definitionType.imports[0]?.path;
1523
}
24+
model.isEmbedded = definitionName.includes('embedded') ||
25+
definition.type === 'node_module';
26+
model.isNodeModule = definition.type === 'node_module';
27+
models.push(model);
28+
return
29+
}
30+
for(const key in definition){
31+
if (!isObject(definition[key])){
32+
continue
33+
}
34+
collectModel(definition[key], definitionName + '/' + key)
1635
}
1736
}
37+
38+
39+
for (const definitionName in openApi.components.schemas) {
40+
if (!openApi.components.schemas.hasOwnProperty(definitionName)) continue;
41+
collectModel(openApi.components.schemas[definitionName], definitionName);
42+
/*
43+
const definition = openApi.components.schemas[definitionName];
44+
const definitionType = getType(definitionName);
45+
const model = getModel(openApi, definition, true, definitionType.base);
46+
models.push(model);
47+
*/
48+
}
49+
1850
return models;
1951
};

src/openApi/v3/parser/getType.spec.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ describe('getType', () => {
3333
expect(type.type).toEqual('Link<string>');
3434
expect(type.base).toEqual('Link');
3535
expect(type.template).toEqual('string');
36-
expect(type.imports).toEqual(['Link']);
36+
expect(type.imports).toEqual([{
37+
nodeModule: false,
38+
path: 'Link',
39+
typeName: 'Link'
40+
}]);
3741
expect(type.isNullable).toEqual(false);
3842
});
3943

@@ -42,7 +46,15 @@ describe('getType', () => {
4246
expect(type.type).toEqual('Link<Model>');
4347
expect(type.base).toEqual('Link');
4448
expect(type.template).toEqual('Model');
45-
expect(type.imports).toEqual(['Link', 'Model']);
49+
expect(type.imports).toEqual([{
50+
nodeModule: false,
51+
path: 'Link',
52+
typeName: 'Link'
53+
},{
54+
nodeModule: false,
55+
path: 'Model',
56+
typeName: 'Model'
57+
}]);
4658
expect(type.isNullable).toEqual(false);
4759
});
4860

@@ -51,7 +63,15 @@ describe('getType', () => {
5163
expect(type.type).toEqual('Link<Link>');
5264
expect(type.base).toEqual('Link');
5365
expect(type.template).toEqual('Link');
54-
expect(type.imports).toEqual(['Link', 'Link']);
66+
expect(type.imports).toEqual([{
67+
nodeModule: false,
68+
path: 'Link',
69+
typeName: 'Link'
70+
},{
71+
nodeModule: false,
72+
path: 'Link',
73+
typeName: 'Link'
74+
}]);
5575
expect(type.isNullable).toEqual(false);
5676
});
5777

@@ -60,7 +80,11 @@ describe('getType', () => {
6080
expect(type.type).toEqual('model_000');
6181
expect(type.base).toEqual('model_000');
6282
expect(type.template).toEqual(null);
63-
expect(type.imports).toEqual(['model_000']);
83+
expect(type.imports).toEqual([{
84+
nodeModule: false,
85+
path: 'model.000',
86+
typeName: 'model_000'
87+
}]);
6488
expect(type.isNullable).toEqual(false);
6589
});
6690

@@ -69,7 +93,11 @@ describe('getType', () => {
6993
expect(type.type).toEqual('some_special_schema');
7094
expect(type.base).toEqual('some_special_schema');
7195
expect(type.template).toEqual(null);
72-
expect(type.imports).toEqual(['some_special_schema']);
96+
expect(type.imports).toEqual([{
97+
nodeModule: false,
98+
path: 'some_special-schema',
99+
typeName: 'some_special_schema'
100+
}]);
73101
expect(type.isNullable).toEqual(false);
74102
});
75103

@@ -78,7 +106,11 @@ describe('getType', () => {
78106
expect(type.type).toEqual('$some_special_schema');
79107
expect(type.base).toEqual('$some_special_schema');
80108
expect(type.template).toEqual(null);
81-
expect(type.imports).toEqual(['$some_special_schema']);
109+
expect(type.imports).toEqual([{
110+
nodeModule: false,
111+
path: '$some+special+schema',
112+
typeName: '$some_special_schema'
113+
}]);
82114
expect(type.isNullable).toEqual(false);
83115
});
84116

src/openApi/v3/parser/getType.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,26 @@ export const getType = (type: string | string[] = 'any', format?: string): Type
7171
}
7272

7373
if (typeWithoutNamespace) {
74+
let path = typeWithoutNamespace.split('/');
75+
const type = encode(path[path.length-1]);
76+
const isNodeModule = path[0] === 'node_modules';
77+
if (isNodeModule){
78+
path.splice(0,1);
79+
path.splice(path.length-1, 1);
80+
}
81+
result.type = type;
82+
result.base = type;
83+
result.imports.push({
84+
typeName: type,
85+
path: path.join('/'),
86+
nodeModule: isNodeModule
87+
});
88+
/*
7489
const type = encode(typeWithoutNamespace);
7590
result.type = type;
7691
result.base = type;
7792
result.imports.push(type);
93+
*/
7894
return result;
7995
}
8096

0 commit comments

Comments
 (0)