Skip to content

Commit 65c05b9

Browse files
author
Luis Fernando Planella Gonzalez
committed
Merge branch 'juneidy-self-ref-allof'
2 parents 0075218 + e4f8cd8 commit 65c05b9

File tree

4 files changed

+130
-36
lines changed

4 files changed

+130
-36
lines changed

lib/model.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class Model extends GenType {
7070
this.properties = sortedNames.map(propName => propertiesByName.get(propName) as Property);
7171
} else {
7272
// Simple / array / enum / union / intersection
73-
this.simpleType = tsType(schema, options, openApi);
73+
this.simpleType = tsType(schema, options, openApi, this);
7474
}
7575
this.collectImports(schema);
7676
this.updateImports();

test/self-ref-allof.config.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "../ng-openapi-gen-schema.json",
3+
"input": "self-ref-allof.json",
4+
"output": "out/self-ref-allof",
5+
"ignoreUnusedModels": false
6+
}

test/self-ref-allof.json

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Blah",
5+
"version": "1"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"Parent.Class": {
11+
"type": "object",
12+
"properties": {
13+
"parentProps": {
14+
"type": "string"
15+
}
16+
}
17+
},
18+
"Foo.Bar.Baz": {
19+
"allOf": [{ "$ref": "#/components/schemas/Parent.Class" }, {
20+
"type": "object",
21+
"required": [
22+
"arrayProperty"
23+
],
24+
"properties": {
25+
"refProperty": {
26+
"$ref": "#/components/schemas/Foo.Bar.Baz"
27+
},
28+
"arrayProperty": {
29+
"type": "array",
30+
"items": {
31+
"$ref": "#/components/schemas/Foo.Bar.Baz"
32+
}
33+
},
34+
"objectProperty": {
35+
"type": "object",
36+
"required": [
37+
"nestedArray",
38+
"nestedRef"
39+
],
40+
"properties": {
41+
"nestedArray": {
42+
"type": "array",
43+
"items": {
44+
"$ref": "#/components/schemas/Foo.Bar.Baz"
45+
}
46+
},
47+
"nestedRef": {
48+
"$ref": "#/components/schemas/Foo.Bar.Baz"
49+
}
50+
}
51+
}
52+
}
53+
}]
54+
}
55+
}
56+
}
57+
}

test/self-ref.spec.ts

+66-35
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,78 @@
11
import {
22
InterfaceDeclaration,
3+
TypeAliasDeclaration,
34
TypescriptParser
45
} from 'typescript-parser';
56
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
67
import options from './self-ref.config.json';
78
import selfRef from './self-ref.json';
89

9-
const gen = new NgOpenApiGen(selfRef, options);
10-
gen.generate();
11-
12-
describe('Generation tests using self-ref.json', () => {
13-
it('Baz model', done => {
14-
const baz = gen.models.get('Foo.Bar.Baz');
15-
const ts = gen.templates.apply('model', baz);
16-
17-
const parser = new TypescriptParser();
18-
parser.parseSource(ts).then(ast => {
19-
expect(ast.declarations.length).toBe(1);
20-
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
21-
const decl = ast.declarations[0] as InterfaceDeclaration;
22-
expect(decl.name).toBe('Baz');
23-
expect(decl.properties.length).toBe(3);
24-
25-
const ref = decl.properties.find(p => p.name === 'refProperty');
26-
expect(ref).withContext('refProperty property').toBeDefined();
27-
if (ref) {
28-
expect(ref.type).toBe('Baz');
29-
}
30-
31-
const array = decl.properties.find(p => p.name === 'arrayProperty');
32-
expect(array).withContext('arrayProperty property').toBeDefined();
33-
if (array) {
34-
expect(array.type).toBe('Array<Baz>');
35-
}
36-
37-
const object = decl.properties.find(p => p.name === 'objectProperty');
38-
expect(object).withContext('objectProperty property').toBeDefined();
39-
if (object) {
40-
expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}');
41-
}
42-
43-
done();
10+
import optionsAllof from './self-ref-allof.config.json';
11+
import selfRefAllof from './self-ref-allof.json';
12+
13+
describe('Test self referencing', () => {
14+
describe('Generation tests using self-ref.json', () => {
15+
const gen = new NgOpenApiGen(selfRef, options);
16+
gen.generate();
17+
it('Baz model', done => {
18+
const baz = gen.models.get('Foo.Bar.Baz');
19+
const ts = gen.templates.apply('model', baz);
20+
21+
const parser = new TypescriptParser();
22+
parser.parseSource(ts).then(ast => {
23+
expect(ast.declarations.length).toBe(1);
24+
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
25+
const decl = ast.declarations[0] as InterfaceDeclaration;
26+
expect(decl.name).toBe('Baz');
27+
expect(decl.properties.length).toBe(3);
28+
29+
const ref = decl.properties.find(p => p.name === 'refProperty');
30+
expect(ref).withContext('refProperty property').toBeDefined();
31+
if (ref) {
32+
expect(ref.type).toBe('Baz');
33+
}
34+
35+
const array = decl.properties.find(p => p.name === 'arrayProperty');
36+
expect(array).withContext('arrayProperty property').toBeDefined();
37+
if (array) {
38+
expect(array.type).toBe('Array<Baz>');
39+
}
40+
41+
const object = decl.properties.find(p => p.name === 'objectProperty');
42+
expect(object).withContext('objectProperty property').toBeDefined();
43+
if (object) {
44+
expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}');
45+
}
46+
47+
done();
48+
});
4449
});
50+
4551
});
4652

53+
describe('Generation tests using self-ref-allof.json', () => {
54+
const gen = new NgOpenApiGen(selfRefAllof, optionsAllof);
55+
gen.generate();
56+
it('Baz model', done => {
57+
const baz = gen.models.get('Foo.Bar.Baz');
58+
const ts = gen.templates.apply('model', baz);
59+
60+
const parser = new TypescriptParser();
61+
parser.parseSource(ts).then(ast => {
62+
expect(ast.declarations.length).toBe(1);
63+
expect(ast.declarations[0]).toEqual(jasmine.any(TypeAliasDeclaration));
64+
const decl = ast.declarations[0] as TypeAliasDeclaration;
65+
expect(decl.name).toBe('Baz');
66+
67+
const text = ts.substring(decl.start || 0, decl.end || ts.length);
68+
69+
expect(text).toContain('\'refProperty\'?: Baz;');
70+
expect(text).toContain('\'arrayProperty\': Array<Baz>;');
71+
expect(text).toContain('\'nestedArray\': Array<Baz>;');
72+
expect(text).toContain('\'nestedRef\': Baz;');
73+
74+
done();
75+
});
76+
});
77+
});
4778
});

0 commit comments

Comments
 (0)