Skip to content

Commit

Permalink
Use expression bodies for properties and accessors (#5053)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 23, 2024
1 parent c062ed8 commit e8d971e
Show file tree
Hide file tree
Showing 28 changed files with 378 additions and 423 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
[IDE0017] Object initialization can be simplified
[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 Down Expand Up @@ -85,7 +83,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0017;IDE0021;IDE0022;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1845;CA1861;CA1870;CA2263;SYSLIB0012</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,7 @@ public string JsonSerializerParameterCode

/// <summary>Gets the JSON converters array code.</summary>
public string JsonConvertersArrayCode
{
get
{
return CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(
_settings.CSharpGeneratorSettings, RequiresJsonExceptionConverter ? new[] { "JsonExceptionConverter" } : null);
}
}
=> CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(_settings.CSharpGeneratorSettings, RequiresJsonExceptionConverter ? new[] { "JsonExceptionConverter" } : null);

/// <summary>Gets the Title.</summary>
public string Title => _document.Info.Title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,9 @@ public string SyncResultType
}

/// <summary>Gets or sets the type of the result.</summary>
public override string ResultType
{
get
{
return SyncResultType == "void"
public override string ResultType => SyncResultType == "void"
? "System.Threading.Tasks.Task"
: "System.Threading.Tasks.Task<" + SyncResultType + ">";
}
}

/// <summary>Gets or sets the type of the exception.</summary>
public override string ExceptionType
Expand Down
16 changes: 2 additions & 14 deletions src/NSwag.CodeGeneration/Models/ParameterModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,10 @@ public string Default
public bool IsForm => _parameter.Style == OpenApiParameterStyle.Form;

/// <summary>Gets the contained value property names (OpenAPI 3).</summary>
public IEnumerable<PropertyModel> PropertyNames
{
get
{
return _properties.Where(p => !p.IsCollection);
}
}
public IEnumerable<PropertyModel> PropertyNames => _properties.Where(p => !p.IsCollection);

/// <summary>Gets the contained collection property names (OpenAPI 3).</summary>
public IEnumerable<PropertyModel> CollectionPropertyNames
{
get
{
return _properties.Where(p => p.IsCollection);
}
}
public IEnumerable<PropertyModel> CollectionPropertyNames => _properties.Where(p => p.IsCollection);

/// <summary>Gets a value indicating whether the parameter has a description.</summary>
public bool HasDescription => !string.IsNullOrEmpty(Description);
Expand Down
17 changes: 2 additions & 15 deletions src/NSwag.CodeGeneration/Models/ResponseModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,13 @@ protected ResponseModelBase(IOperationModel operationModel,
public ICollection<JsonExpectedSchema> ExpectedSchemas => _response.ExpectedSchemas;

/// <summary>Gets a value indicating whether the response is of type date.</summary>
public bool IsDate
{
get
{
return ActualResponseSchema != null &&
public bool IsDate => ActualResponseSchema != null &&
(ActualResponseSchema.Format == JsonFormatStrings.Date ||
ActualResponseSchema.Format == JsonFormatStrings.DateTime) &&
_generator.GetTypeName(ActualResponseSchema, IsNullable, "Response") != "string";
}
}

/// <summary>Gets a value indicating whether the response requires a text/plain content.</summary>
public bool IsPlainText
{
get
{
return !_response.Content.ContainsKey("application/json") &&
_response.Content.ContainsKey("text/plain");
}
}
public bool IsPlainText => !_response.Content.ContainsKey("application/json") && _response.Content.ContainsKey("text/plain");

/// <summary>Gets a value indicating whether this is a file response.</summary>
public bool IsFile => IsSuccess && _response.IsBinary(_operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected CodeGeneratorCommandBase(TSettings settings)
[Argument(Name = "TemplateDirectory", IsRequired = false, Description = "The Liquid template directory (experimental).")]
public string TemplateDirectory
{
get { return Settings.CodeGeneratorSettings.TemplateDirectory; }
set { Settings.CodeGeneratorSettings.TemplateDirectory = value; }
get => Settings.CodeGeneratorSettings.TemplateDirectory;
set => Settings.CodeGeneratorSettings.TemplateDirectory = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,210 +31,210 @@ public JsonSchemaToCSharpCommand()
[Argument(Name = "Namespace", Description = "The namespace of the generated classes.")]
public string Namespace
{
get { return Settings.Namespace; }
set { Settings.Namespace = value; }
get => Settings.Namespace;
set => Settings.Namespace = value;
}

[Argument(Name = "RequiredPropertiesMustBeDefined", IsRequired = false,
Description = "Specifies whether a required property must be defined in JSON (sets Required.Always when the property is required).")]
public bool RequiredPropertiesMustBeDefined
{
get { return Settings.RequiredPropertiesMustBeDefined; }
set { Settings.RequiredPropertiesMustBeDefined = value; }
get => Settings.RequiredPropertiesMustBeDefined;
set => Settings.RequiredPropertiesMustBeDefined = value;
}

[Argument(Name = "DateType", IsRequired = false, Description = "The date .NET type (default: 'DateTimeOffset').")]
public string DateType
{
get { return Settings.DateType; }
set { Settings.DateType = value; }
get => Settings.DateType;
set => Settings.DateType = value;
}

[Argument(Name = "JsonConverters", IsRequired = false, Description = "Specifies the custom Json.NET converter types (optional, comma separated).")]
public string[] JsonConverters
{
get { return Settings.JsonConverters; }
set { Settings.JsonConverters = value; }
get => Settings.JsonConverters;
set => Settings.JsonConverters = value;
}

[Argument(Name = "AnyType", IsRequired = false, Description = "The any .NET type (default: 'object').")]
public string AnyType
{
get { return Settings.AnyType; }
set { Settings.AnyType = value; }
get => Settings.AnyType;
set => Settings.AnyType = value;
}

[Argument(Name = "DateTimeType", IsRequired = false, Description = "The date time .NET type (default: 'DateTimeOffset').")]
public string DateTimeType
{
get { return Settings.DateTimeType; }
set { Settings.DateTimeType = value; }
get => Settings.DateTimeType;
set => Settings.DateTimeType = value;
}

[Argument(Name = "TimeType", IsRequired = false, Description = "The time .NET type (default: 'TimeSpan').")]
public string TimeType
{
get { return Settings.TimeType; }
set { Settings.TimeType = value; }
get => Settings.TimeType;
set => Settings.TimeType = value;
}

[Argument(Name = "TimeSpanType", IsRequired = false, Description = "The time span .NET type (default: 'TimeSpan').")]
public string TimeSpanType
{
get { return Settings.TimeSpanType; }
set { Settings.TimeSpanType = value; }
get => Settings.TimeSpanType;
set => Settings.TimeSpanType = value;
}

[Argument(Name = "ArrayType", IsRequired = false, Description = "The generic array .NET type (default: 'ICollection').")]
public string ArrayType
{
get { return Settings.ArrayType; }
set { Settings.ArrayType = value; }
get => Settings.ArrayType;
set => Settings.ArrayType = value;
}

[Argument(Name = "ArrayInstanceType", IsRequired = false, Description = "The generic array .NET instance type (default: empty = ArrayType).")]
public string ArrayInstanceType
{
get { return Settings.ArrayInstanceType; }
set { Settings.ArrayInstanceType = value; }
get => Settings.ArrayInstanceType;
set => Settings.ArrayInstanceType = value;
}

[Argument(Name = "DictionaryType", IsRequired = false, Description = "The generic dictionary .NET type (default: 'IDictionary').")]
public string DictionaryType
{
get { return Settings.DictionaryType; }
set { Settings.DictionaryType = value; }
get => Settings.DictionaryType;
set => Settings.DictionaryType = value;
}

[Argument(Name = "DictionaryInstanceType", IsRequired = false, Description = "The generic dictionary .NET instance type (default: empty = DictionaryType).")]
public string DictionaryInstanceType
{
get { return Settings.DictionaryInstanceType; }
set { Settings.DictionaryInstanceType = value; }
get => Settings.DictionaryInstanceType;
set => Settings.DictionaryInstanceType = value;
}

[Argument(Name = "ArrayBaseType", IsRequired = false, Description = "The generic array .NET type (default: 'Collection').")]
public string ArrayBaseType
{
get { return Settings.ArrayBaseType; }
set { Settings.ArrayBaseType = value; }
get => Settings.ArrayBaseType;
set => Settings.ArrayBaseType = value;
}

[Argument(Name = "DictionaryBaseType", IsRequired = false, Description = "The generic dictionary .NET type (default: 'Dictionary').")]
public string DictionaryBaseType
{
get { return Settings.DictionaryBaseType; }
set { Settings.DictionaryBaseType = value; }
get => Settings.DictionaryBaseType;
set => Settings.DictionaryBaseType = value;
}

[Argument(Name = "ClassStyle", IsRequired = false, Description = "The CSharp class style, 'Poco' or 'Inpc' (default: 'Poco').")]
public CSharpClassStyle ClassStyle
{
get { return Settings.ClassStyle; }
set { Settings.ClassStyle = value; }
get => Settings.ClassStyle;
set => Settings.ClassStyle = value;
}

[Argument(Name = "JsonLibrary", IsRequired = false, Description = "The CSharp JSON library, 'NewtonsoftJson' or 'SystemTextJson' (default: 'NewtonsoftJson', 'SystemTextJson' is experimental).")]
public CSharpJsonLibrary JsonLibrary
{
get { return Settings.JsonLibrary; }
set { Settings.JsonLibrary = value; }
get => Settings.JsonLibrary;
set => Settings.JsonLibrary = value;
}

[Argument(Name = "GenerateDefaultValues", IsRequired = false, Description = "Specifies whether to generate default values for properties (may generate CSharp 6 code, default: true).")]
public bool GenerateDefaultValues
{
get { return Settings.GenerateDefaultValues; }
set { Settings.GenerateDefaultValues = value; }
get => Settings.GenerateDefaultValues;
set => Settings.GenerateDefaultValues = value;
}

[Argument(Name = "GenerateDataAnnotations", IsRequired = false, Description = "Specifies whether to generate data annotation attributes on DTO classes (default: true).")]
public bool GenerateDataAnnotations
{
get { return Settings.GenerateDataAnnotations; }
set { Settings.GenerateDataAnnotations = value; }
get => Settings.GenerateDataAnnotations;
set => Settings.GenerateDataAnnotations = value;
}

[Argument(Name = "ExcludedTypeNames", IsRequired = false, Description = "The excluded DTO type names (must be defined in an import or other namespace).")]
public string[] ExcludedTypeNames
{
get { return Settings.ExcludedTypeNames; }
set { Settings.ExcludedTypeNames = value; }
get => Settings.ExcludedTypeNames;
set => Settings.ExcludedTypeNames = value;
}

[Argument(Name = "HandleReferences", IsRequired = false, Description = "Use preserve references handling (All) in the JSON serializer (default: false).")]
public bool HandleReferences
{
get { return Settings.HandleReferences; }
set { Settings.HandleReferences = value; }
get => Settings.HandleReferences;
set => Settings.HandleReferences = value;
}

[Argument(Name = "GenerateImmutableArrayProperties", IsRequired = false,
Description = "Specifies whether to remove the setter for non-nullable array properties (default: false).")]
public bool GenerateImmutableArrayProperties
{
get { return Settings.GenerateImmutableArrayProperties; }
set { Settings.GenerateImmutableArrayProperties = value; }
get => Settings.GenerateImmutableArrayProperties;
set => Settings.GenerateImmutableArrayProperties = value;
}

[Argument(Name = "GenerateImmutableDictionaryProperties", IsRequired = false,
Description = "Specifies whether to remove the setter for non-nullable dictionary properties (default: false).")]
public bool GenerateImmutableDictionaryProperties
{
get { return Settings.GenerateImmutableDictionaryProperties; }
set { Settings.GenerateImmutableDictionaryProperties = value; }
get => Settings.GenerateImmutableDictionaryProperties;
set => Settings.GenerateImmutableDictionaryProperties = value;
}

[Argument(Name = "JsonSerializerSettingsTransformationMethod", IsRequired = false,
Description = "The name of a static method which is called to transform the JsonSerializerSettings used in the generated ToJson()/FromJson() methods (default: none).")]
public string JsonSerializerSettingsTransformationMethod
{
get { return Settings.JsonSerializerSettingsTransformationMethod; }
set { Settings.JsonSerializerSettingsTransformationMethod = value; }
get => Settings.JsonSerializerSettingsTransformationMethod;
set => Settings.JsonSerializerSettingsTransformationMethod = value;
}

[Argument(Name = "InlineNamedArrays", Description = "Inline named arrays (default: false).", IsRequired = false)]
public bool InlineNamedArrays
{
get { return Settings.InlineNamedArrays; }
set { Settings.InlineNamedArrays = value; }
get => Settings.InlineNamedArrays;
set => Settings.InlineNamedArrays = value;
}

[Argument(Name = "InlineNamedDictionaries", Description = "Inline named dictionaries (default: false).", IsRequired = false)]
public bool InlineNamedDictionaries
{
get { return Settings.InlineNamedDictionaries; }
set { Settings.InlineNamedDictionaries = value; }
get => Settings.InlineNamedDictionaries;
set => Settings.InlineNamedDictionaries = value;
}

[Argument(Name = "InlineNamedTuples", Description = "Inline named tuples (default: true).", IsRequired = false)]
public bool InlineNamedTuples
{
get { return Settings.InlineNamedTuples; }
set { Settings.InlineNamedTuples = value; }
get => Settings.InlineNamedTuples;
set => Settings.InlineNamedTuples = value;
}

[Argument(Name = "InlineNamedAny", Description = "Inline named any types (default: false).", IsRequired = false)]
public bool InlineNamedAny
{
get { return Settings.InlineNamedAny; }
set { Settings.InlineNamedAny = value; }
get => Settings.InlineNamedAny;
set => Settings.InlineNamedAny = value;
}

[Argument(Name = "GenerateOptionalPropertiesAsNullable", IsRequired = false, Description = "Specifies whether optional schema properties " +
"(not required) are generated as nullable properties (default: false).")]
public bool GenerateOptionalPropertiesAsNullable
{
get { return Settings.GenerateOptionalPropertiesAsNullable; }
set { Settings.GenerateOptionalPropertiesAsNullable = value; }
get => Settings.GenerateOptionalPropertiesAsNullable;
set => Settings.GenerateOptionalPropertiesAsNullable = value;
}

[Argument(Name = "GenerateNullableReferenceTypes", IsRequired = false, Description = "Specifies whether whether to " +
"generate Nullable Reference Type annotations (default: false).")]
public bool GenerateNullableReferenceTypes
{
get { return Settings.GenerateNullableReferenceTypes; }
set { Settings.GenerateNullableReferenceTypes = value; }
get => Settings.GenerateNullableReferenceTypes;
set => Settings.GenerateNullableReferenceTypes = value;
}

public override async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
Expand Down
Loading

0 comments on commit e8d971e

Please sign in to comment.