Skip to content

Commit

Permalink
Do not generate definitions that are not needed. Fixes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 4, 2017
1 parent ac6a825 commit 1061ba1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/TypeFormatter/IntersectionTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
], []);
}
}
19 changes: 12 additions & 7 deletions src/TypeFormatter/ObjectTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]);
}

Expand All @@ -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 ?
Expand Down
18 changes: 0 additions & 18 deletions test/config/jsdoc-inheritance/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
24 changes: 0 additions & 24 deletions test/valid-data/generic-hell/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@
],
"additionalProperties": false
},
"GenericC<GenericC<GenericC<GenericA<string>>>>": {
"type": "object",
"properties": {
"c": {
"$ref": "#/definitions/GenericC<GenericC<GenericA<string>>>"
}
},
"required": [
"c"
],
"additionalProperties": false
},
"GenericC<GenericC<GenericA<string>>>": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -73,18 +61,6 @@
],
"additionalProperties": false
},
"B": {
"type": "object",
"properties": {
"b": {
"type": "number"
}
},
"required": [
"b"
],
"additionalProperties": false
},
"SomeGeneric<1,2>": {
"type": "object",
"properties": {
Expand Down
22 changes: 0 additions & 22 deletions test/valid-data/type-intersection-additional-props/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 0 additions & 24 deletions test/valid-data/type-intersection/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 1061ba1

Please sign in to comment.