Skip to content

Commit

Permalink
Array of nullable items are now automatically detected (#1299)
Browse files Browse the repository at this point in the history
* Array of nullable items are now automatically detected

There is no need to use [ItemsCanBeNull] in an NRT context.
Fixes RicoSuter/Namotion.Reflection#36

* Update package

* u

* Add list test

* Use latest SDK

* Update csprojs

* Inline icon

* Upgrade to .NET 5

* Fix tests

Co-authored-by: Rico Suter <mail@rsuter.com>
  • Loading branch information
jeremyVignelles and RicoSuter authored Feb 4, 2021
1 parent b851592 commit 61d597b
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 38 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
displayName: 'Install .NET Core SDK'
inputs:
packageType: 'sdk'
version: '2.2.203'
version: '5.0.102'

# Patch preview project versions (only when on master branch)
- task: CmdLine@2
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.Benchmark/NJsonSchema.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Schema.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0;net452</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1998,1591</NoWarn>
Expand Down Expand Up @@ -33,10 +33,13 @@
<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" />

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" Condition="'$(TargetFramework)' == 'netcoreapp5.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.2" Condition="'$(TargetFramework)' == 'net452'" />

<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">
<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp5.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<Reference Condition="'$(TargetFramework)' == 'net452'" Include="System.ComponentModel.DataAnnotations">
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<Version>10.3.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NJsonSchema.org</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/NuGetIcon.png</PackageIconUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Company />
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
Expand Down Expand Up @@ -95,4 +95,7 @@
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<None Include="NuGetIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using NJsonSchema.CodeGeneration.TypeScript;
using NJsonSchema.Converters;
using Xunit;

using System.IO;
using System.Reflection;
using System.CodeDom.Compiler;
Expand Down Expand Up @@ -310,20 +309,17 @@ public async Task Subtypes_are_serialized_with_correct_discriminator()

private Assembly Compile(string code)
{

CSharpCompilation compilation = CSharpCompilation.Create("assemblyName")
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));

#if NET452
compilation = compilation.AddReferences(
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(JsonConvert).Assembly.Location),
MetadataReference.CreateFromFile(typeof(GeneratedCodeAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Linq.Expressions.Expression).Assembly.Location));
#endif

#if NETCOREAPP2_0
#else
var coreDir = Directory.GetParent(typeof(Enumerable).GetTypeInfo().Assembly.Location);
compilation = compilation.AddReferences(
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0;net452</TargetFrameworks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable>
Expand All @@ -10,12 +10,12 @@
<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.6.1" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" Condition="'$(TargetFramework)' == 'netcoreapp5.0'" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="1.3.2" Condition="'$(TargetFramework)' == 'net452'" />

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

<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp2.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<PackageReference Condition="'$(TargetFramework)' == 'netcoreapp5.0'" Include="System.ComponentModel.Annotations" Version="4.4.0" />
<Reference Condition="'$(TargetFramework)' == 'net452'" Include="System.ComponentModel.DataAnnotations"></Reference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0;net452</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1998,1591</NoWarn>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<Version>10.3.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NJsonSchema.org</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<PackageIconUrl />
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/NuGetIcon.png</PackageIconUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Company />
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down Expand Up @@ -43,4 +43,7 @@
<ItemGroup>
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
<ItemGroup>
<None Include="NuGetIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<Version>10.3.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NJsonSchema.org</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/NuGetIcon.png</PackageIconUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Company />
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
Expand All @@ -33,4 +33,7 @@
<ItemGroup>
<ProjectReference Include="..\NJsonSchema\NJsonSchema.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="NuGetIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
Binary file added src/NJsonSchema.CodeGeneration/NuGetIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.Demo/NJsonSchema.Demo.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<GenerateProgramFile>true</GenerateProgramFile>
<ApplicationIcon />
Expand Down
28 changes: 28 additions & 0 deletions src/NJsonSchema.Tests/Generation/ArrayGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,34 @@ public async Task When_property_is_JArray_then_schema_with_any_array_is_generate
Assert.True(arrayProperty.Item.ActualTypeSchema.IsAnyType);
}

#nullable enable
public class ClassWithArrayOfNullable
{
public string?[] Array { get; set; } = new string?[0];

public List<string?> List { get; set; } = new List<string?>();
}
#nullable restore

