-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from bonsai-rx/discriminator-dev
Add support for JSON type discriminators
- Loading branch information
Showing
4 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NJsonSchema; | ||
using System.Threading.Tasks; | ||
|
||
namespace Bonsai.Sgen.Tests | ||
{ | ||
[TestClass] | ||
public class DiscriminatorGenerationTests | ||
{ | ||
[TestMethod] | ||
public async Task GenerateDiscriminatorSchema_SerializerAnnotationsDeclareKnownTypes() | ||
{ | ||
var schema = await JsonSchema.FromJsonAsync(@" | ||
{ | ||
""$schema"": ""http://json-schema.org/draft-04/schema#"", | ||
""type"": ""object"", | ||
""title"": ""Container"", | ||
""additionalProperties"": false, | ||
""properties"": { | ||
""Animal"": { | ||
""oneOf"": [ | ||
{ | ||
""$ref"": ""#/definitions/Animal"" | ||
}, | ||
{ | ||
""type"": ""null"" | ||
} | ||
] | ||
} | ||
}, | ||
""definitions"": { | ||
""Dog"": { | ||
""type"": ""object"", | ||
""additionalProperties"": false, | ||
""properties"": { | ||
""Bar"": { | ||
""type"": [ | ||
""null"", | ||
""string"" | ||
] | ||
} | ||
}, | ||
""allOf"": [ | ||
{ | ||
""$ref"": ""#/definitions/Animal"" | ||
} | ||
] | ||
}, | ||
""Animal"": { | ||
""type"": ""object"", | ||
""discriminator"": ""discriminator"", | ||
""x-abstract"": true, | ||
""additionalProperties"": false, | ||
""required"": [ | ||
""discriminator"" | ||
], | ||
""properties"": { | ||
""Foo"": { | ||
""type"": [ | ||
""null"", | ||
""string"" | ||
] | ||
}, | ||
""discriminator"": { | ||
""type"": ""string"" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
"); | ||
var generator = TestHelper.CreateGenerator(schema); | ||
var code = generator.GenerateFile(); | ||
Assert.IsTrue(code.Contains("[JsonInheritanceAttribute(\"Dog\", typeof(Dog))]")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters