Skip to content

Commit

Permalink
Release v9.13.25 (#930)
Browse files Browse the repository at this point in the history
* Add test #929

* Fix property type resolution for any schemas in definitions, RicoSuter/NSwag#2028

* Test and potential Fix for Interfaces. (#909) (#921)

* Add legacy support, #921

* v9.13.25
  • Loading branch information
RicoSuter authored Mar 19, 2019
1 parent 0bba26d commit d4f150b
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 9 deletions.
44 changes: 44 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,49 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge
Assert.Contains("class ExceptionBase : Exception", code);
Assert.Contains("class MyException : ExceptionBase", code);
}

[Fact]
public async Task When_property_references_any_schema_with_inheritance_then_property_type_is_correct()
{
//// Arrange
var json = @"{
""type"": ""object"",
""properties"": {
""dog"": {
""$ref"": ""#/definitions/Dog""
}
},
""definitions"": {
""Pet"": {
""type"": ""object"",
""properties"": {
""name"": {
""type"": ""string""
}
}
},
""Dog"": {
""title"": ""Dog"",
""description"": """",
""allOf"": [
{
""$ref"": ""#/definitions/Pet""
},
{
""type"": ""object""
}
]
}
}
}";
var schema = await JsonSchema4.FromJsonAsync(json);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco });

//// Act
var code = generator.GenerateFile();

//// Assert
Assert.Contains("public Dog Dog { get; set; }", code);
}
}
}
62 changes: 62 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/InterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Threading.Tasks;
using NJsonSchema.Generation;
using Xunit;

namespace NJsonSchema.CodeGeneration.CSharp.Tests
{
public class InterfaceTests
{
public interface IPerson
{
string LastName { get; set; }
string FirstName { get; set; }
}

public class Person : IPerson
{
public string LastName { get; set; }
public string FirstName { get; set; }
}

[Fact]
public async Task When_interface_has_properties_then_properties_are_included_in_schema()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Person>(new JsonSchemaGeneratorSettings());

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
});
var code = generator.GenerateFile("Person");

//// Assert
Assert.Equal(2, schema.Properties.Count);
Assert.Contains("public string LastName { get; set; }\n", code);
Assert.Contains("public string FirstName { get; set; }\n", code);
}

[Fact]
public async Task When_class_implements_interface_then_properties_are_included_in_schema()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Person>(new JsonSchemaGeneratorSettings());

//// Act
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco,
SchemaType = SchemaType.Swagger2
});
var code = generator.GenerateFile("Person");

//// Assert
Assert.Equal(2, schema.Properties.Count);
Assert.Contains("public string LastName { get; set; }\n", code);
Assert.Contains("public string FirstName { get; set; }\n", code);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public string Resolve(JsonSchema4 schema, bool isNullable, string typeNameHint,

// Primitive schemas (no new type)

if (schema.ActualTypeSchema.IsAnyType)
if (schema.ActualTypeSchema.IsAnyType && !schema.HasReference)
return "object";

var type = schema.ActualTypeSchema.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.24</Version>
<Version>9.13.25</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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.24</Version>
<Version>9.13.25</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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfac

// Primitive schemas (no new type)

if (schema.ActualTypeSchema.IsAnyType)
if (schema.ActualTypeSchema.IsAnyType && !schema.HasReference)
return "any";

var type = schema.ActualTypeSchema.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.24</Version>
<Version>9.13.25</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>
Expand Down
24 changes: 24 additions & 0 deletions src/NJsonSchema.Tests/Generation/EnumGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,29 @@ public async Task When_string_enum_property_has_default_then_default_is_converte
//// Assert
Assert.Equal("C", schema.Properties["MyEnumeration"].Default);
}

public class Party
{
public MyEnumeration? EnumValue { get; set; }

public bool ShouldSerializeEnumValue()
{
return EnumValue.HasValue;
}
}

[Fact]
public async Task When_enum_property_has_should_serialize_then_no_npe()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Party>(new JsonSchemaGeneratorSettings());

//// Act
var json = schema.ToJson();

//// Assert
Assert.True(schema.Properties.ContainsKey("EnumValue"));
Assert.NotNull(json);
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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.24</Version>
<Version>9.13.25</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>
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,10 @@ private async Task GeneratePropertiesAsync(Type type, JsonSchema4 schema, JsonSc
var info = propertiesAndFields.FirstOrDefault(p => p.Name == property.UnderlyingName);
var propertyInfo = info as PropertyInfo;
#if !LEGACY
if (Settings.GenerateAbstractProperties || propertyInfo == null ||
if (Settings.GenerateAbstractProperties || propertyInfo == null || propertyInfo.DeclaringType.GetTypeInfo().IsInterface ||
(propertyInfo.GetMethod?.IsAbstract != true && propertyInfo.SetMethod?.IsAbstract != true))
#else
if (Settings.GenerateAbstractProperties || propertyInfo == null ||
if (Settings.GenerateAbstractProperties || propertyInfo == null || propertyInfo.DeclaringType.GetTypeInfo().IsInterface ||
(propertyInfo.GetGetMethod()?.IsAbstract != true && propertyInfo.GetSetMethod()?.IsAbstract != true))
#endif
{
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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.24</Version>
<Version>9.13.25</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>
Expand Down

0 comments on commit d4f150b

Please sign in to comment.