Skip to content

Commit

Permalink
Fix struct handling, closes #1624
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Sep 26, 2023
1 parent e1b561f commit 83950bb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/NJsonSchema.NewtonsoftJson.Tests/Generation/StructTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Newtonsoft.Json;
using NJsonSchema.Annotations;
using NJsonSchema.NewtonsoftJson.Generation;
using Xunit;

namespace NJsonSchema.NewtonsoftJson.Tests.Generation
{
public class StructTests
{
[JsonSchema("UserDefinedStruct")]
public struct UserDefinedStruct
{
}

public class UserDefinedClass
{
[JsonProperty]
public readonly UserDefinedStruct NonNullableField;

[JsonProperty]
public readonly UserDefinedStruct? NullableField;
}

[Fact]
public void Should_have_a_shared_struct_schema()
{
//// Arrange

//// Act
var schema = NewtonsoftJsonSchemaGenerator.FromType<UserDefinedClass>();
var data = schema.ToJson();

//// Assert
Assert.Equal(1, schema.Definitions.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net462</TargetFrameworks>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
<DocumentationFile>bin\Debug\$(TargetFramework)\NJsonSchema.Tests.xml</DocumentationFile>
<NoWarn>$(NoWarn),618,1587,1998,1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<Content Include="References\**\*.json" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
<PackageReference Include="NodaTime" Version="3.1.9" />
<Reference Condition="'$(TargetFramework)' == 'net462'" Include="System.ComponentModel.DataAnnotations"></Reference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

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

</Project>
19 changes: 19 additions & 0 deletions src/NJsonSchema.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "..\build\_build.c
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NJsonSchema.NewtonsoftJson", "NJsonSchema.NewtonsoftJson\NJsonSchema.NewtonsoftJson.csproj", "{A9C2A9CD-44F6-4A21-9D72-00CF5BE0A36F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NJsonSchema.NewtonsoftJson.Tests", "NJsonSchema.NewtonsoftJson.Tests\NJsonSchema.NewtonsoftJson.Tests.csproj", "{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -269,6 +271,22 @@ Global
{A9C2A9CD-44F6-4A21-9D72-00CF5BE0A36F}.Release|x64.Build.0 = Release|Any CPU
{A9C2A9CD-44F6-4A21-9D72-00CF5BE0A36F}.Release|x86.ActiveCfg = Release|Any CPU
{A9C2A9CD-44F6-4A21-9D72-00CF5BE0A36F}.Release|x86.Build.0 = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|ARM.ActiveCfg = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|ARM.Build.0 = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|x64.ActiveCfg = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|x64.Build.0 = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|x86.ActiveCfg = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Debug|x86.Build.0 = Debug|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|Any CPU.Build.0 = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|ARM.ActiveCfg = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|ARM.Build.0 = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x64.ActiveCfg = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x64.Build.0 = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x86.ActiveCfg = Release|Any CPU
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -283,6 +301,7 @@ Global
{E59CE32B-181F-4AAE-BF62-772AEEFC3177} = {785552E3-F3BB-4AEB-B5CF-819AE38AEA8F}
{990EF464-C967-4E08-8C3D-0568A47B6D2A} = {785552E3-F3BB-4AEB-B5CF-819AE38AEA8F}
{8E4E5A64-B5B7-4718-A92F-CB6B08512264} = {863B2D88-A0BD-4466-8583-AAD0B8D3F182}
{AFA1E1E7-37F9-4958-B0E3-ADB12F53E563} = {785552E3-F3BB-4AEB-B5CF-819AE38AEA8F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9D5EDC80-5611-493B-804B-8B364816952B}
Expand Down
6 changes: 3 additions & 3 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public virtual void Generate<TSchemaType>(TSchemaType schema, ContextualType con

if (schemaResolver.RootObject == schema)
{
schema.Title = Settings.SchemaNameGenerator.Generate(typeDescription.ContextualType.OriginalType);
schema.Title = Settings.SchemaNameGenerator.Generate(typeDescription.ContextualType.Type);
}

if (typeDescription.Type.IsObject())
Expand All @@ -167,9 +167,9 @@ public virtual void Generate<TSchemaType>(TSchemaType schema, ContextualType con
}
else
{
if (schemaResolver.HasSchema(typeDescription.ContextualType.OriginalType, false))
if (schemaResolver.HasSchema(typeDescription.ContextualType.Type, false))
{
schema.Reference = schemaResolver.GetSchema(typeDescription.ContextualType.OriginalType, false);
schema.Reference = schemaResolver.GetSchema(typeDescription.ContextualType.Type, false);
}
else if (schema.GetType() == typeof(JsonSchema))
{
Expand Down

0 comments on commit 83950bb

Please sign in to comment.