From 57706d136b7de4082cf6b3cdead555aa6a8ceb61 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 3 Oct 2023 21:39:03 +1100 Subject: [PATCH 1/4] cleanup exception handling --- .../Attributes/DisplayAttribute.cs | 7 +- .../CallerArgumentExpressionAttribute.cs | 16 +++ src/Microsoft.OpenApi/Error.cs | 98 ------------------- .../Expressions/BodyExpression.cs | 5 +- .../Expressions/HeaderExpression.cs | 5 +- .../Expressions/PathExpression.cs | 5 +- .../Expressions/QueryExpression.cs | 5 +- .../Expressions/RequestExpression.cs | 2 +- .../Expressions/ResponseExpression.cs | 2 +- .../Expressions/RuntimeExpression.cs | 5 +- .../Extensions/OpenApiExtensibleExtensions.cs | 13 +-- .../OpenApiSerializableExtensions.cs | 21 +--- .../Models/OpenApiCallback.cs | 16 +-- .../Models/OpenApiComponents.cs | 5 +- .../Models/OpenApiContact.cs | 5 +- .../Models/OpenApiDiscriminator.cs | 5 +- .../Models/OpenApiDocument.cs | 10 +- .../Models/OpenApiEncoding.cs | 5 +- .../Models/OpenApiExample.cs | 5 +- .../Models/OpenApiExtensibleDictionary.cs | 10 +- .../Models/OpenApiExternalDocs.cs | 5 +- src/Microsoft.OpenApi/Models/OpenApiHeader.cs | 10 +- src/Microsoft.OpenApi/Models/OpenApiInfo.cs | 10 +- .../Models/OpenApiLicense.cs | 5 +- src/Microsoft.OpenApi/Models/OpenApiLink.cs | 5 +- .../Models/OpenApiMediaType.cs | 5 +- .../Models/OpenApiOAuthFlow.cs | 5 +- .../Models/OpenApiOAuthFlows.cs | 5 +- .../Models/OpenApiOperation.cs | 10 +- .../Models/OpenApiParameter.cs | 10 +- .../Models/OpenApiPathItem.cs | 10 +- .../Models/OpenApiReference.cs | 15 +-- .../Models/OpenApiRequestBody.cs | 5 +- .../Models/OpenApiResponse.cs | 10 +- src/Microsoft.OpenApi/Models/OpenApiSchema.cs | 20 +--- .../Models/OpenApiSecurityRequirement.cs | 10 +- .../Models/OpenApiSecurityScheme.cs | 10 +- src/Microsoft.OpenApi/Models/OpenApiServer.cs | 5 +- .../Models/OpenApiServerVariable.cs | 5 +- src/Microsoft.OpenApi/Models/OpenApiTag.cs | 10 +- src/Microsoft.OpenApi/Models/OpenApiXml.cs | 5 +- .../Models/RuntimeExpressionAnyWrapper.cs | 5 +- .../Services/OpenApiUrlTreeNode.cs | 18 ++-- src/Microsoft.OpenApi/Utils.cs | 9 +- .../Validations/OpenApiValidator.cs | 10 +- .../Validations/ValidationRule.cs | 10 +- .../Writers/OpenApiWriterAnyExtensions.cs | 43 ++------ .../Writers/OpenApiWriterBase.cs | 5 +- .../Writers/OpenApiWriterExtensions.cs | 20 +--- .../Resources.cs | 4 +- 50 files changed, 118 insertions(+), 426 deletions(-) create mode 100644 src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs delete mode 100644 src/Microsoft.OpenApi/Error.cs diff --git a/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs b/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs index db60448ea..5c9d48a59 100644 --- a/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs +++ b/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs @@ -17,12 +17,7 @@ public class DisplayAttribute : Attribute /// The display name. public DisplayAttribute(string name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); - } - - Name = name; + Name = Utils.CheckArgumentNullOrEmpty(name); } /// diff --git a/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs b/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs new file mode 100644 index 000000000..60ef93911 --- /dev/null +++ b/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs @@ -0,0 +1,16 @@ +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +namespace System.Runtime.CompilerServices; + +[ExcludeFromCodeCoverage] +[DebuggerNonUserCode] +[AttributeUsage(AttributeTargets.Parameter)] +sealed class CallerArgumentExpressionAttribute : + Attribute +{ + public CallerArgumentExpressionAttribute(string parameterName) => + ParameterName = parameterName; + + public string ParameterName { get; } +} diff --git a/src/Microsoft.OpenApi/Error.cs b/src/Microsoft.OpenApi/Error.cs deleted file mode 100644 index d0c41780d..000000000 --- a/src/Microsoft.OpenApi/Error.cs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. - -using System; -using System.Globalization; -using Microsoft.OpenApi.Properties; - -namespace Microsoft.OpenApi -{ - /// - /// Utility class for creating and unwrapping instances. - /// - internal static class Error - { - /// - /// Formats the specified resource string using . - /// - /// A composite format string. - /// An object array that contains zero or more objects to format. - /// The formatted string. - internal static string Format(string format, params object[] args) - { - return string.Format(CultureInfo.CurrentCulture, format, args); - } - - /// - /// Creates an with the provided properties. - /// - /// A composite format string explaining the reason for the exception. - /// An object array that contains zero or more objects to format. - /// The logged . - internal static ArgumentException Argument(string messageFormat, params object[] messageArgs) - { - return new ArgumentException(Format(messageFormat, messageArgs)); - } - - /// - /// Creates an with the provided properties. - /// - /// The name of the parameter that caused the current exception. - /// A composite format string explaining the reason for the exception. - /// An object array that contains zero or more objects to format. - /// The logged . - internal static ArgumentException Argument( - string parameterName, - string messageFormat, - params object[] messageArgs) - { - return new ArgumentException(Format(messageFormat, messageArgs), parameterName); - } - - /// - /// Creates an with the provided properties. - /// - /// The name of the parameter that caused the current exception. - /// The logged . - internal static ArgumentNullException ArgumentNull(string parameterName) - { - return new ArgumentNullException(parameterName); - } - - /// - /// Creates an with the provided properties. - /// - /// The name of the parameter that caused the current exception. - /// A composite format string explaining the reason for the exception. - /// An object array that contains zero or more objects to format. - /// The logged . - internal static ArgumentNullException ArgumentNull( - string parameterName, - string messageFormat, - params object[] messageArgs) - { - return new ArgumentNullException(parameterName, Format(messageFormat, messageArgs)); - } - - /// - /// Creates an with a default message. - /// - /// The name of the parameter that caused the current exception. - /// The logged . - internal static ArgumentException ArgumentNullOrWhiteSpace(string parameterName) - { - return Argument(parameterName, SRResource.ArgumentNullOrWhiteSpace, parameterName); - } - - /// - /// Creates an . - /// - /// A composite format string explaining the reason for the exception. - /// An object array that contains zero or more objects to format. - /// The logged . - internal static NotSupportedException NotSupported(string messageFormat, params object[] messageArgs) - { - return new NotSupportedException(Format(messageFormat, messageArgs)); - } - } -} diff --git a/src/Microsoft.OpenApi/Expressions/BodyExpression.cs b/src/Microsoft.OpenApi/Expressions/BodyExpression.cs index cd5c86bd9..8e5b53902 100644 --- a/src/Microsoft.OpenApi/Expressions/BodyExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/BodyExpression.cs @@ -33,10 +33,7 @@ public BodyExpression() public BodyExpression(JsonPointer pointer) : base(pointer?.ToString()) { - if (pointer == null) - { - throw Error.ArgumentNull(nameof(pointer)); - } + Utils.CheckArgumentNull(pointer); } /// diff --git a/src/Microsoft.OpenApi/Expressions/HeaderExpression.cs b/src/Microsoft.OpenApi/Expressions/HeaderExpression.cs index f6642cb08..bfabf218e 100644 --- a/src/Microsoft.OpenApi/Expressions/HeaderExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/HeaderExpression.cs @@ -20,10 +20,7 @@ public class HeaderExpression : SourceExpression public HeaderExpression(string token) : base(token) { - if (string.IsNullOrWhiteSpace(token)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(token)); - } + Utils.CheckArgumentNullOrEmpty(token); } /// diff --git a/src/Microsoft.OpenApi/Expressions/PathExpression.cs b/src/Microsoft.OpenApi/Expressions/PathExpression.cs index a92c8a569..199fb35cf 100644 --- a/src/Microsoft.OpenApi/Expressions/PathExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/PathExpression.cs @@ -20,10 +20,7 @@ public sealed class PathExpression : SourceExpression public PathExpression(string name) : base(name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); - } + Utils.CheckArgumentNullOrEmpty(name); } /// diff --git a/src/Microsoft.OpenApi/Expressions/QueryExpression.cs b/src/Microsoft.OpenApi/Expressions/QueryExpression.cs index 3277233b3..80ba415f1 100644 --- a/src/Microsoft.OpenApi/Expressions/QueryExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/QueryExpression.cs @@ -20,10 +20,7 @@ public sealed class QueryExpression : SourceExpression public QueryExpression(string name) : base(name) { - if (string.IsNullOrWhiteSpace(name)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); - } + Utils.CheckArgumentNullOrEmpty(name); } /// diff --git a/src/Microsoft.OpenApi/Expressions/RequestExpression.cs b/src/Microsoft.OpenApi/Expressions/RequestExpression.cs index 7ebf77471..2d6b6d983 100644 --- a/src/Microsoft.OpenApi/Expressions/RequestExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/RequestExpression.cs @@ -19,7 +19,7 @@ public sealed class RequestExpression : RuntimeExpression /// The source of the request. public RequestExpression(SourceExpression source) { - Source = source ?? throw Error.ArgumentNull(nameof(source)); + Source = Utils.CheckArgumentNull(source); } /// diff --git a/src/Microsoft.OpenApi/Expressions/ResponseExpression.cs b/src/Microsoft.OpenApi/Expressions/ResponseExpression.cs index b91370297..e0aedde81 100644 --- a/src/Microsoft.OpenApi/Expressions/ResponseExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/ResponseExpression.cs @@ -19,7 +19,7 @@ public sealed class ResponseExpression : RuntimeExpression /// The source of the response. public ResponseExpression(SourceExpression source) { - Source = source ?? throw Error.ArgumentNull(nameof(source)); + Source = Utils.CheckArgumentNull(source); } /// diff --git a/src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs b/src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs index 965572dfd..4f78c75d3 100644 --- a/src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs +++ b/src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs @@ -29,10 +29,7 @@ public abstract class RuntimeExpression : IEquatable /// The built runtime expression object. public static RuntimeExpression Build(string expression) { - if (string.IsNullOrWhiteSpace(expression)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(expression)); - } + Utils.CheckArgumentNullOrEmpty(expression); if (!expression.StartsWith(Prefix)) { diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs index 7656aad89..dbe96df89 100644 --- a/src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs @@ -23,22 +23,15 @@ public static class OpenApiExtensibleExtensions public static void AddExtension(this T element, string name, IOpenApiExtension any) where T : IOpenApiExtensible { - if (element == null) - { - throw Error.ArgumentNull(nameof(element)); - } - - if (string.IsNullOrWhiteSpace(name)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); - } + Utils.CheckArgumentNull(element); + Utils.CheckArgumentNullOrEmpty(name); if (!name.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix)) { throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name)); } - element.Extensions[name] = any ?? throw Error.ArgumentNull(nameof(any)); + element.Extensions[name] = Utils.CheckArgumentNull(any); } } } diff --git a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs index f60c5483b..4f1d108b3 100755 --- a/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs +++ b/src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs @@ -78,10 +78,7 @@ public static void Serialize( OpenApiWriterSettings settings) where T : IOpenApiSerializable { - if (stream == null) - { - throw Error.ArgumentNull(nameof(stream)); - } + Utils.CheckArgumentNull(stream); var streamWriter = new FormattingStreamWriter(stream, CultureInfo.InvariantCulture); @@ -105,15 +102,8 @@ public static void Serialize( public static void Serialize(this T element, IOpenApiWriter writer, OpenApiSpecVersion specVersion) where T : IOpenApiSerializable { - if (element == null) - { - throw Error.ArgumentNull(nameof(element)); - } - - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(element); + Utils.CheckArgumentNull(writer); switch (specVersion) { @@ -174,10 +164,7 @@ public static string Serialize( OpenApiFormat format) where T : IOpenApiSerializable { - if (element == null) - { - throw Error.ArgumentNull(nameof(element)); - } + Utils.CheckArgumentNull(element); using (var stream = new MemoryStream()) { diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs index e6aa5d075..46f7c6417 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs @@ -57,15 +57,8 @@ public OpenApiCallback(OpenApiCallback callback) /// The path item. public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem) { - if (expression == null) - { - throw Error.ArgumentNull(nameof(expression)); - } - - if (pathItem == null) - { - throw Error.ArgumentNull(nameof(pathItem)); - } + Utils.CheckArgumentNull(expression); + Utils.CheckArgumentNull(pathItem); if (PathItems == null) { @@ -80,10 +73,7 @@ public void AddPathItem(RuntimeExpression expression, OpenApiPathItem pathItem) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs index a6bfef594..c403782d7 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs @@ -93,10 +93,7 @@ public OpenApiComponents(OpenApiComponents components) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); // If references have been inlined we don't need the to render the components section // however if they have cycles, then we will need a component rendered diff --git a/src/Microsoft.OpenApi/Models/OpenApiContact.cs b/src/Microsoft.OpenApi/Models/OpenApiContact.cs index 113c91b13..c256249b5 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiContact.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiContact.cs @@ -68,10 +68,7 @@ public void SerializeAsV2(IOpenApiWriter writer) private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs b/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs index 9ae7f0e6a..0854dec33 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDiscriminator.cs @@ -41,10 +41,7 @@ public OpenApiDiscriminator(OpenApiDiscriminator discriminator) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs index d2d9cf893..9b2f46deb 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs @@ -96,10 +96,7 @@ public OpenApiDocument(OpenApiDocument document) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); @@ -141,10 +138,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs b/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs index 13b6e3a0a..81dab6628 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiEncoding.cs @@ -75,10 +75,7 @@ public OpenApiEncoding(OpenApiEncoding encoding) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull("writer"); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiExample.cs b/src/Microsoft.OpenApi/Models/OpenApiExample.cs index 4d091a361..b146268da 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExample.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExample.cs @@ -78,10 +78,7 @@ public OpenApiExample(OpenApiExample example) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs b/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs index 40c26d429..c156b25b5 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExtensibleDictionary.cs @@ -43,10 +43,7 @@ protected OpenApiExtensibleDictionary( /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); @@ -65,10 +62,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs b/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs index 91d750e6c..0531ef135 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiExternalDocs.cs @@ -61,10 +61,7 @@ public void SerializeAsV2(IOpenApiWriter writer) private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs index fb4411478..5fc78ae39 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiHeader.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiHeader.cs @@ -117,10 +117,7 @@ public OpenApiHeader(OpenApiHeader header) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; @@ -209,10 +206,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiInfo.cs b/src/Microsoft.OpenApi/Models/OpenApiInfo.cs index e903c8947..676e9d04a 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiInfo.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiInfo.cs @@ -72,10 +72,7 @@ public OpenApiInfo(OpenApiInfo info) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); @@ -108,10 +105,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiLicense.cs b/src/Microsoft.OpenApi/Models/OpenApiLicense.cs index e22441dc3..5df51db35 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiLicense.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiLicense.cs @@ -61,10 +61,7 @@ public void SerializeAsV2(IOpenApiWriter writer) private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiLink.cs b/src/Microsoft.OpenApi/Models/OpenApiLink.cs index 801ef2ce1..7e851c39f 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiLink.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiLink.cs @@ -86,10 +86,7 @@ public OpenApiLink(OpenApiLink link) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs index 63a58cd02..2f1923243 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiMediaType.cs @@ -65,10 +65,7 @@ public OpenApiMediaType(OpenApiMediaType mediaType) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs index a9cd2b7df..427164b84 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlow.cs @@ -62,10 +62,7 @@ public OpenApiOAuthFlow(OpenApiOAuthFlow oAuthFlow) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs index d9e71510c..78876c0fd 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs @@ -60,10 +60,7 @@ public OpenApiOAuthFlows(OpenApiOAuthFlows oAuthFlows) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs index 9336662d8..a83f7aef5 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiOperation.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiOperation.cs @@ -135,10 +135,7 @@ public OpenApiOperation(OpenApiOperation operation) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); @@ -195,10 +192,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index 3918f233b..f6627df23 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -177,10 +177,7 @@ public OpenApiParameter(OpenApiParameter parameter) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; @@ -277,10 +274,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; if (Reference != null) diff --git a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs index ddd358dc2..68e78738f 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiPathItem.cs @@ -90,10 +90,7 @@ public OpenApiPathItem(OpenApiPathItem pathItem) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; if (Reference != null) @@ -133,10 +130,7 @@ public OpenApiPathItem GetEffective(OpenApiDocument doc) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiReference.cs b/src/Microsoft.OpenApi/Models/OpenApiReference.cs index e8798cc64..54a54e7fa 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiReference.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiReference.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Writers; @@ -69,7 +70,7 @@ public string ReferenceV3 if (!Type.HasValue) { - throw Error.ArgumentNull(nameof(Type)); + throw new ArgumentNullException(nameof(Type)); } if (Type == ReferenceType.Tag) @@ -100,7 +101,7 @@ public string ReferenceV2 if (!Type.HasValue) { - throw Error.ArgumentNull(nameof(Type)); + throw new ArgumentNullException(nameof(Type)); } if (Type == ReferenceType.Tag) @@ -138,10 +139,7 @@ public OpenApiReference(OpenApiReference reference) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Type == ReferenceType.Tag) { @@ -170,10 +168,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Type == ReferenceType.Tag) { diff --git a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs index 70f1f742a..07b36b961 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs @@ -70,10 +70,7 @@ public OpenApiRequestBody(OpenApiRequestBody requestBody) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiResponse.cs b/src/Microsoft.OpenApi/Models/OpenApiResponse.cs index a173f6c1a..beef1deb2 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiResponse.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiResponse.cs @@ -75,10 +75,7 @@ public OpenApiResponse(OpenApiResponse response) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; @@ -144,10 +141,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var target = this; diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index b0ac2dbac..a7e0b9ffe 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -298,10 +298,7 @@ public OpenApiSchema(OpenApiSchema schema) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var settings = writer.GetSettings(); var target = this; @@ -498,10 +495,7 @@ internal void SerializeAsV2( ISet parentRequiredProperties, string propertyName) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); var settings = writer.GetSettings(); var target = this; @@ -563,10 +557,7 @@ internal void SerializeAsV2WithoutReference( internal void WriteAsItemsProperties(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); // type writer.WriteProperty(OpenApiConstants.Type, Type); @@ -637,10 +628,7 @@ internal void WriteAsSchemaProperties( ISet parentRequiredProperties, string propertyName) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); // format if (string.IsNullOrEmpty(Format)) diff --git a/src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs b/src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs index d2564daf2..d22af1df0 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs @@ -33,10 +33,7 @@ public OpenApiSecurityRequirement() /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); @@ -73,10 +70,7 @@ public void SerializeAsV3(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs index 64a65bf83..b37cc5a28 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSecurityScheme.cs @@ -100,10 +100,7 @@ public OpenApiSecurityScheme(OpenApiSecurityScheme securityScheme) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Reference != null) @@ -167,10 +164,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Reference != null) { diff --git a/src/Microsoft.OpenApi/Models/OpenApiServer.cs b/src/Microsoft.OpenApi/Models/OpenApiServer.cs index a13a6fb2d..cc949aa6d 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiServer.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiServer.cs @@ -56,10 +56,7 @@ public OpenApiServer(OpenApiServer server) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs index 4512f76dc..1e38e7156 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiServerVariable.cs @@ -54,10 +54,7 @@ public OpenApiServerVariable(OpenApiServerVariable serverVariable) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/OpenApiTag.cs b/src/Microsoft.OpenApi/Models/OpenApiTag.cs index a0bae5015..5c5bfd729 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiTag.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiTag.cs @@ -65,10 +65,7 @@ public OpenApiTag(OpenApiTag tag) /// public void SerializeAsV3(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Reference != null) { @@ -106,10 +103,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer) /// public void SerializeAsV2(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (Reference != null) { diff --git a/src/Microsoft.OpenApi/Models/OpenApiXml.cs b/src/Microsoft.OpenApi/Models/OpenApiXml.cs index 1fb18d508..20b0041d3 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiXml.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiXml.cs @@ -81,10 +81,7 @@ public void SerializeAsV2(IOpenApiWriter writer) private void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); writer.WriteStartObject(); diff --git a/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs b/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs index 1a1f12a18..2efe00e46 100644 --- a/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs +++ b/src/Microsoft.OpenApi/Models/RuntimeExpressionAnyWrapper.cs @@ -67,10 +67,7 @@ public RuntimeExpression Expression /// public void WriteValue(IOpenApiWriter writer) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (_any != null) { diff --git a/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs b/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs index 488a437bc..b1ecc36d0 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiUrlTreeNode.cs @@ -56,7 +56,7 @@ public class OpenApiUrlTreeNode /// true or false. public bool HasOperations(string label) { - Utils.CheckArgumentNullOrEmpty(label, nameof(label)); + Utils.CheckArgumentNullOrEmpty(label); return PathItems is not null && PathItems.TryGetValue(label, out var item) && item.Operations is not null && item.Operations.Any(); } @@ -87,8 +87,8 @@ public static OpenApiUrlTreeNode Create() /// The root node of the created directory structure. public static OpenApiUrlTreeNode Create(OpenApiDocument doc, string label) { - Utils.CheckArgumentNull(doc, nameof(doc)); - Utils.CheckArgumentNullOrEmpty(label, nameof(label)); + Utils.CheckArgumentNull(doc); + Utils.CheckArgumentNullOrEmpty(label); var root = Create(); @@ -113,8 +113,8 @@ public static OpenApiUrlTreeNode Create(OpenApiDocument doc, string label) /// Name tag for labelling related nodes in the directory structure. public void Attach(OpenApiDocument doc, string label) { - Utils.CheckArgumentNull(doc, nameof(doc)); - Utils.CheckArgumentNullOrEmpty(label, nameof(label)); + Utils.CheckArgumentNull(doc); + Utils.CheckArgumentNullOrEmpty(label); var paths = doc.Paths; if (paths != null) @@ -139,9 +139,9 @@ public OpenApiUrlTreeNode Attach(string path, OpenApiPathItem pathItem, string label) { - Utils.CheckArgumentNullOrEmpty(label, nameof(label)); - Utils.CheckArgumentNullOrEmpty(path, nameof(path)); - Utils.CheckArgumentNull(pathItem, nameof(pathItem)); + Utils.CheckArgumentNullOrEmpty(label); + Utils.CheckArgumentNullOrEmpty(path); + Utils.CheckArgumentNull(pathItem); if (path.StartsWith(RootPathSegment)) { @@ -218,7 +218,7 @@ private OpenApiUrlTreeNode Attach(IEnumerable segments, /// A dictionary of key value pairs that contain information about a node. public void AddAdditionalData(Dictionary> additionalData) { - Utils.CheckArgumentNull(additionalData, nameof(additionalData)); + Utils.CheckArgumentNull(additionalData); foreach (var item in additionalData) { diff --git a/src/Microsoft.OpenApi/Utils.cs b/src/Microsoft.OpenApi/Utils.cs index 07751a57c..e8b01b164 100644 --- a/src/Microsoft.OpenApi/Utils.cs +++ b/src/Microsoft.OpenApi/Utils.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System; +using System.Runtime.CompilerServices; namespace Microsoft.OpenApi { @@ -17,7 +18,9 @@ internal static class Utils /// The input value. /// The input parameter name. /// The input value. - internal static T CheckArgumentNull(T value, string parameterName) where T : class + internal static T CheckArgumentNull( + T value, + [CallerArgumentExpression("value")] string parameterName = "") { return value ?? throw new ArgumentNullException(parameterName, $"Value cannot be null: {parameterName}"); } @@ -28,7 +31,9 @@ internal static T CheckArgumentNull(T value, string parameterName) where T : /// The input string value. /// The input parameter name. /// The input value. - internal static string CheckArgumentNullOrEmpty(string value, string parameterName) + internal static string CheckArgumentNullOrEmpty( + string value, + [CallerArgumentExpression("value")] string parameterName = "") { return string.IsNullOrEmpty(value) ? throw new ArgumentNullException(parameterName, $"Value cannot be null or empty: {parameterName}") : value; } diff --git a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs index 84a054386..cd3f19fab 100644 --- a/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs +++ b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs @@ -55,10 +55,7 @@ public IEnumerable Warnings /// Error to register. public void AddError(OpenApiValidatorError error) { - if (error == null) - { - throw Error.ArgumentNull(nameof(error)); - } + Utils.CheckArgumentNull(error); _errors.Add(error); } @@ -69,10 +66,7 @@ public void AddError(OpenApiValidatorError error) /// Error to register. public void AddWarning(OpenApiValidatorWarning warning) { - if (warning == null) - { - throw Error.ArgumentNull(nameof(warning)); - } + Utils.CheckArgumentNull(warning); _warnings.Add(warning); } diff --git a/src/Microsoft.OpenApi/Validations/ValidationRule.cs b/src/Microsoft.OpenApi/Validations/ValidationRule.cs index fdbf5c330..8917c5fca 100644 --- a/src/Microsoft.OpenApi/Validations/ValidationRule.cs +++ b/src/Microsoft.OpenApi/Validations/ValidationRule.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System; +using System.Globalization; using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Properties; @@ -39,7 +40,7 @@ public class ValidationRule : ValidationRule where T : IOpenApiElement /// Action to perform the validation. public ValidationRule(Action validate) { - _validate = validate ?? throw Error.ArgumentNull(nameof(validate)); + _validate = Utils.CheckArgumentNull(validate); } internal override Type ElementType @@ -49,10 +50,7 @@ internal override Type ElementType internal override void Evaluate(IValidationContext context, object item) { - if (context == null) - { - throw Error.ArgumentNull(nameof(context)); - } + Utils.CheckArgumentNull(context); if (item == null) { @@ -61,7 +59,7 @@ internal override void Evaluate(IValidationContext context, object item) if (!(item is T)) { - throw Error.Argument(string.Format(SRResource.InputItemShouldBeType, typeof(T).FullName)); + throw new ArgumentException(string.Format(SRResource.InputItemShouldBeType, typeof(T).FullName)); } T typedItem = (T)item; diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs index 361da3b2a..f06114516 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs @@ -20,10 +20,7 @@ public static class OpenApiWriterAnyExtensions /// Version of the OpenAPI specification that that will be output. public static void WriteExtensions(this IOpenApiWriter writer, IDictionary extensions, OpenApiSpecVersion specVersion) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (extensions != null) { @@ -43,10 +40,7 @@ public static void WriteExtensions(this IOpenApiWriter writer, IDictionaryThe Any value public static void WriteAny(this IOpenApiWriter writer, T any) where T : IOpenApiAny { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); if (any == null) { @@ -79,15 +73,8 @@ public static void WriteAny(this IOpenApiWriter writer, T any) where T : IOpe private static void WriteArray(this IOpenApiWriter writer, OpenApiArray array) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } - - if (array == null) - { - throw Error.ArgumentNull(nameof(array)); - } + Utils.CheckArgumentNull(writer); + Utils.CheckArgumentNull(array); writer.WriteStartArray(); @@ -101,15 +88,8 @@ private static void WriteArray(this IOpenApiWriter writer, OpenApiArray array) private static void WriteObject(this IOpenApiWriter writer, OpenApiObject entity) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } - - if (entity == null) - { - throw Error.ArgumentNull(nameof(entity)); - } + Utils.CheckArgumentNull(writer); + Utils.CheckArgumentNull(entity); writer.WriteStartObject(); @@ -124,15 +104,8 @@ private static void WriteObject(this IOpenApiWriter writer, OpenApiObject entity private static void WritePrimitive(this IOpenApiWriter writer, IOpenApiPrimitive primitive) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } - - if (primitive == null) - { - throw Error.ArgumentNull(nameof(primitive)); - } + Utils.CheckArgumentNull(writer); + Utils.CheckArgumentNull(primitive); // The Spec version is meaning for the Any type, so it's ok to use the latest one. primitive.Write(writer, OpenApiSpecVersion.OpenApi3_0); diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs index fe24f460c..d10b1be8c 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs @@ -392,10 +392,7 @@ private bool IsScopeType(ScopeType type) /// property name protected void VerifyCanWritePropertyName(string name) { - if (name == null) - { - throw Error.ArgumentNull(nameof(name)); - } + Utils.CheckArgumentNull(name); if (Scopes.Count == 0) { diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs index 08aeb4efd..e9156b7d1 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs @@ -404,33 +404,21 @@ private static void CheckArguments(IOpenApiWriter writer, string name, Action { CheckArguments(writer, name); - if (action == null) - { - throw Error.ArgumentNull(nameof(action)); - } + Utils.CheckArgumentNull(action); } private static void CheckArguments(IOpenApiWriter writer, string name, Action action) { CheckArguments(writer, name); - if (action == null) - { - throw Error.ArgumentNull(nameof(action)); - } + Utils.CheckArgumentNull(action); } private static void CheckArguments(IOpenApiWriter writer, string name) { - if (writer == null) - { - throw Error.ArgumentNull(nameof(writer)); - } + Utils.CheckArgumentNull(writer); - if (string.IsNullOrWhiteSpace(name)) - { - throw Error.ArgumentNullOrWhiteSpace(nameof(name)); - } + Utils.CheckArgumentNullOrEmpty(name); } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/Resources.cs b/test/Microsoft.OpenApi.Readers.Tests/Resources.cs index 895e1ed3f..3c7184cc4 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/Resources.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/Resources.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System; using System.IO; namespace Microsoft.OpenApi.Readers.Tests @@ -33,8 +34,7 @@ public static Stream GetStream(string fileName) if (stream == null) { - string message = Error.Format("The embedded resource '{0}' was not found.", path); - throw new FileNotFoundException(message, path); + throw new FileNotFoundException($"The embedded resource '{path}' was not found.", path); } return stream; From 6f63c622aa72abb6f9fb4366c422bc1e219d2d8f Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 4 Oct 2023 06:36:47 +1100 Subject: [PATCH 2/4] Update src/Microsoft.OpenApi/Utils.cs Co-authored-by: Vincent Biret --- src/Microsoft.OpenApi/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/Utils.cs b/src/Microsoft.OpenApi/Utils.cs index e8b01b164..3ef7788ba 100644 --- a/src/Microsoft.OpenApi/Utils.cs +++ b/src/Microsoft.OpenApi/Utils.cs @@ -33,7 +33,7 @@ internal static T CheckArgumentNull( /// The input value. internal static string CheckArgumentNullOrEmpty( string value, - [CallerArgumentExpression("value")] string parameterName = "") + [CallerArgumentExpression(nameof(value))] string parameterName = "") { return string.IsNullOrEmpty(value) ? throw new ArgumentNullException(parameterName, $"Value cannot be null or empty: {parameterName}") : value; } From 5c05d56f0178126f4f80dd03eb858736cb66a88b Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 5 Oct 2023 21:11:16 +1100 Subject: [PATCH 3/4] Update CallerArgumentExpressionAttribute.cs --- src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs b/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs index 60ef93911..8e4a6b3c4 100644 --- a/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs +++ b/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +#if !NETCOREAPP3_1_OR_GREATER +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace System.Runtime.CompilerServices; @@ -14,3 +15,4 @@ public CallerArgumentExpressionAttribute(string parameterName) => public string ParameterName { get; } } +#endif From 9068e562f1cdfaff9e5424bd0efe6aca0423903a Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 5 Oct 2023 22:10:02 +1100 Subject: [PATCH 4/4] . --- src/Microsoft.OpenApi/Utils.cs | 2 +- .../Expressions/HeaderExpressionTests.cs | 2 +- test/Microsoft.OpenApi.Tests/Expressions/PathExpressionTests.cs | 2 +- .../Microsoft.OpenApi.Tests/Expressions/QueryExpressionTests.cs | 2 +- .../Expressions/RuntimeExpressionTests.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.OpenApi/Utils.cs b/src/Microsoft.OpenApi/Utils.cs index 3ef7788ba..10d5595f8 100644 --- a/src/Microsoft.OpenApi/Utils.cs +++ b/src/Microsoft.OpenApi/Utils.cs @@ -35,7 +35,7 @@ internal static string CheckArgumentNullOrEmpty( string value, [CallerArgumentExpression(nameof(value))] string parameterName = "") { - return string.IsNullOrEmpty(value) ? throw new ArgumentNullException(parameterName, $"Value cannot be null or empty: {parameterName}") : value; + return string.IsNullOrWhiteSpace(value) ? throw new ArgumentNullException(parameterName, $"Value cannot be null or empty: {parameterName}") : value; } } } diff --git a/test/Microsoft.OpenApi.Tests/Expressions/HeaderExpressionTests.cs b/test/Microsoft.OpenApi.Tests/Expressions/HeaderExpressionTests.cs index 859c409b6..abe1cf929 100644 --- a/test/Microsoft.OpenApi.Tests/Expressions/HeaderExpressionTests.cs +++ b/test/Microsoft.OpenApi.Tests/Expressions/HeaderExpressionTests.cs @@ -20,7 +20,7 @@ public void HeaderExpressionConstructorThrows(string token) Action test = () => new HeaderExpression(token); // Act - Assert.Throws("token", test); + Assert.Throws("token", test); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Expressions/PathExpressionTests.cs b/test/Microsoft.OpenApi.Tests/Expressions/PathExpressionTests.cs index 235b11102..3ff084f77 100644 --- a/test/Microsoft.OpenApi.Tests/Expressions/PathExpressionTests.cs +++ b/test/Microsoft.OpenApi.Tests/Expressions/PathExpressionTests.cs @@ -20,7 +20,7 @@ public void PathExpressionConstructorThrows(string name) Action test = () => new PathExpression(name); // Act - Assert.Throws("name", test); + Assert.Throws("name", test); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Expressions/QueryExpressionTests.cs b/test/Microsoft.OpenApi.Tests/Expressions/QueryExpressionTests.cs index 3a741b80c..e4d215496 100644 --- a/test/Microsoft.OpenApi.Tests/Expressions/QueryExpressionTests.cs +++ b/test/Microsoft.OpenApi.Tests/Expressions/QueryExpressionTests.cs @@ -20,7 +20,7 @@ public void QueryExpressionConstructorThrows(string name) Action test = () => new QueryExpression(name); // Act - Assert.Throws("name", test); + Assert.Throws("name", test); } [Fact] diff --git a/test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs b/test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs index f9ada6dbd..9b43b93ec 100644 --- a/test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs +++ b/test/Microsoft.OpenApi.Tests/Expressions/RuntimeExpressionTests.cs @@ -25,7 +25,7 @@ public void BuildRuntimeExpressionThrowsNullOrWhiteSpace(string expression) Action test = () => RuntimeExpression.Build(expression); // Assert - Assert.Throws("expression", test); + Assert.Throws("expression", test); } [Theory]