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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Definition Not Created When Properties Are Nullable #1420

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}
}