Skip to content

Commit

Permalink
Merge pull request #854 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.7
  • Loading branch information
RicoSuter authored Dec 19, 2018
2 parents acc94e3 + 62bf979 commit 5490511
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 51 deletions.
12 changes: 12 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class MyContainer
public EmptyClassInheritingDictionary CustomDictionary { get; set; }
}

/// <summary>
/// Foobar.
/// </summary>
public sealed class EmptyClassInheritingDictionary : Dictionary<string, object>
{
}
Expand All @@ -35,8 +38,11 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan

//// Assert
var dschema = schema.Definitions["EmptyClassInheritingDictionary"];

Assert.Equal(0, dschema.AllOf.Count);
Assert.True(dschema.IsDictionary);
Assert.Contains("Foobar.", data);
Assert.Contains("Foobar.", code);

Assert.DoesNotContain("class CustomDictionary :", code);
Assert.Contains("public EmptyClassInheritingDictionary CustomDictionary", code);
Expand All @@ -50,6 +56,9 @@ public class ExceptionBase : Exception
public string Foo { get; set; }
}

/// <summary>
/// Foobar.
/// </summary>
public class MyException : ExceptionBase
{
public string Bar { get; set; }
Expand All @@ -73,6 +82,9 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge
var code = generator.GenerateFile();

//// Assert
Assert.Contains("Foobar.", data);
Assert.Contains("Foobar.", code);

Assert.Contains("class ExceptionBase : Exception", code);
Assert.Contains("class MyException : ExceptionBase", code);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>

<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.7.0" />

<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />

<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp2.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<Reference Condition="'$(TargetFramework)' == 'net461'" Include="System.ComponentModel.DataAnnotations"></Reference>
<Reference Condition="'$(TargetFramework)' == 'net461'" Include="System.ComponentModel.DataAnnotations">
</Reference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration.CSharp\NJsonSchema.CodeGeneration.CSharp.csproj" />
<ProjectReference Include="..\NJsonSchema.CodeGeneration\NJsonSchema.CodeGeneration.csproj" />
<ProjectReference Include="..\NJsonSchema\NJsonSchema.csproj" />
</ItemGroup>

</Project>
</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ public CSharpGeneratorSettings()
public bool InlineNamedTuples { get; set; }

/// <summary>Gets or sets a value indicating whether named/referenced arrays should be inlined or generated as class with array inheritance.</summary>
public bool InlineNamedArrays { get; internal set; }
public bool InlineNamedArrays { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public string Resolve(JsonSchema4 schema, bool isNullable, string typeNameHint,
/// <summary>Checks whether the given schema should generate a type.</summary>
/// <param name="schema">The schema.</param>
/// <returns>True if the schema should generate a type.</returns>
protected override bool IsTypeSchema(JsonSchema4 schema)
public override bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
if ((schema.IsDictionary && !Settings.InlineNamedDictionaries) ||
(schema.IsArray && !Settings.InlineNamedArrays) ||
Expand All @@ -113,7 +113,7 @@ protected override bool IsTypeSchema(JsonSchema4 schema)
return true;
}

return base.IsTypeSchema(schema);
return base.IsDefinitionTypeSchema(schema);
}

private string ResolveString(JsonSchema4 schema, bool isNullable, string typeNameHint)
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.5</Version>
<Version>9.13.7</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 @@ -226,9 +226,17 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener
if (inlineNamedDictionaries)
{
Assert.Contains("foo: { [key: string] : string; };", code);
Assert.Contains(@"data[""Foo""] = {};", code);
Assert.Contains(@"this.foo = {};", code);
}
else
{
Assert.DoesNotContain("this.foo = {};", code);
Assert.DoesNotContain("data[\"Foo\"] = {};", code);

Assert.Contains(@"this.foo = data[""Foo""] ? DisplayValueDictionary.fromJS(data[""Foo""]) : <any>undefined;", code);
Assert.Contains(@"data[""Foo""] = this.foo ? this.foo.toJSON() : <any>undefined;", code);

Assert.Contains("foo: DisplayValueDictionary", code);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class MyContainer
public EmptyClassInheritingDictionary CustomDictionary { get; set; }
}

/// <summary>
/// Foobar.
/// </summary>
public sealed class EmptyClassInheritingDictionary : Dictionary<string, object>
{
}
Expand All @@ -39,6 +42,7 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan

//// Assert
var dschema = schema.Definitions["EmptyClassInheritingDictionary"];

Assert.Equal(0, dschema.AllOf.Count);
Assert.True(dschema.IsDictionary);

Expand All @@ -49,6 +53,9 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan
}
else
{
Assert.Contains("Foobar.", data);
Assert.Contains("Foobar.", code);

Assert.Contains("customDictionary: EmptyClassInheritingDictionary", code);
Assert.Contains("[key: string]: any;", code);
}
Expand All @@ -61,6 +68,9 @@ public class ExceptionBase : Exception
public string Foo { get; set; }
}

