diff --git a/src/AnnotationsReader/BasicAnnotationsReader.ts b/src/AnnotationsReader/BasicAnnotationsReader.ts index adf8f9623..fca307576 100644 --- a/src/AnnotationsReader/BasicAnnotationsReader.ts +++ b/src/AnnotationsReader/BasicAnnotationsReader.ts @@ -85,7 +85,9 @@ export class BasicAnnotationsReader implements AnnotationsReader { } private parseJsDocTag(jsDocTag: ts.JSDocTagInfo): any { - const text = (jsDocTag.text ?? []).map((part) => part.text).join(""); + // Tags without explicit value (e.g. `@deprecated`) default to `true`. + const text = jsDocTag.text?.map((part) => part.text).join("") || "true"; + if (BasicAnnotationsReader.textTags.has(jsDocTag.name)) { return text; } else if (BasicAnnotationsReader.jsonTags.has(jsDocTag.name)) { @@ -97,6 +99,7 @@ export class BasicAnnotationsReader implements AnnotationsReader { return undefined; } } + private parseJson(value: string): any { try { return json5.parse(value); diff --git a/test/valid-data-annotations.test.ts b/test/valid-data-annotations.test.ts index c07efc013..0840632b5 100644 --- a/test/valid-data-annotations.test.ts +++ b/test/valid-data-annotations.test.ts @@ -4,6 +4,7 @@ describe("valid-data-annotations", () => { it( "annotation-custom", assertValidSchema("annotation-custom", "MyObject", "basic", [ + "customBooleanProperty", "customNumberProperty", "customStringProperty", "customComplexProperty", @@ -18,15 +19,19 @@ describe("valid-data-annotations", () => { assertValidSchema("annotation-empty", "MyObject", "extended", ["customEmptyAnnotation"]) ); it( - "annotation-deprecated-empty-basic", - assertValidSchema("annotation-deprecated-empty", "MyObject", "basic", ["customEmptyAnnotation"]) + "annotation-deprecated-basic", + assertValidSchema("annotation-deprecated", "MyObject", "basic", ["deprecationMessage"]) ); it( - "annotation-deprecated-empty-extended", - assertValidSchema("annotation-deprecated-empty", "MyObject", "extended", ["customEmptyAnnotation"]) + "annotation-deprecated-extended", + assertValidSchema("annotation-deprecated", "MyObject", "extended", ["deprecationMessage"]) ); it("annotation-comment", assertValidSchema("annotation-comment", "MyObject", "extended")); it("annotation-id", assertValidSchema("annotation-id", "MyObject", "extended", [], "Test")); + + it("annotation-readOnly", assertValidSchema("annotation-readOnly", "MyObject", "basic")); + + it("annotation-writeOnly", assertValidSchema("annotation-writeOnly", "MyObject", "basic")); }); diff --git a/test/valid-data/annotation-custom/main.ts b/test/valid-data/annotation-custom/main.ts index b7271ec11..3a3e99ed1 100644 --- a/test/valid-data/annotation-custom/main.ts +++ b/test/valid-data/annotation-custom/main.ts @@ -1,4 +1,5 @@ /** + * @customBooleanProperty false * @customNumberProperty 14 * @customStringProperty "string-value" * @customComplexProperty { "a": 1, "b": 2 } diff --git a/test/valid-data/annotation-custom/schema.json b/test/valid-data/annotation-custom/schema.json index 2b56dceca..d6784f9db 100644 --- a/test/valid-data/annotation-custom/schema.json +++ b/test/valid-data/annotation-custom/schema.json @@ -4,6 +4,7 @@ "definitions": { "MyObject": { "additionalProperties": false, + "customBooleanProperty": false, "customComplexProperty": { "a": 1, "b": 2 diff --git a/test/valid-data/annotation-deprecated-empty/main.ts b/test/valid-data/annotation-deprecated-empty/main.ts deleted file mode 100644 index bd374389e..000000000 --- a/test/valid-data/annotation-deprecated-empty/main.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * @deprecated - */ -export interface MyObject {} diff --git a/test/valid-data/annotation-deprecated/main.ts b/test/valid-data/annotation-deprecated/main.ts new file mode 100644 index 000000000..d80aa1f0e --- /dev/null +++ b/test/valid-data/annotation-deprecated/main.ts @@ -0,0 +1,9 @@ +/** + * @deprecated + * @deprecationMessage Use `NewMyObject` instead. + */ +export interface MyObject { + one?: string; + /** @deprecated */ + two?: number; +} diff --git a/test/valid-data/annotation-deprecated/schema.json b/test/valid-data/annotation-deprecated/schema.json new file mode 100644 index 000000000..3aa98b5ce --- /dev/null +++ b/test/valid-data/annotation-deprecated/schema.json @@ -0,0 +1,21 @@ +{ + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": false, + "deprecated": true, + "deprecationMessage": "Use `NewMyObject` instead.", + "properties": { + "one": { + "type": "string" + }, + "two": { + "deprecated": true, + "type": "number" + } + }, + "type": "object" + } + } +} diff --git a/test/valid-data/annotation-empty/schema.json b/test/valid-data/annotation-empty/schema.json index ddb3e490d..46115745e 100644 --- a/test/valid-data/annotation-empty/schema.json +++ b/test/valid-data/annotation-empty/schema.json @@ -4,7 +4,7 @@ "definitions": { "MyObject": { "additionalProperties": false, - "customEmptyAnnotation": "", + "customEmptyAnnotation": true, "type": "object" } } diff --git a/test/valid-data/annotation-readOnly/main.ts b/test/valid-data/annotation-readOnly/main.ts new file mode 100644 index 000000000..73c08320d --- /dev/null +++ b/test/valid-data/annotation-readOnly/main.ts @@ -0,0 +1,5 @@ +export interface MyObject { + one?: string; + /** @readOnly */ + two?: number; +} diff --git a/test/valid-data/annotation-deprecated-empty/schema.json b/test/valid-data/annotation-readOnly/schema.json similarity index 54% rename from test/valid-data/annotation-deprecated-empty/schema.json rename to test/valid-data/annotation-readOnly/schema.json index 871ebc196..d8ea55cf4 100644 --- a/test/valid-data/annotation-deprecated-empty/schema.json +++ b/test/valid-data/annotation-readOnly/schema.json @@ -4,7 +4,15 @@ "definitions": { "MyObject": { "additionalProperties": false, - "deprecated": "", + "properties": { + "one": { + "type": "string" + }, + "two": { + "readOnly": true, + "type": "number" + } + }, "type": "object" } } diff --git a/test/valid-data/annotation-writeOnly/main.ts b/test/valid-data/annotation-writeOnly/main.ts new file mode 100644 index 000000000..5166fa6d7 --- /dev/null +++ b/test/valid-data/annotation-writeOnly/main.ts @@ -0,0 +1,5 @@ +export interface MyObject { + one?: string; + /** @writeOnly */ + two?: number; +} diff --git a/test/valid-data/annotation-writeOnly/schema.json b/test/valid-data/annotation-writeOnly/schema.json new file mode 100644 index 000000000..7b05cbc9e --- /dev/null +++ b/test/valid-data/annotation-writeOnly/schema.json @@ -0,0 +1,19 @@ +{ + "$ref": "#/definitions/MyObject", + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "MyObject": { + "additionalProperties": false, + "properties": { + "one": { + "type": "string" + }, + "two": { + "type": "number", + "writeOnly": true + } + }, + "type": "object" + } + } +}