From b5f0b8e6937d008288679169a9a9b50ca900acf1 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 26 Sep 2023 12:21:53 +0200 Subject: [PATCH] fixes --- .../SystemTextJsonInheritanceTests.cs | 3 +- .../SystemTextJson/SystemTextJsonTests.cs | 30 +++++++++++++++++++ .../SystemTextJsonReflectionService.cs | 11 +++---- 3 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs index d95306bac..a3d782196 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs @@ -1,5 +1,4 @@ using NJsonSchema.Converters; -using NJsonSchema.NewtonsoftJson.Generation; using System.Threading.Tasks; using Xunit; @@ -29,7 +28,7 @@ public class Fruit public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_schema_is_correct() { //// Act - var schema = NewtonsoftJsonSchemaGenerator.FromType(); + var schema = JsonSchema.FromType(); var data = schema.ToJson(); //// Assert diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs new file mode 100644 index 000000000..1b4a1998f --- /dev/null +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs @@ -0,0 +1,30 @@ +using System.ComponentModel.DataAnnotations; +using System.Threading.Tasks; +using Xunit; + +namespace NJsonSchema.Tests.Generation.SystemTextJson +{ + public class SystemTextJsonTests + { + public class HealthCheckResult + { + [Required] + public string Name { get; } + + public string Description { get; } + } + + [Fact] + public async Task When_property_is_readonly_then_its_in_the_schema() + { + //// Act + var schema = JsonSchema.FromType(); + var data = schema.ToJson(); + + //// Assert + Assert.NotNull(data); + Assert.Contains(@"Name", data); + Assert.Contains(@"Description", data); + } + } +} diff --git a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs index 11f804956..2fec659fe 100644 --- a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs +++ b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs @@ -28,15 +28,16 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex .Concat(contextualType.Fields) .Where(p => p.MemberInfo.DeclaringType == contextualType.Type)) { - if (accessorInfo.MemberInfo is FieldInfo fieldInfo && (fieldInfo.IsPrivate || fieldInfo.IsStatic || !fieldInfo.IsDefined(typeof(DataMemberAttribute)))) + if (accessorInfo.MemberInfo is FieldInfo fieldInfo && + (fieldInfo.IsPrivate || fieldInfo.IsStatic || !fieldInfo.IsDefined(typeof(DataMemberAttribute)))) { continue; } - if (accessorInfo.MemberInfo is PropertyInfo propertyInfo && ( - !(propertyInfo.GetMethod?.IsPrivate != true && propertyInfo.GetMethod?.IsStatic == false) || - !(propertyInfo.SetMethod?.IsPrivate != true && propertyInfo.SetMethod?.IsStatic == false) && - !propertyInfo.IsDefined(typeof(DataMemberAttribute)))) + if (accessorInfo.MemberInfo is PropertyInfo propertyInfo && + (propertyInfo.GetMethod?.IsPrivate == false || propertyInfo.GetMethod?.IsStatic == true) && + (propertyInfo.SetMethod?.IsPrivate == false || propertyInfo.SetMethod?.IsStatic == true) && + !propertyInfo.IsDefined(typeof(DataMemberAttribute))) { continue; }