From cc1b58e9cd2270caeb07848a0c8252a36599f6c1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Planella Gonzalez Date: Fri, 26 Jul 2019 13:19:17 -0300 Subject: [PATCH] Generate models referenced only by oneOf Fixes #24 Fixes #28 --- lib/ng-openapi-gen.ts | 1 + test/all-types.json | 453 +++++++++++++++++++++-------------------- test/all-types.spec.ts | 19 ++ 3 files changed, 255 insertions(+), 218 deletions(-) diff --git a/lib/ng-openapi-gen.ts b/lib/ng-openapi-gen.ts index 55a81fa..51d403d 100644 --- a/lib/ng-openapi-gen.ts +++ b/lib/ng-openapi-gen.ts @@ -231,6 +231,7 @@ export class NgOpenApiGen { const result: string[] = []; (schema.allOf || []).forEach(s => Array.prototype.push.apply(result, this.allReferencedNames(s))); (schema.anyOf || []).forEach(s => Array.prototype.push.apply(result, this.allReferencedNames(s))); + (schema.oneOf || []).forEach(s => Array.prototype.push.apply(result, this.allReferencedNames(s))); if (schema.properties) { for (const prop of Object.keys(schema.properties)) { Array.prototype.push.apply(result, this.allReferencedNames(schema.properties[prop])); diff --git a/test/all-types.json b/test/all-types.json index 0afbecd..ec76e9b 100644 --- a/test/all-types.json +++ b/test/all-types.json @@ -1,233 +1,250 @@ { - "openapi": "3.0", - "info": { - "title": "Test with all types", - "version": "1.0" - }, - "paths": { - "/test": { - "get": { - "responses": { - "200": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Containers" - } - } - } - } + "openapi": "3.0", + "info": { + "title": "Test with all types", + "version": "1.0" + }, + "paths": { + "/test": { + "get": { + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Containers" } + } } + } + } + } + } + }, + "components": { + "schemas": { + "RefEnum": { + "type": "string", + "enum": [ + "valueA", + "valueB", + "valueC" + ] + }, + "RefObject": { + "type": "object", + "required": [ + "id" + ], + "properties": { + "id": { + "type": "string" + } } - }, - "components": { - "schemas": { - "RefEnum": { + }, + "OtherObject": { + "allOf": [ + { + "$ref": "#/components/schemas/RefObject" + }, + { + "type": "object", + "additionalProperties": true + } + ] + }, + "Union": { + "anyOf": [ + { + "type": "object", + "additionalProperties": true + }, + { + "$ref": "#/components/schemas/RefEnum" + }, + { + "$ref": "#/components/schemas/Container" + } + ] + }, + "Disjunct": { + "type": "object", + "properties": { + "id": { + "type": "string" + } + }, + "oneOf": [ + { + "$ref": "#/components/schemas/RefObject" + }, + { + "$ref": "#/components/schemas/OtherObject" + }, + { + "$ref": "#/components/schemas/ReferencedInOneOf" + } + ] + }, + "ReferencedInOneOf": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, + "Containers": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Container" + } + }, + "Container": { + "allOf": [ + { + "type": "object", + "properties": { + "stringProp": { "type": "string", - "enum": ["valueA", "valueB", "valueC"] - }, - "RefObject": { - "type": "object", - "required": ["id"], - "properties": { - "id": { - "type": "string" - } - } - }, - "OtherObject": { + "description": "Property of type string" + }, + "integerProp": { + "type": "integer", + "description": "Property of type integer" + }, + "numberProp": { + "type": "number", + "description": "Property of type number" + }, + "booleanProp": { + "type": "boolean", + "description": "Property of type boolean" + }, + "anyProp": { + "description": "Property of type any" + } + } + }, + { + "$ref": "#/components/schemas/RefObject" + }, + { + "type": "object", + "properties": { + "refEnumProp": { + "$ref": "#/components/schemas/RefEnum", + "description": "Property which references an enumerated string" + }, + "refObjectProp": { + "description": "Property which references an object", "allOf": [ - { - "$ref": "#/components/schemas/RefObject" - }, - { - "type": "object", - "additionalProperties": true - } + { + "$ref": "#/components/schemas/RefObject" + } ] - }, - "Union": { - "anyOf": [ - { - "type": "object", - "additionalProperties": true - }, - { - "$ref": "#/components/schemas/RefEnum" - }, - { - "$ref": "#/components/schemas/Container" - } - ] - }, - "Disjunct": { - "type": "object", - "properties": { - "id": { - "type": "string" - } - }, - "oneOf": [ - { - "$ref": "#/components/schemas/RefObject" - }, - { - "$ref": "#/components/schemas/OtherObject" - } - ] - }, - "Containers": { + }, + "unionProp": { + "$ref": "#/components/schemas/Union", + "description": "Property which references an Union object" + }, + "disjunctProp": { + "$ref": "#/components/schemas/Disjunct", + "decription": "Property wich references an Disjunct object" + }, + "containerProp": { + "$ref": "#/components/schemas/Container", + "description": "Property which references another container" + }, + "arrayOfStringsProp": { "type": "array", + "description": "Property of type array of string", "items": { - "$ref": "#/components/schemas/Container" + "type": "string" } - }, - "Container": { - "allOf": [ - { - "type": "object", - "properties": { - "stringProp": { - "type": "string", - "description": "Property of type string" - }, - "integerProp": { - "type": "integer", - "description": "Property of type integer" - }, - "numberProp": { - "type": "number", - "description": "Property of type number" - }, - "booleanProp": { - "type": "boolean", - "description": "Property of type boolean" - }, - "anyProp": { - "description": "Property of type any" - } - } - }, - { + }, + "arrayOfIntegersProp": { + "type": "array", + "description": "Property of type array of integers", + "items": { + "type": "integer" + } + }, + "arrayOfNumbersProp": { + "type": "array", + "description": "Property of type array of numbers", + "items": { + "type": "number" + } + }, + "arrayOfBooleansProp": { + "type": "array", + "description": "Property of type array of booleans", + "items": { + "type": "boolean" + } + }, + "arrayOfRefEnumsProp": { + "type": "array", + "description": "Property of type array of enums", + "items": { + "$ref": "#/components/schemas/RefEnum" + } + }, + "arrayOfRefObjectsProp": { + "type": "array", + "description": "Property of type array of references an object", + "items": { + "$ref": "#/components/schemas/RefObject" + } + }, + "arrayOfAnyProp": { + "type": "array", + "description": "Property of type array of any type", + "items": {} + }, + "nestedObject": { + "type": "object", + "properties": { + "p1": { + "type": "string" + }, + "p2": { + "type": "integer" + }, + "deeper": { + "type": "object", + "properties": { + "d1": { "$ref": "#/components/schemas/RefObject" - }, - { - "type": "object", - "properties": { - "refEnumProp": { - "$ref": "#/components/schemas/RefEnum", - "description": "Property which references an enumerated string" - }, - "refObjectProp": { - "description": "Property which references an object", - "allOf": [ - { - "$ref": "#/components/schemas/RefObject" - } - ] - }, - "unionProp": { - "$ref": "#/components/schemas/Union", - "description": "Property which references an Union object" - }, - "disjunctProp": { - "$ref": "#/components/schemas/Disjunct", - "decription": "Property wich references an Disjunct object" - }, - "containerProp": { - "$ref": "#/components/schemas/Container", - "description": "Property which references another container" - }, - "arrayOfStringsProp": { - "type": "array", - "description": "Property of type array of string", - "items": { - "type": "string" - } - }, - "arrayOfIntegersProp": { - "type": "array", - "description": "Property of type array of integers", - "items": { - "type": "integer" - } - }, - "arrayOfNumbersProp": { - "type": "array", - "description": "Property of type array of numbers", - "items": { - "type": "number" - } - }, - "arrayOfBooleansProp": { - "type": "array", - "description": "Property of type array of booleans", - "items": { - "type": "boolean" - } - }, - "arrayOfRefEnumsProp": { - "type": "array", - "description": "Property of type array of enums", - "items": { - "$ref": "#/components/schemas/RefEnum" - } - }, - "arrayOfRefObjectsProp": { - "type": "array", - "description": "Property of type array of references an object", - "items": { - "$ref": "#/components/schemas/RefObject" - } - }, - "arrayOfAnyProp": { - "type": "array", - "description": "Property of type array of any type", - "items": {} - }, - "nestedObject": { - "type": "object", - "properties": { - "p1": { - "type": "string" - }, - "p2": { - "type": "integer" - }, - "deeper": { - "type": "object", - "properties": { - "d1": { - "$ref": "#/components/schemas/RefObject" - }, - "d2": { - "anyOf": [ - { - "type": "string" - }, - { - "type": "array", - "items": { - "$ref": "#/components/schemas/RefObject" - } - }, - { - "type": "integer" - } - ] - } - } - } - } + }, + "d2": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "$ref": "#/components/schemas/RefObject" } - }, - "additionalProperties": { - "$ref": "#/components/schemas/OtherObject" - } + }, + { + "type": "integer" + } + ] + } } - ] + } + } + } + }, + "additionalProperties": { + "$ref": "#/components/schemas/OtherObject" } - } + } + ] + } } -} + } +} \ No newline at end of file diff --git a/test/all-types.spec.ts b/test/all-types.spec.ts index f6ff01e..d330126 100644 --- a/test/all-types.spec.ts +++ b/test/all-types.spec.ts @@ -90,6 +90,25 @@ describe('Generation tests using all-types.json', () => { const decl = ast.declarations[0] as InterfaceDeclaration; expect(decl.name).toBe('Disjunct'); expect(decl.properties.length).toBe(1); + expect(decl.properties[0].name).toBe('id'); + expect(decl.properties[0].type).toBe('string'); + done(); + }); + }); + + + it('ReferencedInOneOf model', done => { + const ref = gen.models.get('ReferencedInOneOf'); + const ts = gen.templates.apply('model', ref); + const parser = new TypescriptParser(); + parser.parseSource(ts).then(ast => { + expect(ast.imports.length).toBe(0); + 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('ReferencedInOneOf'); + expect(decl.properties.length).toBe(1); + expect(decl.properties[0].name).toBe('name'); expect(decl.properties[0].type).toBe('string'); done(); });