/// <summary>
/// Foobar.
/// </summary>
public class MyException : ExceptionBase
{
public string Bar { get; set; }
Expand All @@ -84,6 +94,9 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge
var code = generator.GenerateFile();

//// Assert
Assert.Contains("Foobar.", data);
Assert.Contains("Foobar.", code);

Assert.Contains("class ExceptionBase extends Exception", code);
Assert.Contains("class MyException extends ExceptionBase", code);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net451</TargetFrameworks>

<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />

<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp2.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<Reference Condition="'$(TargetFramework)' == 'net451'" Include="System.ComponentModel.DataAnnotations"></Reference>
<Reference Condition="'$(TargetFramework)' == 'net451'" Include="System.ComponentModel.DataAnnotations">
</Reference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration.CSharp\NJsonSchema.CodeGeneration.CSharp.csproj" />
<ProjectReference Include="..\NJsonSchema.CodeGeneration.TypeScript\NJsonSchema.CodeGeneration.TypeScript.csproj" />
<ProjectReference Include="..\NJsonSchema.CodeGeneration\NJsonSchema.CodeGeneration.csproj" />
<ProjectReference Include="..\NJsonSchema\NJsonSchema.csproj" />
</ItemGroup>
</Project>
</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static object CreateModel(DataConversionParameters parameters)

Type = type,

IsNewableObject = IsNewableObject(typeSchema),
IsNewableObject = IsNewableObject(parameters.Schema, parameters),
IsDate = IsDate(typeSchema.Format, parameters.Settings.DateTimeType),
IsDateTime = IsDateTime(typeSchema.Format, parameters.Settings.DateTimeType),

Expand All @@ -65,17 +65,17 @@ private static object CreateModel(DataConversionParameters parameters)
DictionaryValueDefaultValue = dictionaryValueDefaultValue,
HasDictionaryValueDefaultValue = dictionaryValueDefaultValue != null,

IsDictionaryValueNewableObject = typeSchema.AdditionalPropertiesSchema != null && IsNewableObject(typeSchema.AdditionalPropertiesSchema),
IsDictionaryValueNewableObject = typeSchema.AdditionalPropertiesSchema != null && IsNewableObject(typeSchema.AdditionalPropertiesSchema, parameters),
IsDictionaryValueDate = IsDate(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType),
IsDictionaryValueDateTime = IsDateTime(typeSchema.AdditionalPropertiesSchema?.ActualSchema?.Format, parameters.Settings.DateTimeType),
IsDictionaryValueNewableArray = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true &&
IsNewableObject(typeSchema.AdditionalPropertiesSchema.Item),
IsNewableObject(typeSchema.AdditionalPropertiesSchema.Item, parameters),
DictionaryValueArrayItemType = typeSchema.AdditionalPropertiesSchema?.ActualSchema?.IsArray == true ?
parameters.Resolver.TryResolve(typeSchema.AdditionalPropertiesSchema.Item, "Anonymous") ?? "any" : "any",

IsArray = typeSchema.IsArray,
ArrayItemType = parameters.Resolver.TryResolve(typeSchema.Item, parameters.TypeNameHint) ?? "any",
IsArrayItemNewableObject = typeSchema.Item != null && IsNewableObject(typeSchema.Item),
IsArrayItemNewableObject = typeSchema.Item != null && IsNewableObject(typeSchema.Item, parameters),
IsArrayItemDate = IsDate(typeSchema.Item?.Format, parameters.Settings.DateTimeType),
IsArrayItemDateTime = IsDateTime(typeSchema.Item?.Format, parameters.Settings.DateTimeType),

Expand Down Expand Up @@ -143,11 +143,12 @@ private static bool IsDate(string format, TypeScriptDateTimeType type)
return false;
}

