From 79a760f302765ae25595c287477c59c0d1cad7d2 Mon Sep 17 00:00:00 2001 From: Havret Date: Mon, 22 Jan 2024 23:10:11 +0100 Subject: [PATCH] Add option to explicitly exclude types from schema --- .../SystemTextJson/SystemTextJsonTests.cs | 55 +++++++++++++++++++ .../SystemTextJsonReflectionService.cs | 3 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs index 1b4a1998f..530d06014 100644 --- a/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs +++ b/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonTests.cs @@ -1,5 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; +using NJsonSchema.Annotations; +using NJsonSchema.Generation; using Xunit; namespace NJsonSchema.Tests.Generation.SystemTextJson @@ -26,5 +28,58 @@ public async Task When_property_is_readonly_then_its_in_the_schema() Assert.Contains(@"Name", data); Assert.Contains(@"Description", data); } + + public class ContainerType1 + { + public int Property { get; set; } + public NestedType1 NestedType { get; set; } + } + + public class NestedType1 + { + public int NestedProperty { get; set; } + } + + [Fact] + public async Task When_type_is_excluded_then_it_should_not_be_in_the_schema() + { + //// Act + var schema = JsonSchema.FromType(new SystemTextJsonSchemaGeneratorSettings + { + ExcludedTypeNames = [typeof(NestedType1).FullName] + }); + var data = schema.ToJson(); + + //// Assert + Assert.NotNull(data); + Assert.DoesNotContain(@"NestedType1", data); + Assert.Contains(@"Property", data); + } + + public class ContainerType2 + { + public int Property { get; set; } + + [JsonSchemaIgnore] + public NestedType2 NestedType { get; set; } + } + + public class NestedType2 + { + public int NestedProperty { get; set; } + } + + [Fact] + public async Task When_type_is_excluded_with_json_schema_ignore_attribute_then_it_should_not_be_in_the_schema() + { + //// Act + var schema = JsonSchema.FromType(); + var data = schema.ToJson(); + + //// Assert + Assert.NotNull(data); + Assert.DoesNotContain(@"NestedType2", data); + Assert.Contains(@"Property", data); + } } } diff --git a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs index 71cb2cea8..b8dee551e 100644 --- a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs +++ b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs @@ -62,7 +62,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex var ignored = propertyIgnored || schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo) || accessorInfo.GetAttributes(true) - .FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null; + .FirstAssignableToTypeNameOrDefault("System.Text.Json.Serialization.JsonExtensionDataAttribute", TypeNameStyle.FullName) != null + || settings.ExcludedTypeNames.Contains(accessorInfo.AccessorType.Type.FullName); if (!ignored) {