Skip to content

Commit

Permalink
fix: Hidden should not cause additionalProperties to be true. (#1417
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Jason3S committed Sep 22, 2022
1 parent 065ac27 commit 1a935d8
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 8 deletions.
10 changes: 6 additions & 4 deletions factory/formatter.ts
@@ -1,18 +1,20 @@
import { Config } from "../src/Config";
import { ChainTypeFormatter } from "../src/ChainTypeFormatter";
import { CircularReferenceTypeFormatter } from "../src/CircularReferenceTypeFormatter";
import { Config } from "../src/Config";
import { MutableTypeFormatter } from "../src/MutableTypeFormatter";
import { TypeFormatter } from "../src/TypeFormatter";
import { AliasTypeFormatter } from "../src/TypeFormatter/AliasTypeFormatter";
import { AnnotatedTypeFormatter } from "../src/TypeFormatter/AnnotatedTypeFormatter";
import { AnyTypeFormatter } from "../src/TypeFormatter/AnyTypeFormatter";
import { SymbolTypeFormatter } from "../src/TypeFormatter/SymbolTypeFormatter";
import { ArrayTypeFormatter } from "../src/TypeFormatter/ArrayTypeFormatter";
import { BooleanTypeFormatter } from "../src/TypeFormatter/BooleanTypeFormatter";
import { DefinitionTypeFormatter } from "../src/TypeFormatter/DefinitionTypeFormatter";
import { EnumTypeFormatter } from "../src/TypeFormatter/EnumTypeFormatter";
import { HiddenTypeFormatter } from "../src/TypeFormatter/HiddenTypeFormatter";
import { IntersectionTypeFormatter } from "../src/TypeFormatter/IntersectionTypeFormatter";
import { LiteralTypeFormatter } from "../src/TypeFormatter/LiteralTypeFormatter";
import { LiteralUnionTypeFormatter } from "../src/TypeFormatter/LiteralUnionTypeFormatter";
import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter";
import { NullTypeFormatter } from "../src/TypeFormatter/NullTypeFormatter";
import { NumberTypeFormatter } from "../src/TypeFormatter/NumberTypeFormatter";
import { ObjectTypeFormatter } from "../src/TypeFormatter/ObjectTypeFormatter";
Expand All @@ -21,13 +23,12 @@ import { PrimitiveUnionTypeFormatter } from "../src/TypeFormatter/PrimitiveUnion
import { ReferenceTypeFormatter } from "../src/TypeFormatter/ReferenceTypeFormatter";
import { RestTypeFormatter } from "../src/TypeFormatter/RestTypeFormatter";
import { StringTypeFormatter } from "../src/TypeFormatter/StringTypeFormatter";
import { SymbolTypeFormatter } from "../src/TypeFormatter/SymbolTypeFormatter";
import { TupleTypeFormatter } from "../src/TypeFormatter/TupleTypeFormatter";
import { UndefinedTypeFormatter } from "../src/TypeFormatter/UndefinedTypeFormatter";
import { UnionTypeFormatter } from "../src/TypeFormatter/UnionTypeFormatter";
import { UnknownTypeFormatter } from "../src/TypeFormatter/UnknownTypeFormatter";
import { VoidTypeFormatter } from "../src/TypeFormatter/VoidTypeFormatter";
import { MutableTypeFormatter } from "../src/MutableTypeFormatter";
import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter";

export type FormatterAugmentor = (
formatter: MutableTypeFormatter,
Expand Down Expand Up @@ -55,6 +56,7 @@ export function createFormatter(config: Config, augmentor?: FormatterAugmentor):
.addTypeFormatter(new UndefinedTypeFormatter())
.addTypeFormatter(new UnknownTypeFormatter())
.addTypeFormatter(new VoidTypeFormatter())
.addTypeFormatter(new HiddenTypeFormatter())
.addTypeFormatter(new NeverTypeFormatter())

.addTypeFormatter(new LiteralTypeFormatter())
Expand Down
4 changes: 2 additions & 2 deletions src/NodeParser/HiddenTypeNodeParser.ts
Expand Up @@ -2,7 +2,7 @@ import ts from "typescript";
import { Context } from "../NodeParser";
import { SubNodeParser } from "../SubNodeParser";
import { BaseType } from "../Type/BaseType";
import { NeverType } from "../Type/NeverType";
import { HiddenType } from "../Type/HiddenType";
import { isNodeHidden } from "../Utils/isHidden";

export class HiddenNodeParser implements SubNodeParser {
Expand All @@ -13,6 +13,6 @@ export class HiddenNodeParser implements SubNodeParser {
}

public createType(_node: ts.KeywordTypeNode, _context: Context): BaseType {
return new NeverType();
return new HiddenType();
}
}
7 changes: 7 additions & 0 deletions src/Type/HiddenType.ts
@@ -0,0 +1,7 @@
import { NeverType } from "./NeverType";

export class HiddenType extends NeverType {
public getId(): string {
return "hidden";
}
}
16 changes: 16 additions & 0 deletions src/TypeFormatter/HiddenTypeFormatter.ts
@@ -0,0 +1,16 @@
import { Definition } from "../Schema/Definition";
import { SubTypeFormatter } from "../SubTypeFormatter";
import { BaseType } from "../Type/BaseType";
import { HiddenType } from "../Type/HiddenType";

export class HiddenTypeFormatter implements SubTypeFormatter {
public supportsType(type: HiddenType): boolean {
return type instanceof HiddenType;
}
public getDefinition(type: HiddenType): Definition {
return { additionalProperties: false };
}
public getChildren(type: HiddenType): BaseType[] {
return [];
}
}
14 changes: 12 additions & 2 deletions test/config.test.ts
Expand Up @@ -269,8 +269,18 @@ describe("config", () => {

it(
"jsdoc-hidden-types",
assertSchema("jsdoc-hidden", {
type: "MyObject",
assertSchema("jsdoc-hidden-types", {
type: "MyType",
expose: "export",
topRef: true,
jsDoc: "extended",
})
);

it(
"jsdoc-hidden-types-intersection",
assertSchema("jsdoc-hidden-types-intersection", {
type: "MyType",
expose: "export",
topRef: true,
jsDoc: "extended",
Expand Down
24 changes: 24 additions & 0 deletions test/config/jsdoc-hidden-types-intersection/main.ts
@@ -0,0 +1,24 @@
/**
* @hidden
*/
export interface Hidden {
hidden?: number;
}

export type Hidden2 = Hidden;

export interface Visible {
visible: string;
}

export interface Intersection extends Visible, Hidden {}

export type MyType = {
/**
* @hidden
*/
hidden: Visible;
hidden2?: Hidden;
visible: Visible;
intersection: Intersection;
};
46 changes: 46 additions & 0 deletions test/config/jsdoc-hidden-types-intersection/schema.json
@@ -0,0 +1,46 @@
{
"$ref": "#/definitions/MyType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Intersection": {
"additionalProperties": false,
"properties": {
"visible": {
"type": "string"
}
},
"required": [
"visible"
],
"type": "object"
},
"MyType": {
"additionalProperties": false,
"properties": {
"intersection": {
"$ref": "#/definitions/Intersection"
},
"visible": {
"$ref": "#/definitions/Visible"
}
},
"required": [
"visible",
"intersection"
],
"type": "object"
},
"Visible": {
"additionalProperties": false,
"properties": {
"visible": {
"type": "string"
}
},
"required": [
"visible"
],
"type": "object"
}
}
}
10 changes: 10 additions & 0 deletions test/unit/Type/HiddenType.test.ts
@@ -0,0 +1,10 @@
import { HiddenType } from "../../../src/Type/HiddenType";
import { NeverType } from "../../../src/Type/NeverType";

describe("HiddenType", () => {
it("creates a HiddenType", () => {
const hidden = new HiddenType();
expect(hidden instanceof NeverType).toBe(true);
expect(hidden.getId()).toBe("hidden");
});
});

0 comments on commit 1a935d8

Please sign in to comment.