From 1061ba159636d806469b34103a37d918b228d042 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 2 Nov 2017 17:16:30 -0700 Subject: [PATCH] Do not generate definitions that are not needed. Fixes #18 --- .../IntersectionTypeFormatter.ts | 2 +- src/TypeFormatter/ObjectTypeFormatter.ts | 19 +++++++++------ test/config/jsdoc-inheritance/schema.json | 18 -------------- test/valid-data/generic-hell/schema.json | 24 ------------------- .../schema.json | 22 ----------------- test/valid-data/type-intersection/schema.json | 24 ------------------- 6 files changed, 13 insertions(+), 96 deletions(-) diff --git a/src/TypeFormatter/IntersectionTypeFormatter.ts b/src/TypeFormatter/IntersectionTypeFormatter.ts index 1950588c1..3119ca3fe 100644 --- a/src/TypeFormatter/IntersectionTypeFormatter.ts +++ b/src/TypeFormatter/IntersectionTypeFormatter.ts @@ -22,7 +22,7 @@ export class IntersectionTypeFormatter implements SubTypeFormatter { public getChildren(type: IntersectionType): BaseType[] { return type.getTypes().reduce((result: BaseType[], item: BaseType) => [ ...result, - ...this.childTypeFormatter.getChildren(item), + ...this.childTypeFormatter.getChildren(item).slice(1), ], []); } } diff --git a/src/TypeFormatter/ObjectTypeFormatter.ts b/src/TypeFormatter/ObjectTypeFormatter.ts index ce14374eb..26e3b0341 100644 --- a/src/TypeFormatter/ObjectTypeFormatter.ts +++ b/src/TypeFormatter/ObjectTypeFormatter.ts @@ -7,6 +7,15 @@ import { TypeFormatter } from "../TypeFormatter"; import { getAllOfDefinitionReducer } from "../Utils/allOfDefinition"; import { StringMap } from "../Utils/StringMap"; +/** + * Inherited type can be an alias if it only has a base type and nothing else. + */ +function canBeAliased(type: ObjectType) { + return Object.keys(type.getProperties()).length === 0 && + type.getAdditionalProperties() === false && + type.getBaseTypes().length === 1; +} + export class ObjectTypeFormatter implements SubTypeFormatter { public constructor( private childTypeFormatter: TypeFormatter, @@ -21,11 +30,7 @@ export class ObjectTypeFormatter implements SubTypeFormatter { return this.getObjectDefinition(type); } - if ( - Object.keys(type.getProperties()).length === 0 && - type.getAdditionalProperties() === false && - type.getBaseTypes().length === 1 - ) { + if (canBeAliased(type)) { return this.childTypeFormatter.getDefinition(type.getBaseTypes()[0]); } @@ -34,12 +39,12 @@ export class ObjectTypeFormatter implements SubTypeFormatter { } public getChildren(type: ObjectType): BaseType[] { const properties: ObjectProperty[] = type.getProperties(); - const additionalProperties: BaseType|boolean = type.getAdditionalProperties(); + const additionalProperties: BaseType | boolean = type.getAdditionalProperties(); return [ ...type.getBaseTypes().reduce((result: BaseType[], baseType: BaseType) => [ ...result, - ...this.childTypeFormatter.getChildren(baseType), + ...this.childTypeFormatter.getChildren(baseType).slice(canBeAliased(type) ? 0 : 1), ], []), ...additionalProperties instanceof BaseType ? diff --git a/test/config/jsdoc-inheritance/schema.json b/test/config/jsdoc-inheritance/schema.json index 7a0e8c625..7677a3fa8 100644 --- a/test/config/jsdoc-inheritance/schema.json +++ b/test/config/jsdoc-inheritance/schema.json @@ -2,24 +2,6 @@ "$ref": "#/definitions/MyObject", "$schema": "http://json-schema.org/draft-04/schema#", "definitions": { - "Base": { - "additionalProperties": false, - "properties": { - "bar": { - "description": "Base comment.", - "type": "number" - }, - "foo": { - "description": "Base comment.", - "type": "number" - } - }, - "required": [ - "foo", - "bar" - ], - "type": "object" - }, "MyObject": { "additionalProperties": false, "properties": { diff --git a/test/valid-data/generic-hell/schema.json b/test/valid-data/generic-hell/schema.json index cacf818f5..f68e2bb40 100644 --- a/test/valid-data/generic-hell/schema.json +++ b/test/valid-data/generic-hell/schema.json @@ -25,18 +25,6 @@ ], "additionalProperties": false }, - "GenericC>>>": { - "type": "object", - "properties": { - "c": { - "$ref": "#/definitions/GenericC>>" - } - }, - "required": [ - "c" - ], - "additionalProperties": false - }, "GenericC>>": { "type": "object", "properties": { @@ -73,18 +61,6 @@ ], "additionalProperties": false }, - "B": { - "type": "object", - "properties": { - "b": { - "type": "number" - } - }, - "required": [ - "b" - ], - "additionalProperties": false - }, "SomeGeneric<1,2>": { "type": "object", "properties": { diff --git a/test/valid-data/type-intersection-additional-props/schema.json b/test/valid-data/type-intersection-additional-props/schema.json index 18fdfcfb9..d8a71a840 100644 --- a/test/valid-data/type-intersection-additional-props/schema.json +++ b/test/valid-data/type-intersection-additional-props/schema.json @@ -45,28 +45,6 @@ "bar" ], "additionalProperties": false - }, - "B": { - "type": "object", - "additionalProperties": { - "type": [ - "number", - "string" - ] - } - }, - "C": { - "type": "object", - "additionalProperties": { - "anyOf": [ - { - "$ref": "#/definitions/A" - }, - { - "type": "number" - } - ] - } } }, "$ref": "#/definitions/MyObject" diff --git a/test/valid-data/type-intersection/schema.json b/test/valid-data/type-intersection/schema.json index 9e9dccdc7..d254abf7b 100644 --- a/test/valid-data/type-intersection/schema.json +++ b/test/valid-data/type-intersection/schema.json @@ -25,30 +25,6 @@ "value" ], "additionalProperties": false - }, - "Type1": { - "type": "object", - "properties": { - "value1": { - "type": "string" - } - }, - "required": [ - "value1" - ], - "additionalProperties": false - }, - "Type2": { - "type": "object", - "properties": { - "value2": { - "type": "number" - } - }, - "required": [ - "value2" - ], - "additionalProperties": false } }, "$ref": "#/definitions/MyObject"