Skip to content

Commit

Permalink
Showing 11 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ public string Resolve(JsonSchema4 schema, bool isNullable, string typeNameHint,
if (type.HasFlag(JsonObjectType.String) && !schema.ActualTypeSchema.IsEnumeration)
return ResolveString(schema.ActualTypeSchema, isNullable, typeNameHint);

if (type.HasFlag(JsonObjectType.File))
if (schema.IsBinary)
return "byte[]";

// Type generating schemas
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.23</Version>
<Version>9.13.24</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.23</Version>
<Version>9.13.24</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Original file line number Diff line number Diff line change
@@ -103,7 +103,7 @@ private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfac
if (type.HasFlag(JsonObjectType.String) && !schema.ActualTypeSchema.IsEnumeration)
return ResolveString(schema.ActualTypeSchema, typeNameHint);

if (type.HasFlag(JsonObjectType.File))
if (schema.IsBinary)
return "any";

// Type generating schemas
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.23</Version>
<Version>9.13.24</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
16 changes: 16 additions & 0 deletions src/NJsonSchema.Tests/Generation/PrimitiveTypeGenerationTests.cs
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ public class Foo

public OffsetDateTime OffsetDateTime { get; set; }

public Instant Instant { get; set; }

public Duration Duration { get; set; }
}

@@ -128,6 +130,20 @@ public async Task When_property_is_offsetdatetime_then_schema_type_is_string()
Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["OffsetDateTime"].Format);
}

[Fact]
public async Task When_property_is_instant_then_schema_type_is_string()
{
//// Arrange


//// Act
var schema = await JsonSchema4.FromTypeAsync<Foo>();

//// Assert
Assert.Equal(JsonObjectType.String, schema.Properties["Instant"].Type);
Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["Instant"].Format);
}

[Fact]
public async Task When_property_is_duration_then_schema_type_is_string()
{
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.23</Version>
<Version>9.13.24</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
20 changes: 15 additions & 5 deletions src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
@@ -95,7 +95,8 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
type == typeof(DateTimeOffset) ||
type.FullName == "NodaTime.OffsetDateTime" ||
type.FullName == "NodaTime.LocalDateTime" ||
type.FullName == "NodaTime.ZonedDateTime")
type.FullName == "NodaTime.ZonedDateTime" ||
type.FullName == "NodaTime.Instant")
return JsonTypeDescription.Create(type, JsonObjectType.String, false, JsonFormatStrings.DateTime);

if (type == typeof(TimeSpan) ||
@@ -127,8 +128,17 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
return JsonTypeDescription.Create(type, JsonObjectType.None, isNullable, null);
}

if (IsFileType(type, parentAttributes))
return JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null);
if (IsBinary(type, parentAttributes))
{
if (settings.SchemaType == SchemaType.Swagger2)
{
return JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null);
}
else
{
return JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Binary);
}
}

var contract = settings.ResolveContract(type);
if (IsDictionaryType(type, parentAttributes) && contract is JsonDictionaryContract)
@@ -179,11 +189,11 @@ public virtual bool IsNullable(Type type, IEnumerable<Attribute> parentAttribute
return isValueType == false && settings.DefaultReferenceTypeNullHandling == ReferenceTypeNullHandling.Null;
}

/// <summary>Checks whether the given type is a file type.</summary>
/// <summary>Checks whether the given type is a file/binary type.</summary>
/// <param name="type">The type.</param>
/// <param name="parentAttributes">The parent attributes.</param>
/// <returns>true or false.</returns>
protected virtual bool IsFileType(Type type, IEnumerable<Attribute> parentAttributes)
protected virtual bool IsBinary(Type type, IEnumerable<Attribute> parentAttributes)
{
// TODO: Move all file handling to NSwag. How?

3 changes: 3 additions & 0 deletions src/NJsonSchema/JsonFormatStrings.cs
Original file line number Diff line number Diff line change
@@ -60,6 +60,9 @@ public static class JsonFormatStrings

/// <summary>Format for a byte if used with numeric type or for base64 encoded value otherwise.</summary>
public const string Byte = "byte";

/// <summary>Format for a binary value.</summary>
public const string Binary = "binary";

/// <summary>Format for a hostname (DNS name).</summary>
public const string Hostname = "hostname";
11 changes: 11 additions & 0 deletions src/NJsonSchema/JsonSchema4.cs
Original file line number Diff line number Diff line change
@@ -202,6 +202,17 @@ internal static JsonSchema4 FromJsonWithoutReferenceHandling(string data)
return schema;
}

/// <summary>Gets a value indicating whether the schema is binary (file or binary format).</summary>
[JsonIgnore]
public bool IsBinary
{
get
{
return Type.HasFlag(JsonObjectType.File) ||
(Type.HasFlag(JsonObjectType.String) && Format == JsonFormatStrings.Binary);
}
}

/// <summary>Gets the inherited/parent schema (most probable base schema in allOf).</summary>
/// <remarks>Used for code generation.</remarks>
[JsonIgnore]
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.23</Version>
<Version>9.13.24</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>

0 comments on commit 0bba26d

Please sign in to comment.