Skip to content

Commit 1569e52

Browse files
HavretRicoSuter
andauthored
Add option to explicitly exclude types from schema (#1670)
Co-authored-by: Rico Suter <mail@rsuter.com>
1 parent 4c76ebd commit 1569e52

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.ComponentModel.DataAnnotations;
22
using System.Threading.Tasks;
3+
using NJsonSchema.Annotations;
4+
using NJsonSchema.Generation;
35
using Xunit;
46

57
namespace NJsonSchema.Tests.Generation.SystemTextJson
@@ -34,6 +36,59 @@ public async Task When_property_is_readonly_then_its_in_the_schema()
3436
Assert.Contains(@"Name", data);
3537
Assert.Contains(@"Description", data);
3638
}
39+
40+
public class ContainerType1
41+
{
42+
public int Property { get; set; }
43+
public NestedType1 NestedType { get; set; }
44+
}
45+
46+
public class NestedType1
47+
{
48+
public int NestedProperty { get; set; }
49+
}
50+
51+
[Fact]
52+
public async Task When_type_is_excluded_then_it_should_not_be_in_the_schema()
53+
{
54+
//// Act
55+
var schema = JsonSchema.FromType<ContainerType1>(new SystemTextJsonSchemaGeneratorSettings
56+
{
57+
ExcludedTypeNames = [typeof(NestedType1).FullName]
58+
});
59+
var data = schema.ToJson();
60+
61+
//// Assert
62+
Assert.NotNull(data);
63+
Assert.DoesNotContain(@"NestedType1", data);
64+
Assert.Contains(@"Property", data);
65+
}
66+
67+
public class ContainerType2
68+
{
69+
public int Property { get; set; }
70+
71+
[JsonSchemaIgnore]
72+
public NestedType2 NestedType { get; set; }
73+
}
74+
75+
public class NestedType2
76+
{
77+
public int NestedProperty { get; set; }
78+
}
79+
80+
[Fact]
81+
public async Task When_type_is_excluded_with_json_schema_ignore_attribute_then_it_should_not_be_in_the_schema()
82+
{
83+
//// Act
84+
var schema = JsonSchema.FromType<ContainerType2>();
85+
var data = schema.ToJson();
86+
87+
//// Assert
88+
Assert.NotNull(data);
89+
Assert.DoesNotContain(@"NestedType2", data);
90+
Assert.Contains(@"Property", data);
91+
}
3792

3893
[Fact]
3994
public async Task When_property_is_private_and_readonly_then_its_not_in_the_schema()

src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
6868
var ignored = propertyIgnored
6969
|| schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo)
7070
|| accessorInfo.GetAttributes(true)
71-
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null;
71+
.FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null
72+
|| settings.ExcludedTypeNames.Contains(accessorInfo.AccessorType.Type.FullName);
7273

7374
if (!ignored)
7475
{

0 commit comments

Comments
 (0)