From 1569e52401628d009af8044d1886d5a26770d05c Mon Sep 17 00:00:00 2001 From: Krzysztof Porebski Date: Wed, 12 Jun 2024 21:07:34 +0200 Subject: [PATCH] Add option to explicitly exclude types from schema (#1670) Co-authored-by: Rico Suter --- .../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 e5fec8915..83984abbc 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 @@ -34,6 +36,59 @@ 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); + } [Fact] public async Task When_property_is_private_and_readonly_then_its_not_in_the_schema() diff --git a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs index 1d1aa0edd..0704e2cf7 100644 --- a/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs +++ b/src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs @@ -68,7 +68,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) {