Skip to content

Commit

Permalink
Integrate kendallbs PullRequest RicoSuter#1228
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanWetterActiware committed Nov 15, 2022
2 parents e045ae8 + 0a056a7 commit 604f240
Show file tree
Hide file tree
Showing 10 changed files with 890 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ src/packages/Newtonsoft.Json**
_ReSharper.Caches
/.nuke/temp
/artifacts

# Ignore files from JetBrainds Rider
/src/.idea/
387 changes: 387 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/AnyOfTests.cs

Large diffs are not rendered by default.

386 changes: 386 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/OneOfTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.1.24</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>
<Company />
</PropertyGroup>
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.1.24</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>
<Company />
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
11 changes: 11 additions & 0 deletions src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.1.24</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>
<Company />
</PropertyGroup>
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
13 changes: 1 addition & 12 deletions src/NJsonSchema.CodeGeneration/TypeResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public string TryResolve(JsonSchema schema, string typeNameHint)
/// <returns>The type name.</returns>
public virtual string GetOrGenerateTypeName(JsonSchema schema, string typeNameHint)
{
schema = RemoveNullability(schema).ActualSchema;

schema = schema.ActualSchema;
RegisterSchemaDefinitions(schema.Definitions);

if (!_generatedTypeNames.TryGetValue(schema, out var typeNames))
Expand Down Expand Up @@ -81,22 +80,12 @@ public void RegisterSchemaDefinitions(IDictionary<string, JsonSchema> definition
}
}

/// <summary>Removes a nullable oneOf reference if available.</summary>
/// <param name="schema">The schema.</param>
/// <returns>The actually resolvable schema</returns>
public virtual JsonSchema RemoveNullability(JsonSchema schema)
{
// TODO: Method on JsonSchema4?
return schema.OneOf.FirstOrDefault(o => !o.IsNullable(SchemaType.JsonSchema)) ?? schema;
}

/// <summary>Gets the actual schema (i.e. when not referencing a type schema or it is inlined)
/// and removes a nullable oneOf reference if available.</summary>
/// <param name="schema">The schema.</param>
/// <returns>The actually resolvable schema</returns>
public JsonSchema GetResolvableSchema(JsonSchema schema)
{
schema = RemoveNullability(schema);
return IsDefinitionTypeSchema(schema.ActualSchema) ? schema : schema.ActualSchema;
}

Expand Down
11 changes: 11 additions & 0 deletions src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.1.24</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>
<Company />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NJsonSchema\NJsonSchema.csproj" />
Expand Down
52 changes: 52 additions & 0 deletions src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,58 @@ public bool HasActualProperties
}
}

/// <summary>Gets all properties of this schema (i.e. all direct properties and properties from the schemas in allOf which do not have a type).</summary>
/// <remarks>Used for code generation.</remarks>
/// <exception cref="InvalidOperationException" accessor="get">Some properties are defined multiple times.</exception>
#if !LEGACY
private IReadOnlyDictionary<string, JsonSchemaProperty> GetActualProperties(bool includeInherited)
#else
private IDictionary<string, JsonSchemaProperty> GetActualProperties(bool includeInherited)
#endif
{
var ignoredSchema = includeInherited ? null : InheritedSchema;
var properties = Properties
.Union(AllOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(false)))
.Union(AnyOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(true)))
.Union(OneOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(true)))
.ToList();

// Collapse all duplicated properties, checking that the duplicated ones are compatible
var duplicatedProperties = properties
.GroupBy(p => p.Key)
.Where(g => g.Count() > 1)
.Select(g => g.ToList())
.ToList();
var invalidDuplicates = new List<string>();
foreach (var duped in duplicatedProperties)
{
// Make sure all the properties are the same type as each other so they are compatible primitive types
var toKeep = duped[0].Value;
if (duped.Any(dupe => dupe.Value.Type != toKeep.Type))
{
invalidDuplicates.Add(duped[0].Key);
continue;
}

// All good, so remove the duplicates here
foreach (var c in duped.Skip(1))
{
properties.Remove(c);
}
}

if (invalidDuplicates.Count > 0)
{
throw new InvalidOperationException("The properties " + string.Join(", ", invalidDuplicates.Select(key => "'" + key + "'")) + " are defined multiple times and are not the same type.");
}

#if !LEGACY
return new ReadOnlyDictionary<string, JsonSchemaProperty>(properties.ToDictionary(p => p.Key, p => p.Value));
#else
return new Dictionary<string, JsonSchemaProperty>(properties.ToDictionary(p => p.Key, p => p.Value));
#endif
}

/// <summary>Gets all properties of this schema (i.e. all direct properties and properties from the schemas in allOf which do not have a type).</summary>
/// <remarks>Used for code generation.</remarks>
/// <exception cref="InvalidOperationException" accessor="get">Some properties are defined multiple times.</exception>
Expand Down
14 changes: 14 additions & 0 deletions src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>10.1.24</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>
<Company />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net40'">
<DefineConstants>LEGACY</DefineConstants>
Expand Down

0 comments on commit 604f240

Please sign in to comment.