private static bool IsNewableObject(JsonSchema4 schema)
private static bool IsNewableObject(JsonSchema4 schema, DataConversionParameters parameters)
{
schema = schema.ActualSchema;
return (schema.Type.HasFlag(JsonObjectType.Object) || schema.Type == JsonObjectType.None)
&& !schema.IsAnyType && !schema.IsDictionary && !schema.IsEnumeration;
schema = parameters.Resolver.RemoveNullability(schema);

return parameters.Resolver.IsTypeSchema(schema) ||
(schema.HasReference && parameters.Resolver.IsDefinitionTypeSchema(schema.ActualSchema));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public string Inheritance
public bool GenerateConstructorInterface => _settings.GenerateConstructorInterface;

/// <summary>Gets or sets a value indicating whether POJO objects in the constructor data are converted to DTO instances (default: true).</summary>
public bool ConvertConstructorInterfaceData => _settings.ConvertConstructorInterfaceData;
public bool ConvertConstructorInterfaceData => _settings.ConvertConstructorInterfaceData && Properties.Any(p => p.SupportsConstructorConversion);

/// <summary>Gets the null value.</summary>
public string NullValue => _settings.NullValue.ToString().ToLowerInvariant();
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.5</Version>
<Version>9.13.7</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 @@ -61,14 +61,14 @@ public bool SupportsConstructorConversion(JsonSchema4 schema)
/// <summary>Checks whether the given schema should generate a type.</summary>
/// <param name="schema">The schema.</param>
/// <returns>True if the schema should generate a type.</returns>
protected override bool IsTypeSchema(JsonSchema4 schema)
public override bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
if (schema.IsDictionary && !Settings.InlineNamedDictionaries)
{
return true;
}

return base.IsTypeSchema(schema);
return base.IsDefinitionTypeSchema(schema);
}

private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfacePrefix)
Expand Down Expand Up @@ -125,7 +125,7 @@ private string Resolve(JsonSchema4 schema, string typeNameHint, bool addInterfac
return $"{{ [key: {keyType}] : {valueType}; }}";
}

return (addInterfacePrefix && SupportsConstructorConversion(schema) ? "I" : "") +
return (addInterfacePrefix && !schema.ActualTypeSchema.IsEnumeration && SupportsConstructorConversion(schema) ? "I" : "") +
GetOrGenerateTypeName(schema, typeNameHint);
}

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.5</Version>
<Version>9.13.7</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
17 changes: 13 additions & 4 deletions src/NJsonSchema.CodeGeneration/TypeResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void RegisterSchemaDefinitions(IDictionary<string, JsonSchema4> definitio
{
var schema = pair.Value.ActualSchema;

if (IsTypeSchema(schema))
if (IsDefinitionTypeSchema(schema))
{
GetOrGenerateTypeName(schema, pair.Key);
}
Expand All @@ -83,8 +83,9 @@ public void RegisterSchemaDefinitions(IDictionary<string, JsonSchema4> definitio
/// <summary>Removes a nullable oneOf reference if available.</summary>
/// <param name="schema">The schema.</param>
/// <returns>The actually resolvable schema</returns>
protected JsonSchema4 RemoveNullability(JsonSchema4 schema)
public JsonSchema4 RemoveNullability(JsonSchema4 schema)
{
// TODO: Method on JsonSchema4?
return schema.OneOf.FirstOrDefault(o => !o.IsNullable(SchemaType.JsonSchema)) ?? schema;
}

Expand All @@ -95,13 +96,13 @@ protected JsonSchema4 RemoveNullability(JsonSchema4 schema)
protected JsonSchema4 GetResolvableSchema(JsonSchema4 schema)
{
schema = RemoveNullability(schema);
return IsTypeSchema(schema.ActualSchema) ? schema : schema.ActualSchema;
return IsDefinitionTypeSchema(schema.ActualSchema) ? schema : schema.ActualSchema;
}

/// <summary>Checks whether the given schema should generate a type.</summary>
/// <param name="schema">The schema.</param>
/// <returns>True if the schema should generate a type.</returns>
protected virtual bool IsTypeSchema(JsonSchema4 schema)
public bool IsTypeSchema(JsonSchema4 schema)
{
return !schema.IsTuple &&
!schema.IsDictionary &&
Expand All @@ -112,6 +113,14 @@ protected virtual bool IsTypeSchema(JsonSchema4 schema)
schema.Type.HasFlag(JsonObjectType.Object));
}

/// <summary>Checks whether the given schema from definitions should generate a type.</summary>
/// <param name="schema">The schema.</param>
/// <returns>True if the schema should generate a type.</returns>
public virtual bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
return IsTypeSchema(schema);
}

/// <summary>Resolves the type of the dictionary value of the given schema (must be a dictionary schema).</summary>
/// <param name="schema">The schema.</param>
/// <param name="fallbackType">The fallback type (e.g. 'object').</param>
Expand Down
Loading

0 comments on commit 5490511

Please sign in to comment.