Skip to content

Commit

Permalink
fix: Definition Not Created When Properties Are Nullable (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatOneAwkwardGuy committed Sep 25, 2022
1 parent 1a935d8 commit 278e791
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Utils/removeUnreachable.ts
Expand Up @@ -39,7 +39,7 @@ function addReachable(
}
} else if (definition.not) {
addReachable(definition.not, definitions, reachable);
} else if (definition.type === "object") {
} else if (definition.type?.includes("object")) {
for (const prop in definition.properties || {}) {
const propDefinition = definition.properties![prop];
addReachable(propDefinition, definitions, reachable);
Expand All @@ -49,7 +49,7 @@ function addReachable(
if (additionalProperties) {
addReachable(additionalProperties, definitions, reachable);
}
} else if (definition.type === "array") {
} else if (definition.type?.includes("array")) {
const items = definition.items;
if (Array.isArray(items)) {
for (const item of items) {
Expand Down
2 changes: 2 additions & 0 deletions test/valid-data-annotations.test.ts
Expand Up @@ -73,4 +73,6 @@ describe("valid-data-annotations", () => {
it("annotation-writeOnly", assertValidSchema("annotation-writeOnly", "MyObject", "basic"));

it("annotation-union-if-then", assertValidSchema("annotation-union-if-then", "Animal", "basic"));

it("annotation-nullable-definition", assertValidSchema("annotation-nullable-definition", "MyObject", "extended"));
});
10 changes: 10 additions & 0 deletions test/valid-data/annotation-nullable-definition/main.ts
@@ -0,0 +1,10 @@
export class Definition {
name: string
}

export class MyObject {
/**
* @nullable
*/
optional?: Definition[]
}
33 changes: 33 additions & 0 deletions test/valid-data/annotation-nullable-definition/schema.json
@@ -0,0 +1,33 @@
{
"$schema":"http://json-schema.org/draft-07/schema#",
"$ref":"#/definitions/MyObject",
"definitions":{
"MyObject":{
"type":"object",
"properties":{
"optional":{
"type":[
"array",
"null"
],
"items":{
"$ref":"#/definitions/Definition"
}
}
},
"additionalProperties":false
},
"Definition":{
"type":"object",
"properties":{
"name":{
"type":"string"
}
},
"required":[
"name"
],
"additionalProperties":false
}
}
}

0 comments on commit 278e791

Please sign in to comment.