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
10 changes: 3 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,24 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<!--
[IDE0008] Use explicit type instead of 'var'
[IDE0019] Use pattern matching
[IDE0021] Use block body for constructor
[IDE0022] Use block body for method
[IDE0025] Use expression body for property
[IDE0027] Use expression body for accessor
[IDE0028] Collection initialization can be simplified
[IDE0029] Null check can be simplified
[IDE0032] Use auto property
[IDE0039] Use local function]
[IDE0045] 'if' statement can be simplified]
[IDE0039] Use local function
[IDE0045] 'if' statement can be simplified
[IDE0046] 'if' statement can be simplified
[IDE0055] Fix formatting
[IDE0057] Substring can be simplified
[IDE0059] Unnecessary assignment of a value
[IDE0060] Remove unused parameter
[IDE0078] Use pattern matching
[IDE0083] Use pattern matching
[IDE0090] 'new' expression can be simplified
[IDE0100] Remove redundant equality
[IDE0130] Namespace does not match folder structure
[IDE0160] Convert to block scoped namespace
[IDE0260] Use pattern matching
[IDE0290] Use primary constructor
[CA1200] Avoid using cref tags with a prefix
[CA1510] Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance
Expand All @@ -66,7 +62,7 @@
[CA1870] Use a cached 'SearchValues' instance for improved searching performance
[CA2263] Prefer the generic overload 'System.Enum.GetValues<TEnum>()'
-->
<NoWarn>$(NoWarn);IDE0008;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0260;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0100;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
</PropertyGroup>

</Project>
10 changes: 5 additions & 5 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ private string ResolveString(JsonSchema schema, bool isNullable, string? typeNam

#pragma warning disable 618 // used to resolve type from schemas generated with previous version of the library

if (schema.Format == JsonFormatStrings.Guid || schema.Format == JsonFormatStrings.Uuid)
if (schema.Format is JsonFormatStrings.Guid or JsonFormatStrings.Uuid)
{
return isNullable ? "System.Guid?" : "System.Guid";
}

if (schema.Format == JsonFormatStrings.Base64 || schema.Format == JsonFormatStrings.Byte)
if (schema.Format is JsonFormatStrings.Base64 or JsonFormatStrings.Byte)
{
return "byte[]" + nullableReferenceType;
}
Expand All @@ -221,17 +221,17 @@ private static string ResolveInteger(JsonSchema schema, bool isNullable, string?
return isNullable ? "byte?" : "byte";
}

if (schema.Format == JsonFormatStrings.Long || schema.Format == "long")
if (schema.Format is JsonFormatStrings.Long or "long")
{
return isNullable ? "long?" : "long";
}

if (schema.Format == JsonFormatStrings.Long || schema.Format == "long")
if (schema.Format is JsonFormatStrings.Long or "long")
{
return isNullable ? "long?" : "long";
}

if (schema.Format == JsonFormatStrings.ULong || schema.Format == "ulong")
if (schema.Format is JsonFormatStrings.ULong or "ulong")
{
return isNullable ? "ulong?" : "ulong";
}
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public CSharpValueGenerator(CSharpGeneratorSettings settings)
return $"new {targetType}({stringLiteral})";
}

if (targetType == "System.DateTime" || targetType == "System.DateTime?")
if (targetType is "System.DateTime" or "System.DateTime?")
{
var stringLiteral = GetDefaultAsStringLiteral(schema);
return $"System.DateTime.Parse({stringLiteral})";
}
}

var isOptional = (schema as JsonSchemaProperty)?.IsRequired == false;
var isOptional = schema is JsonSchemaProperty { IsRequired: false };

