Skip to content
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
23 changes: 23 additions & 0 deletions src/NJsonSchema.Tests/Generation/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ public async Task When_enum_has_description_attributes_then_descriptions_are_inc
await VerifyHelper.Verify(json);
}

[Fact]
public async Task When_enum_has_no_description_attributes_then_descriptions_are_not_included_in_schema()
{
// Arrange

// Act
var schema = NewtonsoftJsonSchemaGenerator.FromType<EnumWithFlags>(new NewtonsoftJsonSchemaGeneratorSettings
{
SerializerSettings =
{
Converters = { new StringEnumConverter() }
}
});
var json = schema.ToJson();

// Assert
Assert.Empty(schema.EnumerationDescriptions);

// Verify the JSON output does not contain the x-enumDescriptions property
await VerifyHelper.Verify(json);
}


[Fact]
public async Task When_schema_has_x_enum_names_then_backward_compatibility_works()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "EnumWithFlags",
"type": "string",
"description": "",
"x-enumFlags": true,
"x-enumNames": [
"Foo",
"Bar",
"Baz"
],
"enum": [
"Foo",
"Bar",
"Baz"
]
}
8 changes: 8 additions & 0 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ protected virtual void GenerateEnum(JsonSchema schema, JsonTypeDescription typeD
schema.EnumerationDescriptions.Clear();
schema.IsFlagEnumerable = contextualType.IsAttributeDefined<FlagsAttribute>(true);

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

schema.EnumerationNames.Add(enumName);
schema.EnumerationDescriptions.Add(enumDescription);
allDescriptionsEmpty &= string.IsNullOrWhiteSpace(enumDescription);
}

if (allDescriptionsEmpty)
{
// don't output empty descriptions
schema.EnumerationDescriptions.Clear();
}

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