diff --git a/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs b/src/Microsoft.OpenApi/Attributes/DisplayAttribute.cs
index a77b914cf..30670456d 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..8e4a6b3c4
--- /dev/null
+++ b/src/Microsoft.OpenApi/CallerArgumentExpressionAttribute.cs
@@ -0,0 +1,18 @@
+#if !NETCOREAPP3_1_OR_GREATER
+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; }
+}
+#endif
diff --git a/src/Microsoft.OpenApi/Error.cs b/src/Microsoft.OpenApi/Error.cs
deleted file mode 100644
index 29e1c7af2..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(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(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(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(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(Format(messageFormat, messageArgs));
- }
- }
-}
diff --git a/src/Microsoft.OpenApi/Expressions/BodyExpression.cs b/src/Microsoft.OpenApi/Expressions/BodyExpression.cs
index 92344dbf7..c09e06d4d 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 4bb53f94c..e5f92bed3 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 85ec5baac..b9c1cc618 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 53e86d4ba..322c34de1 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 4aa923b8c..7b5ddcffb 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 212282f2d..569eb375f 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 d4a116943..25792257e 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 9cb3f21ed..d6522ead3 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 a8fef9c3c..25d8b630e 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)
{
@@ -173,10 +163,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();
element.Serialize(stream, specVersion, format);
diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
index e945adc31..98799c9c6 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 5a653aa2a..31b7282f3 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 49394116a..a53f6ee1b 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 c1b4d913c..bb98be623 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 f55d9c205..b3cea1a8d 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 d5b0a2057..77d601a22 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 70815818a..d70bab01d 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 02945e0f9..8b7e95c41 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 8760ae93c..c591ee5b3 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 6e7fa9388..0e5fa4e8d 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;
@@ -207,10 +204,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 f8ae4e9d5..2066830d7 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 da5d1743d..a05c915d5 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 773c06566..fb406473d 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 cebfa995c..444f41ba5 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 7743bcb85..f7e330686 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 24fc0f797..dc8cf7bc0 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 a569c7d64..cdf18cddb 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 337767b2a..553fd3da5 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 2619b1461..6ea4b2d48 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 abb401ccf..e366bf10d 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 7ce175b2d..ff1a70b92 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 0ee768978..320ecc484 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 78536d99d..ccd8a0034 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs
@@ -297,10 +297,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;
@@ -496,10 +493,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;
@@ -560,10 +554,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);
@@ -634,10 +625,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 2ccb58a52..76728c957 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 2cddc0bce..620b79d3b 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)
{
@@ -166,10 +163,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 dcb2382a1..836303579 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 1eefee39b..fe5293cd4 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 43493a8ff..f47287182 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 78a6583c8..1b1abcdf7 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 43af59f5b..1ef1aaaa1 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 2d55e7038..e95d573b2 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..10d5595f8 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,9 +31,11 @@ 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(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/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs b/src/Microsoft.OpenApi/Validations/OpenApiValidator.cs
index 0d5489532..ae2c52721 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 ce9f03f03..5c930de1b 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 not 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 2c8fc7d68..05b6771ec 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 c39244745..16958727d 100644
--- a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs
+++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs
@@ -391,10 +391,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 1bdd5414e..8f4a866ae 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 a817dc203..460e33710 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
@@ -31,8 +32,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;
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]