Skip to content

Commit 1187c69

Browse files
authored
Don't output enum descriptions if all empty (#1854)
1 parent 79efc82 commit 1187c69

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/NJsonSchema.Tests/Generation/EnumTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,29 @@ public async Task When_enum_has_description_attributes_then_descriptions_are_inc
190190
await VerifyHelper.Verify(json);
191191
}
192192

193+
[Fact]
194+
public async Task When_enum_has_no_description_attributes_then_descriptions_are_not_included_in_schema()
195+
{
196+
// Arrange
197+
198+
// Act
199+
var schema = NewtonsoftJsonSchemaGenerator.FromType<EnumWithFlags>(new NewtonsoftJsonSchemaGeneratorSettings
200+
{
201+
SerializerSettings =
202+
{
203+
Converters = { new StringEnumConverter() }
204+
}
205+
});
206+
var json = schema.ToJson();
207+
208+
// Assert
209+
Assert.Empty(schema.EnumerationDescriptions);
210+
211+
// Verify the JSON output does not contain the x-enumDescriptions property
212+
await VerifyHelper.Verify(json);
213+
}
214+
215+
193216
[Fact]
194217
public async Task When_schema_has_x_enum_names_then_backward_compatibility_works()
195218
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "EnumWithFlags",
4+
"type": "string",
5+
"description": "",
6+
"x-enumFlags": true,
7+
"x-enumNames": [
8+
"Foo",
9+
"Bar",
10+
"Baz"
11+
],
12+
"enum": [
13+
"Foo",
14+
"Bar",
15+
"Baz"
16+
]
17+
}

src/NJsonSchema/Generation/JsonSchemaGenerator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ protected virtual void GenerateEnum(JsonSchema schema, JsonTypeDescription typeD
702702
schema.EnumerationDescriptions.Clear();
703703
schema.IsFlagEnumerable = contextualType.IsAttributeDefined<FlagsAttribute>(true);
704704

705+
var allDescriptionsEmpty = true;
705706
Func<object, string?>? enumValueConverter = null;
706707
var underlyingType = Enum.GetUnderlyingType(contextualType.Type);
707708
foreach (var enumName in Enum.GetNames(contextualType.Type))
@@ -739,6 +740,13 @@ protected virtual void GenerateEnum(JsonSchema schema, JsonTypeDescription typeD
739740

740741
schema.EnumerationNames.Add(enumName);
741742
schema.EnumerationDescriptions.Add(enumDescription);
743+
allDescriptionsEmpty &= string.IsNullOrWhiteSpace(enumDescription);
744+
}
745+
746+
if (allDescriptionsEmpty)
747+
{
748+
// don't output empty descriptions
749+
schema.EnumerationDescriptions.Clear();
742750
}
743751

744752
if (typeDescription.Type == JsonObjectType.Integer && Settings.GenerateEnumMappingDescription)

0 commit comments

Comments
 (0)