Skip to content

Commit

Permalink
Use expression bodies for properties and accessors (#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 3066455 commit 9bbf46b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 54 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
[IDE0008] Use explicit type instead of 'var'
[IDE0021] Use block body for constructor
[IDE0022] Use block body for method
[IDE0025] Use expression body for property
[IDE0027] Use expression body for accessor
[IDE0029] Null check can be simplified
[IDE0032] Use auto property
[IDE0039] Use local function
Expand All @@ -61,7 +59,7 @@
[CA1870] Use a cached 'SearchValues' instance for improved searching performance
[CA2263] Prefer the generic overload 'System.Enum.GetValues<TEnum>()'
-->
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0025;IDE0027;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0100;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0100;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
</PropertyGroup>

</Project>
2 changes: 1 addition & 1 deletion src/NJsonSchema/Generation/SchemaProcessorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SchemaProcessorContext(ContextualType contextualType, JsonSchema schema,

/// <summary>The source type.</summary>
[Obsolete("Use ContextualType to obtain this instead.")]
public Type Type { get => ContextualType.OriginalType; }
public Type Type => ContextualType.OriginalType;

/// <summary>The source contextual type.</summary>
public ContextualType ContextualType { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonSchema.Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static void ThrowInvalidOperationException(string message)
[JsonIgnore]
public override JsonSchema? Reference
{
get { return base.Reference; }
get => base.Reference;
set
{
base.Reference = value;
Expand Down
42 changes: 18 additions & 24 deletions src/NJsonSchema/JsonSchema.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal object? DiscriminatorRaw
[JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object? ExclusiveMaximumRaw
{
get { return ExclusiveMaximum ?? (IsExclusiveMaximum ? (object)true : null); }
get => ExclusiveMaximum ?? (IsExclusiveMaximum ? (object)true : null);
set
{
if (value is bool)
Expand All @@ -161,7 +161,7 @@ internal object? ExclusiveMaximumRaw
[JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object? ExclusiveMinimumRaw
{
get { return ExclusiveMinimum ?? (IsExclusiveMinimum ? (object)true : null); }
get => ExclusiveMinimum ?? (IsExclusiveMinimum ? (object)true : null);
set
{
if (value is bool)
Expand Down Expand Up @@ -352,8 +352,8 @@ private void ResetTypeRaw()
[JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<string>? RequiredPropertiesRaw
{
get { return RequiredProperties != null && RequiredProperties.Count > 0 ? RequiredProperties : null; }
set { RequiredProperties = value ?? []; }
get => RequiredProperties != null && RequiredProperties.Count > 0 ? RequiredProperties : null;
set => RequiredProperties = value ?? [];
}

[JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
Expand All @@ -366,59 +366,53 @@ internal IDictionary<string, JsonSchemaProperty>? PropertiesRaw
[JsonProperty("patternProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchemaProperty>? PatternPropertiesRaw
{
get
{
return _patternProperties is { Count: > 0 }
get => _patternProperties is { Count: > 0 }
? PatternProperties.ToDictionary(p => p.Key, p => p.Value)
: null;
}
set
{
PatternProperties = value != null ? new ObservableDictionary<string, JsonSchemaProperty>(value!) : [];
}
set => PatternProperties = value != null ? new ObservableDictionary<string, JsonSchemaProperty>(value!) : [];
}

[JsonProperty("definitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema>? DefinitionsRaw
{
get { return Definitions != null && Definitions.Count > 0 ? Definitions : null; }
set { Definitions = value != null ? new ObservableDictionary<string, JsonSchema>(value!) : []; }
get => Definitions != null && Definitions.Count > 0 ? Definitions : null;
set => Definitions = value != null ? new ObservableDictionary<string, JsonSchema>(value!) : [];
}

/// <summary>Gets or sets the enumeration names (optional, draft v5). </summary>
[JsonProperty("x-enumNames", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal Collection<string>? EnumerationNamesRaw
{
get { return EnumerationNames != null && EnumerationNames.Count > 0 ? EnumerationNames : null; }
set { EnumerationNames = value != null ? new ObservableCollection<string>(value) : []; }
get => EnumerationNames != null && EnumerationNames.Count > 0 ? EnumerationNames : null;
set => EnumerationNames = value != null ? new ObservableCollection<string>(value) : [];
}

[JsonProperty("enum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<object?>? EnumerationRaw
{
get { return Enumeration != null && Enumeration.Count > 0 ? Enumeration : null; }
set { Enumeration = value != null ? new ObservableCollection<object?>(value) : []; }
get => Enumeration != null && Enumeration.Count > 0 ? Enumeration : null;
set => Enumeration = value != null ? new ObservableCollection<object?>(value) : [];
}

[JsonProperty("allOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema>? AllOfRaw
{
get { return _allOf != null && _allOf.Count > 0 ? AllOf : null; }
set { AllOf = value != null ? new ObservableCollection<JsonSchema>(value) : []; }
get => _allOf != null && _allOf.Count > 0 ? AllOf : null;
set => AllOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
}

[JsonProperty("anyOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema>? AnyOfRaw
{
get { return _anyOf != null && _anyOf.Count > 0 ? AnyOf : null; }
set { AnyOf = value != null ? new ObservableCollection<JsonSchema>(value) : []; }
get => _anyOf != null && _anyOf.Count > 0 ? AnyOf : null;
set => AnyOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
}

[JsonProperty("oneOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema>? OneOfRaw
{
get { return _oneOf != null && _oneOf.Count > 0 ? OneOf : null; }
set { OneOf = value != null ? new ObservableCollection<JsonSchema>(value) : []; }
get => _oneOf != null && _oneOf.Count > 0 ? OneOf : null;
set => OneOf = value != null ? new ObservableCollection<JsonSchema>(value) : [];
}

private void RegisterProperties(ObservableDictionary<string, JsonSchemaProperty>? oldCollection, ObservableDictionary<string, JsonSchemaProperty>? newCollection)
Expand Down
39 changes: 16 additions & 23 deletions src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,7 @@ internal static JsonSchema FromJsonWithCurrentSettings(object obj)

/// <summary>Gets a value indicating whether the schema is binary (file or binary format).</summary>
[JsonIgnore]
public bool IsBinary
{
get
{
return Type.IsFile() ||
(Type.IsString() && Format == JsonFormatStrings.Binary);
}
}
public bool IsBinary => Type.IsFile() || (Type.IsString() && Format == JsonFormatStrings.Binary);

/// <summary>Gets the inherited/parent schema (most probable base schema in allOf).</summary>
/// <remarks>Used for code generation.</remarks>
Expand Down Expand Up @@ -529,7 +522,7 @@ public JsonObjectType Type
[JsonProperty("x-dictionaryKey", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema? DictionaryKey
{
get { return _dictionaryKey; }
get => _dictionaryKey;
set
{
_dictionaryKey = value;
Expand All @@ -544,7 +537,7 @@ public JsonSchema? DictionaryKey
[JsonIgnore]
public IDictionary<string, JsonSchemaProperty> Properties
{
get { return _properties; }
get => _properties;
internal set
{
if (_properties != value)
Expand All @@ -560,7 +553,7 @@ internal set
[JsonProperty("xml", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonXmlObject? Xml
{
get { return _xmlObject; }
get => _xmlObject;
set
{
_xmlObject = value;
Expand All @@ -579,7 +572,7 @@ public JsonXmlObject? Xml
[JsonIgnore]
public IDictionary<string, JsonSchemaProperty> PatternProperties
{
get { return _patternProperties; }
get => _patternProperties;
internal set
{
if (_patternProperties != value)
Expand All @@ -595,7 +588,7 @@ internal set
[JsonIgnore]
public JsonSchema? Item
{
get { return _item; }
get => _item;
set
{
if (_item != value)
Expand All @@ -614,7 +607,7 @@ public JsonSchema? Item
[JsonIgnore]
public ICollection<JsonSchema> Items
{
get { return _items; }
get => _items;
internal set
{
if (_items != value)
Expand All @@ -635,7 +628,7 @@ internal set
[JsonProperty("not", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema? Not
{
get { return _not; }
get => _not;
set
{
_not = value;
Expand All @@ -650,7 +643,7 @@ public JsonSchema? Not
[JsonIgnore]
public IDictionary<string, JsonSchema> Definitions
{
get { return _definitions; }
get => _definitions;
internal set
{
if (_definitions != value)
Expand All @@ -666,7 +659,7 @@ internal set
[JsonIgnore]
public ICollection<JsonSchema> AllOf
{
get { return _allOf; }
get => _allOf;
internal set
{
if (_allOf != value)
Expand All @@ -682,7 +675,7 @@ internal set
[JsonIgnore]
public ICollection<JsonSchema> AnyOf
{
get { return _anyOf; }
get => _anyOf;
internal set
{
if (_anyOf != value)
Expand All @@ -698,7 +691,7 @@ internal set
[JsonIgnore]
public ICollection<JsonSchema> OneOf
{
get { return _oneOf; }
get => _oneOf;
internal set
{
if (_oneOf != value)
Expand All @@ -715,7 +708,7 @@ internal set
[JsonIgnore]
public bool AllowAdditionalItems
{
get { return _allowAdditionalItems; }
get => _allowAdditionalItems;
set
{
if (_allowAdditionalItems != value)
Expand All @@ -734,7 +727,7 @@ public bool AllowAdditionalItems
[JsonIgnore]
public JsonSchema? AdditionalItemsSchema
{
get { return _additionalItemsSchema; }
get => _additionalItemsSchema;
set
{
if (_additionalItemsSchema != value)
Expand All @@ -753,7 +746,7 @@ public JsonSchema? AdditionalItemsSchema
[JsonIgnore]
public bool AllowAdditionalProperties
{
get { return _allowAdditionalProperties; }
get => _allowAdditionalProperties;
set
{
if (_allowAdditionalProperties != value)
Expand All @@ -772,7 +765,7 @@ public bool AllowAdditionalProperties
[JsonIgnore]
public JsonSchema? AdditionalPropertiesSchema
{
get { return _additionalPropertiesSchema; }
get => _additionalPropertiesSchema;
set
{
if (_additionalPropertiesSchema != value)
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/JsonSchemaProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JsonSchemaProperty : JsonSchema
[JsonIgnore]
public override object? Parent
{
get { return _parent; }
get => _parent;
set
{
var initialize = _parent == null;
Expand All @@ -43,7 +43,7 @@ public override object? Parent
[JsonIgnore]
public bool IsRequired
{
get { return ParentSchema!.RequiredProperties.Contains(Name); }
get => ParentSchema!.RequiredProperties.Contains(Name);
set
{
if (ParentSchema == null)
Expand Down

0 comments on commit 9bbf46b

Please sign in to comment.