Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Sep 26, 2023
1 parent c93b2a2 commit b5f0b8e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NJsonSchema.Converters;
using NJsonSchema.NewtonsoftJson.Generation;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -29,7 +28,7 @@ public class Fruit
public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_schema_is_correct()
{
//// Act
var schema = NewtonsoftJsonSchemaGenerator.FromType<Fruit>();
var schema = JsonSchema.FromType<Fruit>();
var data = schema.ToJson();

//// Assert
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HealthCheckResult>();
var data = schema.ToJson();

//// Assert
Assert.NotNull(data);
Assert.Contains(@"Name", data);
Assert.Contains(@"Description", data);
}
}
}
11 changes: 6 additions & 5 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit b5f0b8e

Please sign in to comment.