[Fact]
public async Task When_property_is_Array_of_nullable_then_schema_with_array_of_nullable_is_generated()
{
//// Act
var schema = JsonSchema.FromType<ClassWithArrayOfNullable>(new JsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
var json = schema.ToJson();

//// Assert
Assert.Equal(2, schema.ActualProperties.Count);

var arrayProperty = schema.ActualProperties["Array"].ActualTypeSchema;
Assert.Equal(JsonObjectType.Array, arrayProperty.Type);
Assert.True(arrayProperty.Item.IsNullableRaw);

var listProperty = schema.ActualProperties["List"].ActualTypeSchema;
Assert.Equal(JsonObjectType.Array, listProperty.Type);
Assert.True(listProperty.Item.IsNullableRaw);
}

public class ListContainer
{
public SomeModelCollectionResponse Response { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/NJsonSchema.Tests/NJsonSchema.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0;net46;net452</TargetFrameworks>

<IsPackable>false</IsPackable>
<LangVersion>8</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml.Tests/NJsonSchema.Yaml.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net46;net452</TargetFrameworks>
<TargetFrameworks>netcoreapp5.0;net46;net452</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
Expand Down
7 changes: 5 additions & 2 deletions src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<Version>10.3.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NJsonSchema.org</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/NuGetIcon.png</PackageIconUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Company />
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
Expand All @@ -21,4 +21,7 @@
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
</ItemGroup>
<ItemGroup>
<None Include="NuGetIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
Binary file added src/NJsonSchema.Yaml/NuGetIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,18 @@ protected virtual void GenerateArray<TSchemaType>(
typeDescription.ApplyType(schema);

var jsonSchemaAttribute = contextualType.GetTypeAttribute<JsonSchemaAttribute>();
var itemType = jsonSchemaAttribute?.ArrayItem ?? contextualType.OriginalType.GetEnumerableItemType();
var itemType = jsonSchemaAttribute?.ArrayItem.ToContextualType() ?? contextualType.EnumerableItemType;
if (itemType != null)
{
var contextualItemType = itemType.ToContextualType();
var itemIsNullable = contextualType.GetContextAttribute<ItemsCanBeNullAttribute>() != null ||
contextualItemType.Nullability == Nullability.Nullable;
itemType.Nullability == Nullability.Nullable;

schema.Item = GenerateWithReferenceAndNullability<JsonSchema>(
contextualItemType, itemIsNullable, schemaResolver, (itemSchema, typeSchema) =>
itemType, itemIsNullable, schemaResolver, (itemSchema, typeSchema) =>
{
if (Settings.GenerateXmlObjects)
{
itemSchema.GenerateXmlObjectForItemType(contextualItemType);
itemSchema.GenerateXmlObjectForItemType(itemType);
}
});

Expand Down Expand Up @@ -904,7 +903,7 @@ private void GenerateProperties(Type type, JsonSchema schema, JsonSchemaResolver
}
else
{
// TODO: Remove this hacky code (used to support serialization of exceptions and restore the old behavior [pre 9.x])
// TODO: Remove this hacky code (used to support serialization of exceptions and restore the old behavior [pre 9.x])
foreach (var memberInfo in contextualMembers.Where(m => allowedProperties == null || allowedProperties.Contains(m.Name)))
{
var attribute = memberInfo.GetContextAttribute<JsonPropertyAttribute>();
Expand Down
9 changes: 6 additions & 3 deletions src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<Version>10.3.3</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>http://NJsonSchema.org</PackageProjectUrl>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>../NJsonSchema.snk</AssemblyOriginatorKeyFile>
<Authors>Rico Suter</Authors>
<PackageIconUrl>https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/NuGetIcon.png</PackageIconUrl>
<PackageIcon>NuGetIcon.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Company />
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
Expand All @@ -28,7 +28,7 @@
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Namotion.Reflection" Version="1.0.15" />
<PackageReference Include="Namotion.Reflection" Version="1.0.18" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
Expand All @@ -44,4 +44,7 @@
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<None Include="NuGetIcon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
Binary file added src/NJsonSchema/NuGetIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 61d597b

Please sign in to comment.