Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix interface inheritance #34 #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/TypeFormatter/ObjectTypeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { Definition } from "../Schema/Definition";
import { StringMap } from "../Utils/StringMap";
import { UnionType } from "../Type/UnionType";
import { UndefinedType } from "../Type/UndefinedType";
import { assertInstanceOf } from "../Utils/assert";
import { derefType } from "../Utils/derefType";
import { uniqueArray } from "../Utils/uniqueArray";

export class ObjectTypeFormatter implements SubTypeFormatter {
public constructor(
Expand All @@ -22,11 +25,25 @@ export class ObjectTypeFormatter implements SubTypeFormatter {
return this.getObjectDefinition(type);
}

const propertyNames = this.getPropertyNames(type);

return {
allOf: [
this.getObjectDefinition(type),
...type.getBaseTypes().map((baseType) => this.childTypeFormatter.getDefinition(baseType)),
{
...this.getObjectDefinition(type),
additionalProperties: true,
},
...type.getBaseTypes().map((baseType) => ({
...this.childTypeFormatter.getDefinition(baseType),
additionalProperties: true,
})),
],
...!propertyNames.length ? {} : {
propertyNames: {
type: "string",
enum: propertyNames,
},
},
};
}
public getChildren(type: ObjectType): BaseType[] {
Expand Down Expand Up @@ -103,4 +120,34 @@ export class ObjectTypeFormatter implements SubTypeFormatter {
false,
);
}

private getPropertyNames(type: ObjectType): string[] {
if (this.hasAdditionalProperties(type)) {
return [];
}

const parents = type.getBaseTypes().map((baseType) => assertInstanceOf(
derefType(baseType),
ObjectType,
`Object parent type should be instance of UnionType ("${baseType.getId()}" given)`,
));

return uniqueArray(
parents.reduce((result, parent) => [
...result,
...this.getPropertyNames(parent),
], type.getProperties().map((it) => it.getName())),
);
}
private hasAdditionalProperties(type: ObjectType): boolean {
if (type.getAdditionalProperties() !== undefined) {
return true;
}

return type.getBaseTypes().map((baseType) => assertInstanceOf(
derefType(baseType),
ObjectType,
`Object parent type should be instance of UnionType ("${baseType.getId()}" given)`,
)).some((baseType) => this.hasAdditionalProperties(baseType));
}
}
12 changes: 10 additions & 2 deletions test/config/jsdoc-inheritance/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"MyObject": {
"allOf": [
{
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"bar": {
"type": "number"
Expand All @@ -40,9 +40,17 @@
"type": "object"
},
{
"additionalProperties": true,
"$ref": "#/definitions/Base"
}
]
],
"propertyNames": {
"type": "string",
"enum": [
"foo",
"bar"
]
}
}
}
}
5 changes: 5 additions & 0 deletions test/valid-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ describe("valid-data", () => {
assertSchema("interface-multi", "MyObject");
assertSchema("interface-recursion", "MyObject");
assertSchema("interface-extra-props", "MyObject");
assertSchema("interface-extends-1", "Example");
assertSchema("interface-extends-2", "MyObject");
assertSchema("interface-extends-3", "MyObject");
assertSchema("interface-extends-4", "MyObject");
assertSchema("interface-extends-5", "MyObject");

assertSchema("structure-private", "MyObject");
assertSchema("structure-anonymous", "MyObject");
Expand Down
15 changes: 13 additions & 2 deletions test/valid-data/generic-hell/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"MyObject": {
"allOf": [
{
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"someAlias": {
"$ref": "#/definitions/SomeAlias<\"alias\">"
Expand All @@ -141,12 +141,23 @@
"type": "object"
},
{
"additionalProperties": true,
"$ref": "#/definitions/GenericC<GenericC<GenericC<GenericA<string>>>>"
},
{
"additionalProperties": true,
"$ref": "#/definitions/B"
}
]
],
"propertyNames": {
"type": "string",
"enum": [
"someGeneric",
"someAlias",
"c",
"b"
]
}
},
"SomeAlias<\"alias\">": {
"$ref": "#/definitions/SomeGeneric<\"alias\",\"alias\">"
Expand Down
7 changes: 7 additions & 0 deletions test/valid-data/interface-extends-1/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface BaseExample {
baseExampleProperty: number;
}

export interface Example extends BaseExample {
exampleProperty: string;
}
41 changes: 41 additions & 0 deletions test/valid-data/interface-extends-1/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Example",
"definitions": {
"Example": {
"allOf": [
{
"type": "object",
"properties": {
"exampleProperty": {
"type": "string"
}
},
"required": [
"exampleProperty"
],
"additionalProperties": true
},
{
"type": "object",
"properties": {
"baseExampleProperty": {
"type": "number"
}
},
"required": [
"baseExampleProperty"
],
"additionalProperties": true
}
],
"propertyNames": {
"type": "string",
"enum": [
"exampleProperty",
"baseExampleProperty"
]
}
}
}
}
10 changes: 10 additions & 0 deletions test/valid-data/interface-extends-2/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface MyGrandParent {
grandParentField: string;
[name: string]: string | number | boolean;
}
export interface MyParent extends MyGrandParent {
parentField: number;
}
export interface MyObject extends MyParent {
objectField: boolean;
}
64 changes: 64 additions & 0 deletions test/valid-data/interface-extends-2/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/MyObject",
"definitions": {
"MyObject": {
"allOf": [
{
"type": "object",
"properties": {
"objectField": {
"type": "boolean"
}
},
"required": [
"objectField"
],
"additionalProperties": true
},
{
"$ref": "#/definitions/MyParent",
"additionalProperties": true
}
]
},
"MyParent": {
"allOf": [
{
"type": "object",
"properties": {
"parentField": {
"type": "number"
}
},
"required": [
"parentField"
],
"additionalProperties": true
},
{
"$ref": "#/definitions/MyGrandParent",
"additionalProperties": true
}
]
},
"MyGrandParent": {
"type": "object",
"properties": {
"grandParentField": {
"type": "string"
}
},
"required": [
"grandParentField"
],
"additionalProperties": {
"type": [
"string",
"number",
"boolean"
]
}
}
}
}
10 changes: 10 additions & 0 deletions test/valid-data/interface-extends-3/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface MyParent1 {
parentField1: string;
[name: string]: string | number | boolean;
}
export interface MyParent2 {
parentField2: number;
}
export interface MyObject extends MyParent1, MyParent2 {
objectField: boolean;
}
60 changes: 60 additions & 0 deletions test/valid-data/interface-extends-3/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/MyObject",
"definitions": {
"MyObject": {
"allOf": [
{
"type": "object",
"properties": {
"objectField": {
"type": "boolean"
}
},
"required": [
"objectField"
],
"additionalProperties": true
},
{
"$ref": "#/definitions/MyParent1",
"additionalProperties": true
},
{
"$ref": "#/definitions/MyParent2",
"additionalProperties": true
}
]
},
"MyParent1": {
"type": "object",
"properties": {
"parentField1": {
"type": "string"
}
},
"required": [
"parentField1"
],
"additionalProperties": {
"type": [
"string",
"number",
"boolean"
]
}
},
"MyParent2": {
"type": "object",
"properties": {
"parentField2": {
"type": "number"
}
},
"required": [
"parentField2"
],
"additionalProperties": false
}
}
}
9 changes: 9 additions & 0 deletions test/valid-data/interface-extends-4/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface MyGrandParent {
grandParentField: string;
}
export interface MyParent extends MyGrandParent {
parentField: number;
}
export interface MyObject extends MyParent {
objectField: boolean;
}
Loading