Skip to content

Commit

Permalink
Merge branch 'juneidy-self-ref-allof'
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Fernando Planella Gonzalez committed Sep 6, 2024
2 parents 0075218 + e4f8cd8 commit 65c05b9
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 36 deletions.
2 changes: 1 addition & 1 deletion lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Model extends GenType {
this.properties = sortedNames.map(propName => propertiesByName.get(propName) as Property);
} else {
// Simple / array / enum / union / intersection
this.simpleType = tsType(schema, options, openApi);
this.simpleType = tsType(schema, options, openApi, this);
}
this.collectImports(schema);
this.updateImports();
Expand Down
6 changes: 6 additions & 0 deletions test/self-ref-allof.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../ng-openapi-gen-schema.json",
"input": "self-ref-allof.json",
"output": "out/self-ref-allof",
"ignoreUnusedModels": false
}
57 changes: 57 additions & 0 deletions test/self-ref-allof.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"openapi": "3.0.1",
"info": {
"title": "Blah",
"version": "1"
},
"paths": {},
"components": {
"schemas": {
"Parent.Class": {
"type": "object",
"properties": {
"parentProps": {
"type": "string"
}
}
},
"Foo.Bar.Baz": {
"allOf": [{ "$ref": "#/components/schemas/Parent.Class" }, {
"type": "object",
"required": [
"arrayProperty"
],
"properties": {
"refProperty": {
"$ref": "#/components/schemas/Foo.Bar.Baz"
},
"arrayProperty": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Foo.Bar.Baz"
}
},
"objectProperty": {
"type": "object",
"required": [
"nestedArray",
"nestedRef"
],
"properties": {
"nestedArray": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Foo.Bar.Baz"
}
},
"nestedRef": {
"$ref": "#/components/schemas/Foo.Bar.Baz"
}
}
}
}
}]
}
}
}
}
101 changes: 66 additions & 35 deletions test/self-ref.spec.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
import {
InterfaceDeclaration,
TypeAliasDeclaration,
TypescriptParser
} from 'typescript-parser';
import { NgOpenApiGen } from '../lib/ng-openapi-gen';
import options from './self-ref.config.json';
import selfRef from './self-ref.json';

const gen = new NgOpenApiGen(selfRef, options);
gen.generate();

describe('Generation tests using self-ref.json', () => {
it('Baz model', done => {
const baz = gen.models.get('Foo.Bar.Baz');
const ts = gen.templates.apply('model', baz);

const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.declarations.length).toBe(1);
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
const decl = ast.declarations[0] as InterfaceDeclaration;
expect(decl.name).toBe('Baz');
expect(decl.properties.length).toBe(3);

const ref = decl.properties.find(p => p.name === 'refProperty');
expect(ref).withContext('refProperty property').toBeDefined();
if (ref) {
expect(ref.type).toBe('Baz');
}

const array = decl.properties.find(p => p.name === 'arrayProperty');
expect(array).withContext('arrayProperty property').toBeDefined();
if (array) {
expect(array.type).toBe('Array<Baz>');
}

const object = decl.properties.find(p => p.name === 'objectProperty');
expect(object).withContext('objectProperty property').toBeDefined();
if (object) {
expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}');
}

done();
import optionsAllof from './self-ref-allof.config.json';
import selfRefAllof from './self-ref-allof.json';

describe('Test self referencing', () => {
describe('Generation tests using self-ref.json', () => {
const gen = new NgOpenApiGen(selfRef, options);
gen.generate();
it('Baz model', done => {
const baz = gen.models.get('Foo.Bar.Baz');
const ts = gen.templates.apply('model', baz);

const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.declarations.length).toBe(1);
expect(ast.declarations[0]).toEqual(jasmine.any(InterfaceDeclaration));
const decl = ast.declarations[0] as InterfaceDeclaration;
expect(decl.name).toBe('Baz');
expect(decl.properties.length).toBe(3);

const ref = decl.properties.find(p => p.name === 'refProperty');
expect(ref).withContext('refProperty property').toBeDefined();
if (ref) {
expect(ref.type).toBe('Baz');
}

const array = decl.properties.find(p => p.name === 'arrayProperty');
expect(array).withContext('arrayProperty property').toBeDefined();
if (array) {
expect(array.type).toBe('Array<Baz>');
}

const object = decl.properties.find(p => p.name === 'objectProperty');
expect(object).withContext('objectProperty property').toBeDefined();
if (object) {
expect(object.type).toBe('{\n\'nestedArray\': Array<Baz>;\n\'nestedRef\': Baz;\n}');
}

done();
});
});

});

describe('Generation tests using self-ref-allof.json', () => {
const gen = new NgOpenApiGen(selfRefAllof, optionsAllof);
gen.generate();
it('Baz model', done => {
const baz = gen.models.get('Foo.Bar.Baz');
const ts = gen.templates.apply('model', baz);

const parser = new TypescriptParser();
parser.parseSource(ts).then(ast => {
expect(ast.declarations.length).toBe(1);
expect(ast.declarations[0]).toEqual(jasmine.any(TypeAliasDeclaration));
const decl = ast.declarations[0] as TypeAliasDeclaration;
expect(decl.name).toBe('Baz');

const text = ts.substring(decl.start || 0, decl.end || ts.length);

expect(text).toContain('\'refProperty\'?: Baz;');
expect(text).toContain('\'arrayProperty\': Array<Baz>;');
expect(text).toContain('\'nestedArray\': Array<Baz>;');
expect(text).toContain('\'nestedRef\': Baz;');

done();
});
});
});
});

0 comments on commit 65c05b9

Please sign in to comment.