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 #113 from gregsdennis/schemagen-v1.4.0
SchemaGen v1.4.0
- Loading branch information
Showing
10 changed files
with
216 additions
and
11 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,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using NUnit.Framework; | ||
|
||
namespace Json.Schema.Generation.Tests { | ||
public class AttributeHandlerTests { | ||
|
||
[Test] | ||
public void DirectAttributeHandler() { | ||
JsonSchema expected = new JsonSchemaBuilder() | ||
.Type(SchemaValueType.Object) | ||
.Properties( | ||
("MyProperty", new JsonSchemaBuilder().Type(SchemaValueType.String).MaxLength(AttributeWithDirectHandler.MaxLength)) | ||
); | ||
|
||
JsonSchema actual = new JsonSchemaBuilder().FromType<TypeWithCustomAttribute1>(); | ||
|
||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
|
||
[Test] | ||
public void IndirectAttributeHandler() { | ||
AttributeHandler.RemoveHandler<CustomAttributeHandler>(); | ||
AttributeHandler.AddHandler<CustomAttributeHandler>(); | ||
|
||
JsonSchema expected = new JsonSchemaBuilder() | ||
.Type(SchemaValueType.Object) | ||
.Properties( | ||
("MyProperty", new JsonSchemaBuilder().Type(SchemaValueType.String).MaxLength(AttributeWithIndirectHandler.MaxLength)) | ||
); | ||
|
||
JsonSchema actual = new JsonSchemaBuilder().FromType<TypeWithCustomAttribute2>(); | ||
|
||
Assert.AreEqual(expected, actual); | ||
} | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Property)] | ||
public class AttributeWithDirectHandler : Attribute, IAttributeHandler { | ||
|
||
public const uint MaxLength = 100; | ||
|
||
void IAttributeHandler.AddConstraints(SchemaGeneratorContext context) { | ||
if (context.Attributes.Any(x => x.GetType() == typeof(AttributeWithDirectHandler))) { | ||
context.Intents.Add(new Intents.MaxLengthIntent(MaxLength)); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Property)] | ||
public class AttributeWithIndirectHandler : Attribute { | ||
|
||
public const uint MaxLength = 200; | ||
|
||
} | ||
|
||
|
||
public class CustomAttributeHandler : IAttributeHandler { | ||
void IAttributeHandler.AddConstraints(SchemaGeneratorContext context) { | ||
if (context.Attributes.Any(x => x.GetType() == typeof(AttributeWithIndirectHandler))) { | ||
context.Intents.Add(new Intents.MaxLengthIntent(AttributeWithIndirectHandler.MaxLength)); | ||
} | ||
} | ||
} | ||
|
||
|
||
public class TypeWithCustomAttribute1 { | ||
|
||
[AttributeWithDirectHandler] | ||
public string MyProperty { get; set; } | ||
|
||
} | ||
|
||
|
||
public class TypeWithCustomAttribute2 { | ||
|
||
[AttributeWithIndirectHandler] | ||
public string MyProperty { get; set; } | ||
|
||
} | ||
|
||
} | ||
} |
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,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using NUnit.Framework; | ||
|
||
namespace Json.Schema.Generation.Tests | ||
{ | ||
public class PropertyOrderTests | ||
{ | ||
private class SpecifiedOrder | ||
{ | ||
public int Second { get; set; } | ||
public int First { get; set; } | ||
} | ||
|
||
[Test] | ||
public void PropertiesAsDeclared() | ||
{ | ||
var config = new SchemaGeneratorConfiguration | ||
{ | ||
PropertyOrder = PropertyOrder.AsDeclared | ||
}; | ||
|
||
JsonSchema schema = new JsonSchemaBuilder() | ||
.FromType<SpecifiedOrder>(config); | ||
|
||
var properties = schema.Keywords.OfType<PropertiesKeyword>().Single(); | ||
|
||
Assert.AreEqual(nameof(SpecifiedOrder.Second), properties.Properties.Keys.First()); | ||
Assert.AreEqual(nameof(SpecifiedOrder.First), properties.Properties.Keys.Last()); | ||
} | ||
|
||
[Test] | ||
public void PropertiesByName() | ||
{ | ||
var config = new SchemaGeneratorConfiguration | ||
{ | ||
PropertyOrder = PropertyOrder.ByName | ||
}; | ||
|
||
JsonSchema schema = new JsonSchemaBuilder() | ||
.FromType<SpecifiedOrder>(config); | ||
|
||
var properties = schema.Keywords.OfType<PropertiesKeyword>().Single(); | ||
|
||
Assert.AreEqual(nameof(SpecifiedOrder.First), properties.Properties.Keys.First()); | ||
Assert.AreEqual(nameof(SpecifiedOrder.Second), properties.Properties.Keys.Last()); | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
namespace Json.Schema.Generation | ||
{ | ||
/// <summary> | ||
/// Indicates the sequence in which properties will be listed in the schema. | ||
/// </summary> | ||
public enum PropertyOrder | ||
{ | ||
/// <summary> | ||
/// Properties will be listed in the order they're declared in code. | ||
/// </summary> | ||
AsDeclared, | ||
/// <summary> | ||
/// Properties will be sorted by name, case-insensitive. | ||
/// </summary> | ||
ByName | ||
} | ||
} |
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