Skip to content

Commit

Permalink
Merge pull request #884 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.16
  • Loading branch information
RicoSuter committed Jan 23, 2019
2 parents d53b5ec + bee010a commit 53601fe
Show file tree
Hide file tree
Showing 24 changed files with 391 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_
{
//// Arrange
var json = @"{
'required': [ 'emptySchema' ],
'properties': {
'emptySchema': { 'type': 'array' }
}
Expand Down Expand Up @@ -560,6 +561,7 @@ public async Task When_patternProperties_is_set_with_string_value_type_then_corr
{
//// Arrange
var schemaJson = @"{
""required"": [ ""dict"" ],
""properties"": {
""dict"": {
""type"": ""object"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<None Remove="References\A.json" />
<None Remove="References\B.json" />
<None Remove="References\C.json" />
<None Remove="References\D.json" />
<None Remove="References\E.json" />
</ItemGroup>
<ItemGroup>
<Content Include="References\A.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\B.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\C.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\D.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="References\E.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
Expand Down
20 changes: 20 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/A.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "object",
"required": [
"signalPaths"
],
"description": "This is the class A",
"properties": {
"type": {
"$ref": "./C.json",
"description": "This is the type of A"
},
"signalPaths": {
"children": "array",
"items": {
"$ref": "./B.json",
"description": "This is a list of children"
}
}
}
}
18 changes: 18 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/B.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type": "object",
"description": "This is the class B",
"properties": {
"id": {
"type": "string",
"description": "This is the ID of B"
},
"type": {
"$ref": "./C.json",
"description": "This is the type of B"
},
"empty": {
"$ref": "./D.json",
"description": "This one should be of type D and not object"
}
}
}
8 changes: 8 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/C.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type":"string",
"description":"Possible types",
"enum":[
"First",
"Second"
]
}
4 changes: 4 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/D.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "object",
"description": "Possible types"
}
52 changes: 52 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/E.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"definitions": {
"B": {
"type": "object",
"description": "This is the class B",
"properties": {
"id": {
"type": "string",
"description": "This is the ID of B"
},
"type": {
"$ref": "#/definitions/C",
"description": "This is the type of B"
},
"empty": {
"$ref": "#/definitions/D",
"description": "This one should be of type D and not object"
}
}
},
"C": {
"type": "string",
"description": "Possible types",
"enum": [
"First",
"Second"
]
},
"D": {
"type": "object",
"description": "Possible types"
}
},
"type": "object",
"required": [
"signalPaths"
],
"description": "This is the class E",
"properties": {
"type": {
"$ref": "#/definitions/C",
"description": "This is the type of E"
},
"signalPaths": {
"children": "array",
"items": {
"$ref": "#/definitions/B",
"description": "This is a list of children"
}
}
}
}
55 changes: 55 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using NJsonSchema.CodeGeneration.CSharp;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;

namespace NJsonSchema.CodeGeneration.Tests.CSharp
{
public class ReferencesTest
{
[Fact]
public async Task When_ref_is_definitions_no_types_are_duplicated()
{
//// Arrange
var path = GetTestDirectory() + "/References/E.json";

//// Act
var schema = await JsonSchema4.FromFileAsync(path);
var generator = new CSharpGenerator(schema);

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

//// Assert
Assert.Contains("public enum C", code);
Assert.DoesNotContain("public enum C2", code);
}

[Fact]
public async Task When_ref_is_file_no_types_are_duplicated()
{
//// Arrange
var path = GetTestDirectory() + "/References/A.json";

//// Act
var schema = await JsonSchema4.FromFileAsync(path);
var generator = new CSharpGenerator(schema);

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

//// Assert
Assert.Contains("public enum C", code);
Assert.DoesNotContain("public enum C2", code);
}

private string GetTestDirectory()
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
var uri = new UriBuilder(codeBase);
return Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver);
if (value == null)
{
var isOptional = (schema as JsonProperty)?.IsRequired == false;

schema = schema.ActualSchema;
if (schema != null && allowsNull == false)
if (schema != null && allowsNull == false && isOptional == false)
{
if (schema.Type.HasFlag(JsonObjectType.Array) ||
schema.Type.HasFlag(JsonObjectType.Object))
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.15</Version>
<Version>9.13.16</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
@@ -1,6 +1,11 @@
using System;
using Newtonsoft.Json;
using NJsonSchema.Converters;
using NJsonSchema.Generation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -305,5 +310,45 @@ public async Task When_Knockout_class_is_generated_then_initializers_are_correct
//// Assert
Assert.DoesNotContain("let firstName_ = data[\"FirstName\"];", code);
}

[Fact]
public async Task When_GenerateConstructorInterface_is_disabled_then_data_is_not_checked_and_default_initialization_is_always_exectued()
{
// Assert
var schema = JsonSchema4.FromTypeAsync(
typeof(MyDerivedClass),
new JsonSchemaGeneratorSettings
{
GenerateAbstractProperties = true
}).Result;

var generator = new TypeScriptGenerator(schema);
generator.Settings.GenerateConstructorInterface = false;
generator.Settings.MarkOptionalProperties = true;
generator.Settings.TypeStyle = TypeScriptTypeStyle.Class;

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

// Assert
Assert.DoesNotContain("if (!data) {", output);
}

[JsonConverter(typeof(JsonInheritanceConverter))]
[KnownType(typeof(MyDerivedClass))]
public abstract class MyBaseClass
{
[Required]
public MyPropertyClass MyProperty { get; set; }
}

public sealed class MyDerivedClass : MyBaseClass
{
}

public sealed class MyPropertyClass
{
public string MyStringProperty { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public async Task When_property_is_dto_dictionary_then_assignment_may_create_new
{
//// Arrange
var json = @"{
""required"": [ ""resource"" ],
""properties"": {
""resource"": {
""type"": ""object"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Person

public Gender? GenderOrNull { get; set; }

[JsonProperty("address", Required = Required.Always)]
public Address Address { get; set; }

[CanBeNull]
Expand Down
Loading

0 comments on commit 53601fe

Please sign in to comment.