Skip to content

Commit

Permalink
Merge pull request #857 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.10
  • Loading branch information
RicoSuter authored Dec 20, 2018
2 parents 0d6a4b3 + a38b65f commit 5d317aa
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 44 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,4 @@ Applications which use the library:

- [VisualJsonEditor](http://visualjsoneditor.org), a JSON schema based file editor for Windows.
- [NSwag](http://nswag.org): The Swagger API toolchain for .NET
- [SigSpec for SignalR Core](https://github.com/RSuter/SigSpec): Specification and code generator for SignalR Core.
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>
public override bool IsDefinitionTypeSchema(JsonSchema4 schema)
protected override bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
if ((schema.IsDictionary && !Settings.InlineNamedDictionaries) ||
(schema.IsArray && !Settings.InlineNamedArrays) ||
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.9</Version>
<Version>9.13.10</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
21 changes: 18 additions & 3 deletions src/NJsonSchema.CodeGeneration.TypeScript.Tests/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ public class DisplayValueDictionary : Dictionary<string, string>
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task When_property_uses_custom_dictionary_class_then_class_is_generated(bool inlineNamedDictionaries)
[InlineData(false, true)]
[InlineData(true, true)]
[InlineData(false, false)]
[InlineData(true, false)]
public async Task When_property_uses_custom_dictionary_class_then_class_is_generated(bool inlineNamedDictionaries, bool convertConstructorInterfaceData)
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<DictionaryContainer>();
Expand All @@ -218,6 +220,7 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener
{
TypeStyle = TypeScriptTypeStyle.Class,
NullValue = TypeScriptNullValue.Undefined,
ConvertConstructorInterfaceData = convertConstructorInterfaceData,
InlineNamedDictionaries = inlineNamedDictionaries
});
var code = codeGenerator.GenerateFile("Test");
Expand All @@ -228,6 +231,9 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener
Assert.Contains("foo: { [key: string] : string; };", code);
Assert.Contains(@"data[""Foo""] = {};", code);
Assert.Contains(@"this.foo = {};", code);

// for convertConstructorInterfaceData == true or false
Assert.DoesNotContain("new DisplayValueDictionary", code);
}
else
{
Expand All @@ -238,6 +244,15 @@ public async Task When_property_uses_custom_dictionary_class_then_class_is_gener
Assert.Contains(@"data[""Foo""] = this.foo ? this.foo.toJSON() : <any>undefined;", code);

Assert.Contains("foo: DisplayValueDictionary", code);

if (convertConstructorInterfaceData)
{
Assert.Contains("this.foo = data.foo && !(<any>data.foo).toJSON ? new DisplayValueDictionary(data.foo) : <DisplayValueDictionary>this.foo;", code);
}
else
{
Assert.DoesNotContain("new DisplayValueDictionary(data.foo)", code);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public sealed class EmptyClassInheritingDictionary : Dictionary<string, object>
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works(bool inlineNamedDictionaries)
[InlineData(false, true)]
[InlineData(true, true)]
[InlineData(false, false)]
[InlineData(true, false)]
public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritance_still_works(bool inlineNamedDictionaries, bool convertConstructorInterfaceData)
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<MyContainer>();
Expand All @@ -34,7 +36,8 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeScriptVersion = 2.0m,
InlineNamedDictionaries = inlineNamedDictionaries
InlineNamedDictionaries = inlineNamedDictionaries,
ConvertConstructorInterfaceData = convertConstructorInterfaceData
});

//// Act
Expand All @@ -58,6 +61,15 @@ public async Task When_empty_class_inherits_from_dictionary_then_allOf_inheritan

Assert.Contains("customDictionary: EmptyClassInheritingDictionary", code);
Assert.Contains("[key: string]: any;", code);

if (convertConstructorInterfaceData)
{
Assert.Contains("this.customDictionary = data.customDictionary && !(<any>data.customDictionary).toJSON ? new EmptyClassInheritingDictionary(data.customDictionary) : <EmptyClassInheritingDictionary>this.customDictionary;", code);
}
else
{
Assert.DoesNotContain("new EmptyClassInheritingDictionary(data.customDictionary)", code);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ private static object CreateModel(DataConversionParameters parameters)
Variable = parameters.Variable,
Value = parameters.Value,

HasDefaultValue = valueGenerator.GetDefaultValue(typeSchema,
HasDefaultValue = valueGenerator.GetDefaultValue(parameters.Schema,
parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues, parameters.Resolver) != null,
DefaultValue = valueGenerator.GetDefaultValue(typeSchema,
DefaultValue = valueGenerator.GetDefaultValue(parameters.Schema,
parameters.IsPropertyNullable, type, parameters.TypeNameHint, parameters.Settings.GenerateDefaultValues, parameters.Resolver),

Type = type,
Expand Down Expand Up @@ -148,10 +148,7 @@ private static bool IsNewableObject(JsonSchema4 schema, DataConversionParameters
if (schema.ActualTypeSchema.IsEnumeration)
return false;

schema = parameters.Resolver.RemoveNullability(schema);

return parameters.Resolver.IsTypeSchema(schema) ||
(schema.HasReference && parameters.Resolver.IsDefinitionTypeSchema(schema.ActualSchema));
return parameters.Resolver.GeneratesType(schema);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public bool SupportsConstructorConversion
}

/// <summary>Gets a value indicating whether the property type is an array.</summary>
public bool IsArray => _property.ActualTypeSchema.IsArray;
public bool IsArray => _resolver.GetResolvableSchema(_property).IsArray;

/// <summary>Gets a value indicating whether the property type is a dictionary.</summary>
public bool IsDictionary => _property.ActualTypeSchema.IsDictionary;
public bool IsDictionary => _resolver.GetResolvableSchema(_property).IsDictionary;

/// <summary>Gets the type of the array item.</summary>
public string ArrayItemType => _resolver.TryResolve(_property.ActualTypeSchema?.Item, PropertyName) ?? "any";
Expand Down Expand Up @@ -129,7 +129,7 @@ public string ConvertToClassCode
Variable = typeStyle == TypeScriptTypeStyle.Class ?
(IsReadOnly ? "(<any>this)." : "this.") + PropertyName : PropertyName + "_",
Value = "data[\"" + _property.Name + "\"]",
Schema = _property.ActualSchema,
Schema = _property,
IsPropertyNullable = _property.IsNullable(_settings.SchemaType),
TypeNameHint = PropertyName,
Resolver = _resolver,
Expand All @@ -154,7 +154,7 @@ public string ConvertToJavaScriptCode
{
Variable = "data[\"" + _property.Name + "\"]",
Value = typeStyle == TypeScriptTypeStyle.Class ? "this." + PropertyName : PropertyName + "_",
Schema = _property.ActualSchema,
Schema = _property,
IsPropertyNullable = _property.IsNullable(_settings.SchemaType),
TypeNameHint = PropertyName,
Resolver = _resolver,
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.9</Version>
<Version>9.13.10</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,7 +61,7 @@ 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>
public override bool IsDefinitionTypeSchema(JsonSchema4 schema)
protected override bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
if (schema.IsDictionary && !Settings.InlineNamedDictionaries)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver);
if (value == null)
{
schema = schema.ActualSchema;
if (schema != null && allowsNull == false)
{
if (schema.IsArray)
return "[]";
if (typeResolver.GeneratesType(schema) &&
!schema.ActualTypeSchema.IsEnumeration &&
!schema.ActualTypeSchema.IsAbstract)
{
return "new " + targetType + "()";
}

if (schema.IsDictionary)
return "{}";
if (schema.ActualTypeSchema.IsArray)
{
return "[]";
}

if (schema.Type.HasFlag(JsonObjectType.Object) &&
!schema.IsAbstract &&
!schema.IsAnyType)
if (schema.ActualTypeSchema.IsDictionary)
{
return "new " + targetType + "()";
return "{}";
}
}
}
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.9</Version>
<Version>9.13.10</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: 13 additions & 11 deletions src/NJsonSchema.CodeGeneration/TypeResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,26 @@ public JsonSchema4 RemoveNullability(JsonSchema4 schema)
/// and removes a nullable oneOf reference if available.</summary>
/// <param name="schema">The schema.</param>
/// <returns>The actually resolvable schema</returns>
protected JsonSchema4 GetResolvableSchema(JsonSchema4 schema)
public JsonSchema4 GetResolvableSchema(JsonSchema4 schema)
{
schema = RemoveNullability(schema);
return IsDefinitionTypeSchema(schema.ActualSchema) ? schema : schema.ActualSchema;
}

/// <summary>Checks whether the given schema should generate a type.</summary>
/// <summary>Checks whether the given schema generates a new type (e.g. class, enum, class with dictionary inheritance, etc.)
/// or is an inline type (e.g. string, number, etc.). Warning: Enum will also return true.</summary>
/// <param name="schema"></param>
/// <returns></returns>
public bool GeneratesType(JsonSchema4 schema)
{
schema = GetResolvableSchema(schema);
return schema.HasReference || (schema.IsObject && !schema.IsDictionary && !schema.IsAnyType);
}

/// <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 bool IsTypeSchema(JsonSchema4 schema)
protected virtual bool IsDefinitionTypeSchema(JsonSchema4 schema)
{
return !schema.IsTuple &&
!schema.IsDictionary &&
Expand All @@ -113,14 +123,6 @@ public 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
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.9</Version>
<Version>9.13.10</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
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.9</Version>
<Version>9.13.10</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 5d317aa

Please sign in to comment.