schema = schema.ActualSchema;
if (schema != null && allowsNull == false && isOptional == false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public IEnumerable<PropertyModel> AllProperties { get; }

/// <summary>Gets a value indicating whether the class has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) &&
public bool HasDescription => _schema is not JsonSchemaProperty &&
(!string.IsNullOrEmpty(_schema.Description) ||
!string.IsNullOrEmpty(_schema.ActualTypeSchema.Description));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public EnumTemplateModel(string typeName, JsonSchema schema, CSharpGeneratorSett
public string Name { get; }

/// <summary>Gets a value indicating whether the enum has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) && !string.IsNullOrEmpty(_schema.Description);
public bool HasDescription => _schema is not JsonSchemaProperty && !string.IsNullOrEmpty(_schema.Description);

/// <summary>Gets the description.</summary>
public string? Description => _schema.Description;
Expand Down
6 changes: 3 additions & 3 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public string RangeMinimumValue
var minimum = schema.Minimum;
if (minimum.HasValue && schema.IsExclusiveMinimum)
{
if (propertyFormat == JsonFormatStrings.Integer || propertyFormat == JsonFormatStrings.Long)
if (propertyFormat is JsonFormatStrings.Integer or JsonFormatStrings.Long)
{
minimum++;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public string RangeMaximumValue
var maximum = schema.Maximum;
if (maximum.HasValue && schema.IsExclusiveMaximum)
{
if (propertyFormat == JsonFormatStrings.Integer || propertyFormat == JsonFormatStrings.Long)
if (propertyFormat is JsonFormatStrings.Integer or JsonFormatStrings.Long)
{
maximum--;
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public bool RenderRegularExpressionAttribute

private string? GetSchemaFormat(JsonSchema schema)
{
if (Type == "long" || Type == "long?")
if (Type is "long" or "long?")
{
return JsonFormatStrings.Long;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,56 +213,17 @@ private static bool IsDateTime(string? format, TypeScriptDateTimeType type)
// TODO: Make this more generic (see TypeScriptTypeResolver.ResolveString)
if (type == TypeScriptDateTimeType.Date)
{
if (format == JsonFormatStrings.DateTime)
{
return true;
}

if (format == JsonFormatStrings.Time)
{
return false;
}

if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return false;
}
return format == JsonFormatStrings.DateTime;
}
else if (type == TypeScriptDateTimeType.DayJS ||
type == TypeScriptDateTimeType.MomentJS ||
type == TypeScriptDateTimeType.OffsetMomentJS)
{
if (format == JsonFormatStrings.DateTime)
{
return true;
}

if (format == JsonFormatStrings.Time)
{
return true;
}

if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return true;
}
}
else if (type == TypeScriptDateTimeType.Luxon)
if (type is TypeScriptDateTimeType.DayJS or TypeScriptDateTimeType.MomentJS or TypeScriptDateTimeType.OffsetMomentJS)
{
if (format == JsonFormatStrings.DateTime)
{
return true;
}

if (format == JsonFormatStrings.Time)
{
return true;
}
return format is JsonFormatStrings.DateTime or JsonFormatStrings.Time or JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan;
}

if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return true;
}
if (type == TypeScriptDateTimeType.Luxon)
{
return format is JsonFormatStrings.DateTime or JsonFormatStrings.Time or JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan;
}
return false;
}
Expand All @@ -273,26 +234,17 @@ private static bool IsDate(string? format, TypeScriptDateTimeType type)
// TODO: Make this more generic (see TypeScriptTypeResolver.ResolveString)
if (type == TypeScriptDateTimeType.Date)
{
if (format == JsonFormatStrings.Date)
{
return true;
}
return format == JsonFormatStrings.Date;
}
else if (type == TypeScriptDateTimeType.DayJS ||
type == TypeScriptDateTimeType.MomentJS ||
type == TypeScriptDateTimeType.OffsetMomentJS)

if (type is TypeScriptDateTimeType.DayJS or TypeScriptDateTimeType.MomentJS or TypeScriptDateTimeType.OffsetMomentJS)
{
if (format == JsonFormatStrings.Date)
{
return true;
}
return format == JsonFormatStrings.Date;
}
else if (type == TypeScriptDateTimeType.Luxon)

if (type == TypeScriptDateTimeType.Luxon)
{
if (format == JsonFormatStrings.Date)
{
return true;
}
return format == JsonFormatStrings.Date;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ClassTemplateModel(string typeName, string discriminatorName,
public string? BaseDiscriminator => _schema.ResponsibleDiscriminatorObject?.PropertyName;

/// <summary>Gets a value indicating whether the class has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) &&
public bool HasDescription => _schema is not JsonSchemaProperty &&
(!string.IsNullOrEmpty(_schema.Description) ||
!string.IsNullOrEmpty(_schema.ActualTypeSchema.Description));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public EnumTemplateModel(string typeName, JsonSchema schema, TypeScriptGenerator
public string Name { get; }

/// <summary>Gets a value indicating whether the enum has description.</summary>
public bool HasDescription => !(_schema is JsonSchemaProperty) && !string.IsNullOrEmpty(_schema.Description);
public bool HasDescription => _schema is not JsonSchemaProperty && !string.IsNullOrEmpty(_schema.Description);

/// <summary>Gets the description.</summary>
public string Description => ConversionUtilities.RemoveLineBreaks(_schema.Description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ private string ResolveString(JsonSchema schema, string? typeNameHint)
return "string";
}
}
else if (Settings.DateTimeType == TypeScriptDateTimeType.MomentJS ||
Settings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS)
else if (Settings.DateTimeType is TypeScriptDateTimeType.MomentJS or TypeScriptDateTimeType.OffsetMomentJS)
{
if (schema.Format == JsonFormatStrings.Date)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ schema.Format is not null &&
}
}

var isOptional = (schema as JsonSchemaProperty)?.IsRequired == false;
if (schema != null && allowsNull == false && isOptional == false)
if (!allowsNull && schema is JsonSchemaProperty { IsRequired: false } == false)
{
if (typeResolver.GeneratesType(schema) &&
!schema.ActualTypeSchema.IsEnumeration &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public JsonExceptionConverter(bool hideStackTrace, IDictionary<string, Assembly>
/// <param name="serializer">The calling serializer.</param>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var exception = value as Exception;
if (exception != null)
if (value is Exception exception)
{
var resolver = serializer.ContractResolver as DefaultContractResolver ?? _defaultContractResolver;

Expand All @@ -62,16 +61,13 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
}
};

if (value is not null)
foreach (var property in GetExceptionProperties(exception.GetType()))
{
foreach (var property in JsonExceptionConverter.GetExceptionProperties(value.GetType()))
var propertyValue = property.Key.GetValue(exception);
if (propertyValue != null)
{
var propertyValue = property.Key.GetValue(exception);
if (propertyValue != null)
{
jObject.AddFirst(new JProperty(resolver.GetResolvedPropertyName(property.Value),
JToken.FromObject(propertyValue, serializer)));
}
jObject.AddFirst(new JProperty(resolver.GetResolvedPropertyName(property.Value),
JToken.FromObject(propertyValue, serializer)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ public override bool CanConvert(Type objectType)
var discriminator = jObject.GetValue(_discriminator, StringComparison.OrdinalIgnoreCase)?.Value<string>();
var subtype = GetDiscriminatorType(jObject, objectType, discriminator);

var objectContract = serializer.ContractResolver.ResolveContract(subtype) as JsonObjectContract;
if (objectContract == null || objectContract.Properties.All(p => p.PropertyName != _discriminator))
if (serializer.ContractResolver.ResolveContract(subtype) is not JsonObjectContract objectContract || objectContract.Properties.All(p => p.PropertyName != _discriminator))
{
jObject.Remove(_discriminator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
var contract = settings.ResolveContract(contextualType.Type);

var allowedProperties = schemaGenerator.GetTypeProperties(contextualType.Type);
var objectContract = contract as JsonObjectContract;
if (objectContract != null && allowedProperties == null)
if (allowedProperties == null && contract is JsonObjectContract objectContract)
{
foreach (var jsonProperty in objectContract.Properties.Where(p => p.DeclaringType == contextualType.Type))
{
Expand Down Expand Up @@ -175,7 +174,7 @@ private void LoadPropertyOrField(JsonProperty jsonProperty, ContextualAccessorIn
.GetAttributes(true)
.FirstAssignableToTypeNameOrDefault("System.ComponentModel.DataAnnotations.RequiredAttribute");

var hasJsonNetAttributeRequired = jsonProperty.Required == Required.Always || jsonProperty.Required == Required.AllowNull;
var hasJsonNetAttributeRequired = jsonProperty.Required is Required.Always or Required.AllowNull;
var isDataContractMemberRequired = schemaGenerator.GetDataMemberAttribute(accessorInfo, parentType)?.IsRequired == true;

var hasRequiredAttribute = requiredAttribute != null;
Expand All @@ -184,9 +183,7 @@ private void LoadPropertyOrField(JsonProperty jsonProperty, ContextualAccessorIn
parentSchema.RequiredProperties.Add(propertyName);
}

var isNullable = propertyTypeDescription.IsNullable &&
hasRequiredAttribute == false &&
(jsonProperty.Required == Required.Default || jsonProperty.Required == Required.AllowNull);
var isNullable = propertyTypeDescription.IsNullable && !hasRequiredAttribute && jsonProperty.Required is Required.Default or Required.AllowNull;

var defaultValue = jsonProperty.DefaultValue;

Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/ConversionUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static string ConvertToStringLiteral(string input)
break;
default:
// ASCII printable character
if (c >= 0x20 && c <= 0x7e)
if (c is >= (char)0x20 and <= (char)0x7e)
{
literal.Append(c);
// As UTF16 escaped character
Expand Down
6 changes: 2 additions & 4 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription
}
}

if (typeDescription.Type == JsonObjectType.Number ||
typeDescription.Type == JsonObjectType.Integer)
if (typeDescription.Type is JsonObjectType.Number or JsonObjectType.Integer)
{
JsonSchemaGenerator.ApplyRangeAttribute(schema, contextualType.GetContextAttributes(true));

Expand Down Expand Up @@ -893,8 +892,7 @@ private void GenerateKnownTypes(Type type, JsonSchemaResolver schemaResolver)
var methodInfo = type.GetRuntimeMethod((string)attribute.MethodName, Type.EmptyTypes);
if (methodInfo != null)
{
var knownTypes = methodInfo.Invoke(null, null) as IEnumerable<Type>;
if (knownTypes != null)
if (methodInfo.Invoke(null, null) is IEnumerable<Type> knownTypes)
{
foreach (var knownType in knownTypes)
{
Expand Down
Loading