Skip to content

Commit

Permalink
Merge pull request #817 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.12.3
  • Loading branch information
RicoSuter authored Nov 19, 2018
2 parents 5cd46f9 + 363bed8 commit f41446b
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.12.2</Version>
<Version>9.12.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</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;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.12.2</Version>
<Version>9.12.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</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 @@ -49,6 +49,7 @@ public CodeGeneratorSettingsBase()
public string TemplateDirectory { get; set; }

/// <summary>Gets or sets the output language specific value generator.</summary>
[JsonIgnore]
public ValueGeneratorBase ValueGenerator { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.12.2</Version>
<Version>9.12.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
4 changes: 0 additions & 4 deletions src/NJsonSchema.Tests/Generation/KnownTypeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public async Task When_KnownType_attribute_exists_then_specified_classes_are_als
Assert.Contains(schema.Definitions, s => s.Key == "SpecialTeacher");
}

public async Task ReproAsync()
{
var schema = await JsonSchema4.FromTypeAsync<Container>();
}
[Fact]
public async Task When_KnownType_attribute_includes_method_name_then_specified_classes_are_also_generated()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ComplexArrayProperty()
{
//// Arrange
var data = @"{
array: [
persons: [
{
foo: ""bar"",
bar: ""foo""
Expand All @@ -58,11 +58,12 @@ public void ComplexArrayProperty()
//// Act
var schema = JsonSchema4.FromSampleJson(data);
var json = schema.ToJson();
var property = schema.Properties["array"].ActualTypeSchema;
var property = schema.Properties["persons"].ActualTypeSchema;

//// Assert
Assert.Equal(JsonObjectType.Array, property.Type);
Assert.Equal(3, property.Item.ActualSchema.Properties.Count);
Assert.True(schema.Definitions.ContainsKey("Person"));
}

[Fact]
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;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.12.2</Version>
<Version>9.12.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
13 changes: 12 additions & 1 deletion src/NJsonSchema/ConversionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static string ConvertToUpperCamelCase(string input, bool firstCharacterMu

return input;
}

/// <summary>Converts the string to a string literal which can be used in C# or TypeScript code.</summary>
/// <param name="input">The input.</param>
/// <returns>The literal.</returns>
Expand Down Expand Up @@ -127,6 +127,17 @@ public static string RemoveLineBreaks(string text)
.Trim('\n', '\t', ' ');
}

/// <summary>Singularizes the given noun in plural.</summary>
/// <param name="word">The plural noun.</param>
/// <returns>The singular noun.</returns>
public static string Singularize(string word)
{
if (word == "people")
return "Person";

return word.EndsWith("s") ? word.Substring(0, word.Length - 1) : word;
}

/// <summary>Add tabs to the given string.</summary>
/// <param name="input">The input.</param>
/// <param name="tabCount">The tab count.</param>
Expand Down
9 changes: 5 additions & 4 deletions src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
if (jsonSchemaTypeAttribute.IsNullableRaw.HasValue)
isNullable = jsonSchemaTypeAttribute.IsNullableRaw.Value;
}

var jsonSchemaAttribute = type.GetTypeInfo().GetCustomAttribute<JsonSchemaAttribute>() ??
parentAttributes?.OfType<JsonSchemaAttribute>().SingleOrDefault();

Expand Down Expand Up @@ -117,14 +117,15 @@ public virtual JsonTypeDescription GetDescription(Type type, IEnumerable<Attribu
if (type == typeof(byte[]))
return JsonTypeDescription.Create(type, JsonObjectType.String, isNullable, JsonFormatStrings.Byte);

if (type == typeof(JArray))
if (type.IsAssignableTo(nameof(JArray), TypeNameStyle.Name))
return JsonTypeDescription.Create(type, JsonObjectType.Array, isNullable, null);

if (type == typeof(JObject) ||
type == typeof(JToken) ||
if (type.IsAssignableTo(nameof(JToken), TypeNameStyle.Name) ||
type.FullName == "System.Dynamic.ExpandoObject" ||
type == typeof(object))
{
return JsonTypeDescription.Create(type, JsonObjectType.None, isNullable, null);
}

if (IsFileType(type, parentAttributes))
return JsonTypeDescription.Create(type, JsonObjectType.File, isNullable, null);
Expand Down
6 changes: 5 additions & 1 deletion src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,12 @@ private async Task<bool> TryHandleSpecialTypesAsync<TSchemaType>(Type type, TSch
return true;
}

if (type == typeof(JObject) || type == typeof(JToken) || type == typeof(object))
if (type.IsAssignableTo(nameof(JArray), TypeNameStyle.Name) == false &&
(type.IsAssignableTo(nameof(JToken), TypeNameStyle.Name) == true ||
type == typeof(object)))
{
return true;
}

return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/NJsonSchema/Generation/JsonSchemaGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ public JsonSchemaGeneratorSettings()

/// <summary>Gets or sets the contract resolver.</summary>
/// <remarks><see cref="DefaultPropertyNameHandling"/> will be ignored.</remarks>
[JsonIgnore]
public IContractResolver ContractResolver { get; set; }

/// <summary>Gets or sets the serializer settings.</summary>
/// <remarks><see cref="DefaultPropertyNameHandling"/>, <see cref="DefaultEnumHandling"/> and <see cref="ContractResolver"/> will be ignored.</remarks>
[JsonIgnore]
public JsonSerializerSettings SerializerSettings { get; set; }

/// <summary>Gets or sets the excluded type names (same as <see cref="JsonSchemaIgnoreAttribute"/>).</summary>
Expand Down Expand Up @@ -104,6 +106,7 @@ public JsonSchemaGeneratorSettings()
/// <summary>Gets the contract resolver.</summary>
/// <returns>The contract resolver.</returns>
/// <exception cref="InvalidOperationException">A setting is misconfigured.</exception>
[JsonIgnore]
public IContractResolver ActualContractResolver
{
get
Expand Down Expand Up @@ -139,6 +142,7 @@ public IContractResolver ActualContractResolver

/// <summary>Gets the serializer settings.</summary>
/// <exception cref="InvalidOperationException">A setting is misconfigured.</exception>
[JsonIgnore]
public JsonSerializerSettings ActualSerializerSettings
{
get
Expand Down
39 changes: 24 additions & 15 deletions src/NJsonSchema/Generation/SampleJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ public JsonSchema4 Generate(string json)
});

var schema = new JsonSchema4();
Generate(token, schema, schema);
Generate(token, schema, schema, "Anonymous");
return schema;
}

private void Generate(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema)
private void Generate(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema, string typeNameHint)
{
if (schema != rootSchema && token.Type == JTokenType.Object)
{
var referencedSchema = new JsonSchema4();
AddSchemaDefinition(rootSchema, referencedSchema);
AddSchemaDefinition(rootSchema, referencedSchema, typeNameHint);

schema.Reference = referencedSchema;
GenerateWithoutReference(token, referencedSchema, rootSchema);
GenerateWithoutReference(token, referencedSchema, rootSchema, typeNameHint);
return;
}

GenerateWithoutReference(token, schema, rootSchema);
GenerateWithoutReference(token, schema, rootSchema, typeNameHint);
}

private void GenerateWithoutReference(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema)
private void GenerateWithoutReference(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema, string typeNameHint)
{
if (token == null)
return;
Expand All @@ -60,7 +60,7 @@ private void GenerateWithoutReference(JToken token, JsonSchema4 schema, JsonSche
break;

case JTokenType.Array:
GenerateArray(token, schema, rootSchema);
GenerateArray(token, schema, rootSchema, typeNameHint);
break;

case JTokenType.Date:
Expand Down Expand Up @@ -123,31 +123,33 @@ private void GenerateObject(JToken token, JsonSchema4 schema, JsonSchema4 rootSc
foreach (var property in ((JObject)token).Properties())
{
var propertySchema = new JsonProperty();
Generate(property.Value, propertySchema, rootSchema);
var typeNameHint = ConversionUtilities.ConvertToUpperCamelCase(ConversionUtilities.Singularize(property.Name), true);

Generate(property.Value, propertySchema, rootSchema, typeNameHint);
schema.Properties[property.Name] = propertySchema;
}
}

private void GenerateArray(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema)
private void GenerateArray(JToken token, JsonSchema4 schema, JsonSchema4 rootSchema, string typeNameHint)
{
schema.Type = JsonObjectType.Array;

var itemSchemas = ((JArray)token).Select(item =>
{
var itemSchema = new JsonSchema4();
GenerateWithoutReference(item, itemSchema, rootSchema);
GenerateWithoutReference(item, itemSchema, rootSchema, typeNameHint);
return itemSchema;
}).ToList();

if (itemSchemas.Count == 0)
schema.Item = new JsonSchema4();
else if (itemSchemas.GroupBy(s => s.Type).Count() == 1)
MergeAndAssignItemSchemas(rootSchema, schema, itemSchemas);
MergeAndAssignItemSchemas(rootSchema, schema, itemSchemas, typeNameHint);
else
schema.Item = itemSchemas.First();
}

private void MergeAndAssignItemSchemas(JsonSchema4 rootSchema, JsonSchema4 schema, List<JsonSchema4> itemSchemas)
private void MergeAndAssignItemSchemas(JsonSchema4 rootSchema, JsonSchema4 schema, List<JsonSchema4> itemSchemas, string typeNameHint)
{
var firstItemSchema = itemSchemas.First();
var itemSchema = new JsonSchema4
Expand All @@ -161,13 +163,20 @@ private void MergeAndAssignItemSchemas(JsonSchema4 rootSchema, JsonSchema4 schem
itemSchema.Properties[property.Key] = property.First().Value;
}

AddSchemaDefinition(rootSchema, itemSchema);
AddSchemaDefinition(rootSchema, itemSchema, typeNameHint);
schema.Item = new JsonSchema4 { Reference = itemSchema };
}

private void AddSchemaDefinition(JsonSchema4 rootSchema, JsonSchema4 schema)
private void AddSchemaDefinition(JsonSchema4 rootSchema, JsonSchema4 schema, string typeNameHint)
{
rootSchema.Definitions["Anonymous" + (rootSchema.Definitions.Count + 1)] = schema;
if (string.IsNullOrEmpty(typeNameHint) || rootSchema.Definitions.ContainsKey(typeNameHint))
{
rootSchema.Definitions["Anonymous" + (rootSchema.Definitions.Count + 1)] = schema;
}
else
{
rootSchema.Definitions[typeNameHint] = schema;
}
}
}
}
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;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.12.2</Version>
<Version>9.12.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down

0 comments on commit f41446b

Please sign in to comment.