Skip to content

Commit

Permalink
Merge pull request #31 from bonsai-rx/discriminator-dev
Browse files Browse the repository at this point in the history
Fallback for anonymous array item discriminators
  • Loading branch information
glopesdev authored Jan 19, 2024
2 parents 7238c97 + 4c2d56a commit 21acb5c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
69 changes: 69 additions & 0 deletions Bonsai.Sgen.Tests/DiscriminatorGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,74 @@ public async Task GenerateFromOneOfDiscriminatorSchemaProperty_SerializerAnnotat
AssertDiscriminatorAttribute(code, serializerLibraries, "kind");
CompilerTestHelper.CompileFromSource(code);
}

[TestMethod]
[DataRow(SerializerLibraries.YamlDotNet)]
[DataRow(SerializerLibraries.NewtonsoftJson)]
[DataRow(SerializerLibraries.NewtonsoftJson | SerializerLibraries.YamlDotNet)]
public async Task GenerateFromArrayItemDiscriminator_EnsureFallbackDiscriminatorBaseTypeName(SerializerLibraries serializerLibraries)
{
var schema = await JsonSchema.FromJsonAsync(@"
{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object"",
""title"": ""Container"",
""properties"": {
""Animals"": {
""type"": ""array"",
""items"": {
""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"" ]
}
},
""required"": [ ""kind"" ]
},
""Cat"": {
""type"": ""object"",
""properties"": {
""kind"": {
""enum"": [ ""Cat"" ]
}
},
""required"": [ ""kind"" ]
}
}
}
");
var generator = TestHelper.CreateGenerator(schema, serializerLibraries);
var code = generator.GenerateFile();
Assert.IsTrue(code.Contains("class Dog : Anonymous"), "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("List<Anonymous> Animal"), "Container element type does not match base type.");
Assert.IsTrue(code.Contains("[JsonInheritanceAttribute(\"Dog\", typeof(Dog))]"));
AssertDiscriminatorAttribute(code, serializerLibraries, "kind");
CompilerTestHelper.CompileFromSource(code);
}
}
}
17 changes: 11 additions & 6 deletions Bonsai.Sgen/JsonSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ protected override JsonSchema VisitSchema(JsonSchema schema, string path, string
{
if (schema.DiscriminatorObject != null)
{
if (schema is JsonSchemaProperty schemaProperty)
if (schema is JsonSchemaProperty || schema.ParentSchema?.Item == schema)
{
if (string.IsNullOrEmpty(typeNameHint))
{
typeNameHint = "Anonymous";
}

if (!RootObject.Definitions.ContainsKey(typeNameHint))
{
var discriminatorSchema = new JsonSchema();
discriminatorSchema.DiscriminatorObject = schemaProperty.DiscriminatorObject;
discriminatorSchema.IsAbstract = schemaProperty.IsAbstract;
foreach (var derivedSchema in schemaProperty.OneOf)
discriminatorSchema.DiscriminatorObject = schema.DiscriminatorObject;
discriminatorSchema.IsAbstract = schema.IsAbstract;
foreach (var derivedSchema in schema.OneOf)
{
if (derivedSchema.IsNullable(SchemaType.JsonSchema))
{
Expand All @@ -45,8 +50,8 @@ protected override JsonSchema VisitSchema(JsonSchema schema, string path, string
RootObject.Definitions.Add(typeNameHint, discriminatorSchema);
}

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

Expand Down

0 comments on commit 21acb5c

Please sign in to comment.