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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inline oneOf discriminated properties #27

Merged
merged 3 commits into from
Jan 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 96 additions & 24 deletions Bonsai.Sgen.Tests/DiscriminatorGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ namespace Bonsai.Sgen.Tests
[TestClass]
public class DiscriminatorGenerationTests
{
static void AssertDiscriminatorAttribute(string code, SerializerLibraries serializerLibraries, string discriminatorName)
{
if (serializerLibraries.HasFlag(SerializerLibraries.NewtonsoftJson))
{
Assert.IsTrue(
code.Contains($"[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"{discriminatorName}\")]"),
message: "Missing JSON discriminator attribute.");
}
if (serializerLibraries.HasFlag(SerializerLibraries.YamlDotNet))
{
Assert.IsTrue(
code.Contains($"[YamlDiscriminator(\"{discriminatorName}\")]"),
message: "Missing YAML discriminator attribute.");
}
}

[TestMethod]
[DataRow(SerializerLibraries.YamlDotNet)]
[DataRow(SerializerLibraries.NewtonsoftJson)]
Expand Down Expand Up @@ -80,18 +96,7 @@ public async Task GenerateFromAnyOfDiscriminatorSchema_SerializerAnnotationsDecl
var generator = TestHelper.CreateGenerator(schema, serializerLibraries);
var code = generator.GenerateFile();
Assert.IsTrue(code.Contains("[JsonInheritanceAttribute(\"DogType\", typeof(Dog))]"));
if (serializerLibraries.HasFlag(SerializerLibraries.NewtonsoftJson))
{
Assert.IsTrue(
code.Contains("[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"discriminator\")]"),
message: "Missing JSON discriminator attribute.");
}
if (serializerLibraries.HasFlag(SerializerLibraries.YamlDotNet))
{
Assert.IsTrue(
code.Contains("[YamlDiscriminator(\"discriminator\")]"),
message: "Missing YAML discriminator attribute.");
}
AssertDiscriminatorAttribute(code, serializerLibraries, "discriminator");
CompilerTestHelper.CompileFromSource(code);
}

Expand Down Expand Up @@ -194,18 +199,85 @@ public async Task GenerateFromOneOfDiscriminatorSchema_SerializerAnnotationsDecl
Assert.IsTrue(!code.Contains("public enum DogKind"), "Discriminator property is repeated in derived types.");
Assert.IsTrue(code.Contains("List<Animal> Animals"), "Container array element type does not match base type.");
Assert.IsTrue(code.Contains("[JsonInheritanceAttribute(\"Dog\", typeof(Dog))]"));
if (serializerLibraries.HasFlag(SerializerLibraries.NewtonsoftJson))
{
Assert.IsTrue(
code.Contains("[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), \"kind\")]"),
message: "Missing JSON discriminator attribute.");
}
if (serializerLibraries.HasFlag(SerializerLibraries.YamlDotNet))
{
Assert.IsTrue(
code.Contains("[YamlDiscriminator(\"kind\")]"),
message: "Missing YAML discriminator attribute.");
}
AssertDiscriminatorAttribute(code, serializerLibraries, "kind");
CompilerTestHelper.CompileFromSource(code);
}

[TestMethod]
[DataRow(SerializerLibraries.YamlDotNet)]
[DataRow(SerializerLibraries.NewtonsoftJson)]
[DataRow(SerializerLibraries.NewtonsoftJson | SerializerLibraries.YamlDotNet)]
public async Task GenerateFromOneOfDiscriminatorSchemaProperty_SerializerAnnotationsDeclareKnownTypes(SerializerLibraries serializerLibraries)
{
var schema = await JsonSchema.FromJsonAsync(@"
{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object"",
""title"": ""Container"",
""properties"": {
""Animal"": {
""x-abstract"": true,
""discriminator"": {
""propertyName"": ""kind"",
""mapping"": {
""Dog"": ""#/definitions/Dog"",
""Cat"": ""#/definitions/Cat""
}
},
""oneOf"": [
{
""$ref"": ""#/definitions/Dog""
},
{
""$ref"": ""#/definitions/Cat""
},
{
""type"": ""null""
}
]
}
},
""definitions"": {
""Dog"": {
""type"": ""object"",
""properties"": {
""kind"": {
""enum"": [ ""Dog"" ]
},
""Bar"": {
""type"": [
""null"",
""string""
]
}
},
""required"": [ ""kind"", ""Bar"" ]
},
""Cat"": {
""type"": ""object"",
""properties"": {
""kind"": {
""enum"": [ ""Cat"" ]
},
""Baz"": {
""type"": [
""null"",
""string""
]
}
},
""required"": [ ""kind"", ""Baz"" ]
}
}
}
");
var generator = TestHelper.CreateGenerator(schema, serializerLibraries);
var code = generator.GenerateFile();
Assert.IsTrue(code.Contains("class Dog : Animal"), "Derived types do not inherit from base type.");
Assert.IsTrue(!code.Contains("public enum DogKind"), "Discriminator property is repeated in derived types.");
Assert.IsTrue(code.Contains("Animal Animal"), "Container element type does not match base type.");
Assert.IsTrue(code.Contains("[JsonInheritanceAttribute(\"Dog\", typeof(Dog))]"));
AssertDiscriminatorAttribute(code, serializerLibraries, "kind");
CompilerTestHelper.CompileFromSource(code);
}
}
Expand Down
28 changes: 26 additions & 2 deletions Bonsai.Sgen/JsonSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,41 @@ public static JsonSchema WithUniqueDiscriminatorProperties(this JsonSchema schem

class DiscriminatorSchemaVisitor : JsonSchemaVisitorBase
{
public DiscriminatorSchemaVisitor(object rootObject)
public DiscriminatorSchemaVisitor(JsonSchema rootObject)
{
RootObject = rootObject;
}

public object RootObject { get; }
public JsonSchema RootObject { get; }

protected override JsonSchema VisitSchema(JsonSchema schema, string path, string typeNameHint)
{
if (schema.DiscriminatorObject != null)
{
if (schema is JsonSchemaProperty schemaProperty)
{
if (!RootObject.Definitions.ContainsKey(typeNameHint))
{
var discriminatorSchema = new JsonSchema();
discriminatorSchema.DiscriminatorObject = schemaProperty.DiscriminatorObject;
discriminatorSchema.IsAbstract = schemaProperty.IsAbstract;
foreach (var derivedSchema in schemaProperty.OneOf)
{
if (derivedSchema.IsNullable(SchemaType.JsonSchema))
{
continue;
}

derivedSchema.ActualSchema.AllOf.Add(new JsonSchema { Reference = discriminatorSchema });
}
RootObject.Definitions.Add(typeNameHint, discriminatorSchema);
}

schemaProperty.DiscriminatorObject = null;
schemaProperty.IsAbstract = false;
return schema;
}

foreach (var derivedSchema in schema.GetDerivedSchemas(RootObject).Keys)
{
foreach (var property in derivedSchema.Properties.Keys.ToList())
Expand Down