Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to explicitly exclude types from schema #1670

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ContainerType1>(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<ContainerType2>();
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading