Skip to content

Commit 58a90b9

Browse files
authored
fix: Enum descriptions serialization/property (#1861)
1 parent d4cdaea commit 58a90b9

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

src/NJsonSchema/Generation/JsonSchemaGenerator.cs

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

705-
var allDescriptionsEmpty = true;
706705
Func<object, string?>? enumValueConverter = null;
706+
707+
var allDescriptionsEmpty = true;
707708
var underlyingType = Enum.GetUnderlyingType(contextualType.Type);
708709
foreach (var enumName in Enum.GetNames(contextualType.Type))
709710
{
710-
string? enumDescription = null;
711711
var field = contextualType.Type.GetRuntimeField(enumName);
712+
712713
// Retrieve the Description attribute value, if present.
714+
string? enumDescription = null;
713715
var descriptionAttribute = field?.GetCustomAttribute<DescriptionAttribute>();
714-
715716
if (descriptionAttribute != null)
716717
{
717718
enumDescription = descriptionAttribute.Description;
@@ -785,7 +786,7 @@ private void ApplyAdditionalProperties<TSchemaType>(TSchemaType schema, Type typ
785786
{
786787
var extensionDataProperty = type.GetContextualProperties()
787788
.FirstOrDefault(p => p.GetAttributes(true).Any(a =>
788-
Namotion.Reflection.TypeExtensions.IsAssignableToTypeName(a.GetType(), "JsonExtensionDataAttribute", TypeNameStyle.Name)));
789+
a.GetType().IsAssignableToTypeName("JsonExtensionDataAttribute", TypeNameStyle.Name)));
789790

790791
if (extensionDataProperty != null)
791792
{

src/NJsonSchema/JsonSchema.Serialization.cs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ internal object? DiscriminatorRaw
125125
}
126126
set
127127
{
128-
if (value is string)
128+
if (value is string stringValue)
129129
{
130-
Discriminator = (string) value;
130+
Discriminator = stringValue;
131131
}
132132
else if (value != null)
133133
{
@@ -151,9 +151,9 @@ internal object? ExclusiveMaximumRaw
151151
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object) true : null);
152152
set
153153
{
154-
if (value is bool)
154+
if (value is bool boolValue)
155155
{
156-
IsExclusiveMaximum = (bool) value;
156+
IsExclusiveMaximum = boolValue;
157157
}
158158
else if (value != null && (value.Equals("true") || value.Equals("false")))
159159
{
@@ -173,9 +173,9 @@ internal object? ExclusiveMinimumRaw
173173
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object) true : null);
174174
set
175175
{
176-
if (value is bool)
176+
if (value is bool boolValue)
177177
{
178-
IsExclusiveMinimum = (bool) value;
178+
IsExclusiveMinimum = boolValue;
179179
}
180180
else if (value != null && (value.Equals("true") || value.Equals("false")))
181181
{
@@ -207,9 +207,9 @@ internal object? AdditionalItemsRaw
207207
}
208208
set
209209
{
210-
if (value is bool)
210+
if (value is bool boolValue)
211211
{
212-
AllowAdditionalItems = (bool) value;
212+
AllowAdditionalItems = boolValue;
213213
}
214214
else if (value != null && (value.Equals("true") || value.Equals("false")))
215215
{
@@ -261,9 +261,9 @@ internal object? AdditionalPropertiesRaw
261261
}
262262
set
263263
{
264-
if (value is bool)
264+
if (value is bool boolValue)
265265
{
266-
AllowAdditionalProperties = (bool) value;
266+
AllowAdditionalProperties = boolValue;
267267
}
268268
else if (value != null && (value.Equals("true") || value.Equals("false")))
269269
{
@@ -295,9 +295,9 @@ internal object? ItemsRaw
295295
}
296296
set
297297
{
298-
if (value is JArray)
298+
if (value is JArray array)
299299
{
300-
Items = new ObservableCollection<JsonSchema>(((JArray) value).Select(FromJsonWithCurrentSettings));
300+
Items = new ObservableCollection<JsonSchema>(array.Select(FromJsonWithCurrentSettings));
301301
}
302302
else if (value != null)
303303
{
@@ -350,14 +350,14 @@ private void ResetTypeRaw()
350350
[JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
351351
internal ICollection<string>? RequiredPropertiesRaw
352352
{
353-
get => RequiredProperties != null && RequiredProperties.Count > 0 ? RequiredProperties : null;
353+
get => RequiredProperties is { Count: > 0 } ? RequiredProperties : null;
354354
set => RequiredProperties = value ?? [];
355355
}
356356

357357
[JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
358358
internal IDictionary<string, JsonSchemaProperty>? PropertiesRaw
359359
{
360-
get => _properties != null && _properties.Count > 0 ? Properties : null;
360+
get => _properties is { Count: > 0 } ? Properties : null;
361361
set => Properties = value != null ? new ObservableDictionary<string, JsonSchemaProperty>(value!) : [];
362362
}
363363

@@ -373,7 +373,7 @@ internal IDictionary<string, JsonSchemaProperty>? PatternPropertiesRaw
373373
[JsonProperty("definitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
374374
internal IDictionary<string, JsonSchema>? DefinitionsRaw
375375
{
376-
get => Definitions != null && Definitions.Count > 0 ? Definitions : null;
376+
get => Definitions is { Count: > 0 } ? Definitions : null;
377377
set => Definitions = value != null ? new ObservableDictionary<string, JsonSchema>(value!) : [];
378378
}
379379

@@ -409,7 +409,7 @@ internal Collection<string>? EnumerationVarNamesRaw
409409
[JsonProperty("x-enumNames", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
410410
internal Collection<string>? EnumerationNamesRaw
411411
{
412-
get => EnumerationNames != null && EnumerationNames.Count > 0 ? EnumerationNames : null;
412+
get => EnumerationNames is { Count: > 0 } ? EnumerationNames : null;
413413
set => EnumerationNames = value != null ? new ObservableCollection<string>(value) : [];
414414
}
415415

@@ -421,7 +421,7 @@ internal JArray? EnumerationDescriptionsRaw
421421
set => EnumerationDescriptionsDashedRaw = value;
422422
}
423423

424-
/// <summary>Gets or sets the enumeration descriptions (optional, draft v5). </summary>
424+
/// <summary>Gets or sets the enumeration descriptions (optional, draft v5).</summary>
425425
[JsonProperty("x-enum-descriptions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
426426
internal JArray? EnumerationDescriptionsDashedRaw
427427
{
@@ -439,28 +439,28 @@ internal JArray? EnumerationDescriptionsDashedRaw
439439
[JsonProperty("enum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
440440
internal ICollection<object?>? EnumerationRaw
441441
{
442-
get => Enumeration != null && Enumeration.Count > 0 ? Enumeration : null;
442+
get => Enumeration is { Count: > 0 } ? Enumeration : null;
443443
set => Enumeration = value != null ? new ObservableCollection<object?>(value) : [];
444444
}
445445

446446
[JsonProperty("allOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
447447
internal ICollection<JsonSchema>? AllOfRaw
448448
{
449-
get => _allOf != null && _allOf.Count > 0 ? AllOf : null;
449+
get => _allOf is { Count: > 0 } ? AllOf : null;
450450
set => AllOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
451451
}
452452

453453
[JsonProperty("anyOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
454454
internal ICollection<JsonSchema>? AnyOfRaw
455455
{
456-
get => _anyOf != null && _anyOf.Count > 0 ? AnyOf : null;
456+
get => _anyOf is { Count: > 0 } ? AnyOf : null;
457457
set => AnyOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
458458
}
459459

460460
[JsonProperty("oneOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
461461
internal ICollection<JsonSchema>? OneOfRaw
462462
{
463-
get => _oneOf != null && _oneOf.Count > 0 ? OneOf : null;
463+
get => _oneOf is { Count: > 0 } ? OneOf : null;
464464
set => OneOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
465465
}
466466

@@ -542,9 +542,8 @@ private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedE
542542

543543
if (keysToRemove != null)
544544
{
545-
for (var i = 0; i < keysToRemove.Count; i++)
545+
foreach (var key in keysToRemove)
546546
{
547-
var key = keysToRemove[i];
548547
collection.Remove(key);
549548
}
550549
}
@@ -553,32 +552,23 @@ private void InitializeSchemaCollection(object? sender, NotifyCollectionChangedE
553552

554553
private static List<string?>? ConvertPossibleStringArray(JArray? array)
555554
{
556-
if (array is null || array.Count == 0)
557-
{
558-
return null;
559-
}
560-
561-
if (array.Count > 0)
555+
if (array?.Count > 0)
562556
{
563557
var result = new List<string?>(array.Count);
564-
for (var i = 0; i < array.Count; i++)
558+
foreach (var item in array)
565559
{
566-
var item = array[i];
567-
if (item.Type is JTokenType.String)
560+
switch (item.Type)
568561
{
569-
result.Add(null);
570-
}
571-
else if (item.Type is JTokenType.Null)
572-
{
573-
result.Add(null);
574-
}
575-
else
576-
{
577-
// we stop processing as we don't to trust the content
578-
return null;
562+
case JTokenType.String:
563+
result.Add(item.Value<string>());
564+
break;
565+
case JTokenType.Null:
566+
result.Add(null);
567+
break;
568+
default:
569+
return null; // unsupported token type, abort
579570
}
580571
}
581-
582572
return result;
583573
}
584574

0 commit comments

Comments
 (0)