fix: deal with nested complex types

- make sure the definitions object is the same
  when making recursive calls to generate schema.
  this way all definitions are captured and can
  be referenced correctly.
- add tests for nested complex types
This commit is contained in:
Stevan Freeborn
2024-06-30 15:25:53 -05:00
parent 606c81d384
commit b594927df0
2 changed files with 75 additions and 2 deletions
@@ -494,11 +494,82 @@ public class JsonSchemaGeneratorTestData : IEnumerable<object[]>
},
}
};
// nested class parameter type with no attributes
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Family family) => family),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
},
[$"{typeof(Family).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Members"] = new JsonObject()
{
["type"] = "array",
["items"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}"
},
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Members"
}
}
},
["properties"] = new JsonObject()
{
["family"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Family).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"family",
},
}
};
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
class Family
{
public List<Person> Members { get; } = [];
}
class Person
{