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

Added support for KnownType attributes that specify the name of a method #292

Merged
merged 1 commit into from
Jan 9, 2017
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
36 changes: 36 additions & 0 deletions src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,27 @@ public class Person
{
}

public class Pen
{
}

public class Pencil
{
}

[KnownType("GetKnownTypes")]
public class WritingInstrument
{
public static Type[] GetKnownTypes()
{
return new[] { typeof(Pen), typeof(Pencil) };
}
}

public class Container
{
public Person Person { get; set; }
public WritingInstrument WritingInstrument { get; set; }
}

[TestMethod]
Expand All @@ -37,5 +55,23 @@ public async Task When_KnownType_attribute_exists_then_specified_classes_are_als
//// Assert
Assert.IsTrue(schema.Definitions.Any(s => s.Key == "Teacher"));
}

public async Task ReproAsync()
{
var schema = await JsonSchema4.FromTypeAsync<Container>();
}
[TestMethod]
public async Task When_KnownType_attribute_includes_method_name_then_specified_classes_are_also_generated()
{
//// Arrange

//// Act
var schema = await JsonSchema4.FromTypeAsync<Container>();
var schemaData = schema.ToJson();

//// Assert
Assert.IsTrue(schema.Definitions.Any(s => s.Key == "Pen"));
Assert.IsTrue(schema.Definitions.Any(s => s.Key == "Pencil"));
}
}
}
29 changes: 25 additions & 4 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,32 @@ private async Task GenerateKnownTypesAsync(Type type, JsonSchemaResolver schemaR
{
foreach (dynamic knownTypeAttribute in type.GetTypeInfo().GetCustomAttributes().Where(a => a.GetType().Name == "KnownTypeAttribute"))
{
var typeDescription = JsonObjectTypeDescription.FromType(knownTypeAttribute.Type, null, Settings.DefaultEnumHandling);
var isIntegerEnum = typeDescription.Type == JsonObjectType.Integer;
if (knownTypeAttribute.Type != null)
{
var typeDescription = JsonObjectTypeDescription.FromType(knownTypeAttribute.Type, null, Settings.DefaultEnumHandling);
var isIntegerEnum = typeDescription.Type == JsonObjectType.Integer;

if (!schemaResolver.HasSchema(knownTypeAttribute.Type, isIntegerEnum))
await GenerateAsync(knownTypeAttribute.Type, schemaResolver).ConfigureAwait(false);
}
else if (!string.IsNullOrWhiteSpace(knownTypeAttribute.MethodName))
{
var methodInfo = type.GetRuntimeMethod((string)knownTypeAttribute.MethodName, new Type[0]);
var knownTypes = methodInfo.Invoke(null, null) as Type[];

foreach (var knownType in knownTypes)
{
var typeDescription = JsonObjectTypeDescription.FromType(knownType, null, Settings.DefaultEnumHandling);
var isIntegerEnum = typeDescription.Type == JsonObjectType.Integer;

if (!schemaResolver.HasSchema(knownTypeAttribute.Type, isIntegerEnum))
await GenerateAsync(knownTypeAttribute.Type, schemaResolver).ConfigureAwait(false);
if (!schemaResolver.HasSchema(knownType, isIntegerEnum))
await GenerateAsync(knownType, schemaResolver).ConfigureAwait(false);
}
}
else
{
throw new ArgumentException($"A KnownType attribute on {type.FullName} does not specify a type or a method name.", nameof(type));
}
}
}

Expand Down