|
1 | 1 | using System.ComponentModel.DataAnnotations;
|
2 | 2 | using System.Threading.Tasks;
|
| 3 | +using NJsonSchema.Annotations; |
| 4 | +using NJsonSchema.Generation; |
3 | 5 | using Xunit;
|
4 | 6 |
|
5 | 7 | namespace NJsonSchema.Tests.Generation.SystemTextJson
|
@@ -34,6 +36,59 @@ public async Task When_property_is_readonly_then_its_in_the_schema()
|
34 | 36 | Assert.Contains(@"Name", data);
|
35 | 37 | Assert.Contains(@"Description", data);
|
36 | 38 | }
|
| 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 | + } |
37 | 92 |
|
38 | 93 | [Fact]
|
39 | 94 | public async Task When_property_is_private_and_readonly_then_its_not_in_the_schema()
|
|
0 commit comments