diff --git a/src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs b/src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs
index 5531b1809..fabd1a177 100644
--- a/src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs
+++ b/src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs
@@ -13,6 +13,6 @@ public interface IOpenApiExtensible : IOpenApiElement
///
/// Specification extensions.
///
- IDictionary Extensions { get; set; }
+ IDictionary? Extensions { get; set; }
}
}
diff --git a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
index 5f8b1cb22..472a4f1de 100644
--- a/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
+++ b/src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
@@ -21,7 +21,7 @@ public interface IOpenApiReader
/// The OpenApi reader settings.
/// Propagates notification that an operation should be cancelled.
///
- Task ReadAsync(TextReader input, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default);
+ Task ReadAsync(TextReader input, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default);
///
/// Parses the JsonNode input into an Open API document.
@@ -31,7 +31,7 @@ public interface IOpenApiReader
/// Propagates notifications that operations should be cancelled.
/// The OpenAPI format.
///
- Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default);
+ Task ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string? format = null, CancellationToken cancellationToken = default);
///
/// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element.
@@ -41,7 +41,7 @@ public interface IOpenApiReader
/// Returns diagnostic object containing errors detected during parsing.
/// The OpenApiReader settings.
/// Instance of newly created IOpenApiElement.
- T ReadFragment(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
+ T ReadFragment(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement;
///
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
@@ -51,6 +51,6 @@ public interface IOpenApiReader
/// Returns diagnostic object containing errors detected during parsing.
/// The OpenApiReader settings.
/// Instance of newly created IOpenApiElement.
- T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
+ T ReadFragment(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement;
}
}
diff --git a/src/Microsoft.OpenApi/Interfaces/IOpenApiReferenceable.cs b/src/Microsoft.OpenApi/Interfaces/IOpenApiReferenceable.cs
index ceb5e1b7d..9392f6783 100644
--- a/src/Microsoft.OpenApi/Interfaces/IOpenApiReferenceable.cs
+++ b/src/Microsoft.OpenApi/Interfaces/IOpenApiReferenceable.cs
@@ -19,7 +19,7 @@ public interface IOpenApiReferenceable : IOpenApiSerializable
///
/// Reference object.
///
- OpenApiReference Reference { get; set; }
+ OpenApiReference? Reference { get; set; }
///
/// Serialize to OpenAPI V31 document without using reference.
diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
index b5db340ba..14eb865d7 100644
--- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
+++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
@@ -2,6 +2,7 @@
netstandard2.0
Latest
+ enable
true
1.6.11
.NET models with JSON and YAML writers for OpenAPI specification
diff --git a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
index 23910545b..b215576e5 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiCallback.cs
@@ -17,7 +17,7 @@ public class OpenApiCallback : IOpenApiReferenceable, IOpenApiExtensible, IEffec
///
/// A Path Item Object used to define a callback request and expected responses.
///
- public virtual Dictionary PathItems { get; set; }
+ public virtual Dictionary? PathItems { get; set; }
= new();
///
@@ -28,12 +28,12 @@ public class OpenApiCallback : IOpenApiReferenceable, IOpenApiExtensible, IEffec
///
/// Reference pointer.
///
- public OpenApiReference Reference { get; set; }
+ public OpenApiReference? Reference { get; set; }
///
/// This object MAY be extended with Specification Extensions.
///
- public virtual IDictionary Extensions { get; set; } = new Dictionary();
+ public virtual IDictionary? Extensions { get; set; } = new Dictionary();
///
/// Parameter-less constructor
@@ -45,9 +45,9 @@ public OpenApiCallback() { }
///
public OpenApiCallback(OpenApiCallback callback)
{
- PathItems = callback?.PathItems != null ? new(callback?.PathItems) : null;
+ PathItems = callback?.PathItems != null ? new(callback.PathItems) : null;
UnresolvedReference = callback?.UnresolvedReference ?? UnresolvedReference;
- Reference = callback?.Reference != null ? new(callback?.Reference) : null;
+ Reference = callback?.Reference != null ? new(callback.Reference) : null;
Extensions = callback?.Extensions != null ? new Dictionary(callback.Extensions) : null;
}
@@ -160,13 +160,16 @@ internal void SerializeInternalWithoutReference(IOpenApiWriter writer, OpenApiSp
writer.WriteStartObject();
// path items
- foreach (var item in PathItems)
+ if (PathItems != null)
{
- writer.WriteRequiredObject(item.Key.Expression, item.Value, callback);
- }
+ foreach (var item in PathItems)
+ {
+ writer.WriteRequiredObject(item.Key.Expression, item.Value, callback);
+ }
+ }
// extensions
- writer.WriteExtensions(Extensions, version);
+ writer.WriteExtensions(Extensions!, version);
writer.WriteEndObject();
}
diff --git a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
index 4af4248ab..5dad27afc 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiComponents.cs
@@ -19,60 +19,60 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
///
/// An object to hold reusable Objects.
///
- public IDictionary Schemas { get; set; } = new Dictionary();
+ public IDictionary? Schemas { get; set; } = new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Responses { get; set; } = new Dictionary();
+ public virtual IDictionary? Responses { get; set; } = new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Parameters { get; set; } =
+ public virtual IDictionary? Parameters { get; set; } =
new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Examples { get; set; } = new Dictionary();
+ public virtual IDictionary? Examples { get; set; } = new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary RequestBodies { get; set; } =
+ public virtual IDictionary? RequestBodies { get; set; } =
new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Headers { get; set; } = new Dictionary();
+ public virtual IDictionary? Headers { get; set; } = new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary SecuritySchemes { get; set; } =
+ public virtual IDictionary? SecuritySchemes { get; set; } =
new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Links { get; set; } = new Dictionary();
+ public virtual IDictionary? Links { get; set; } = new Dictionary();
///
/// An object to hold reusable Objects.
///
- public virtual IDictionary Callbacks { get; set; } = new Dictionary();
+ public virtual IDictionary? Callbacks { get; set; } = new Dictionary();
///
/// An object to hold reusable Object.
///
- public virtual IDictionary PathItems { get; set; } = new Dictionary();
+ public virtual IDictionary? PathItems { get; set; } = new Dictionary();
///
/// This object MAY be extended with Specification Extensions.
///
- public virtual IDictionary Extensions { get; set; } = new Dictionary();
+ public virtual IDictionary? Extensions { get; set; } = new Dictionary();
///
/// Parameter-less constructor
@@ -118,7 +118,7 @@ public void SerializeAsV31(IOpenApiWriter writer)
// pathItems - only present in v3.1
writer.WriteOptionalMap(
OpenApiConstants.PathItems,
- PathItems,
+ PathItems!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -170,7 +170,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// schemas
writer.WriteOptionalMap(
OpenApiConstants.Schemas,
- Schemas,
+ Schemas!,
(w, key, s) =>
{
var reference = s.GetRef();
@@ -188,7 +188,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// responses
writer.WriteOptionalMap(
OpenApiConstants.Responses,
- Responses,
+ Responses!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -206,7 +206,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// parameters
writer.WriteOptionalMap(
OpenApiConstants.Parameters,
- Parameters,
+ Parameters!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -224,7 +224,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// examples
writer.WriteOptionalMap(
OpenApiConstants.Examples,
- Examples,
+ Examples!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -242,7 +242,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// requestBodies
writer.WriteOptionalMap(
OpenApiConstants.RequestBodies,
- RequestBodies,
+ RequestBodies!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -261,7 +261,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// headers
writer.WriteOptionalMap(
OpenApiConstants.Headers,
- Headers,
+ Headers!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -279,7 +279,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// securitySchemes
writer.WriteOptionalMap(
OpenApiConstants.SecuritySchemes,
- SecuritySchemes,
+ SecuritySchemes!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -297,7 +297,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// links
writer.WriteOptionalMap(
OpenApiConstants.Links,
- Links,
+ Links!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -315,7 +315,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// callbacks
writer.WriteOptionalMap(
OpenApiConstants.Callbacks,
- Callbacks,
+ Callbacks!,
(w, key, component) =>
{
if (component.Reference != null &&
@@ -331,7 +331,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
});
// extensions
- writer.WriteExtensions(Extensions, version);
+ writer.WriteExtensions(Extensions!, version);
writer.WriteEndObject();
}
@@ -343,7 +343,7 @@ private void RenderComponents(IOpenApiWriter writer, OpenApiSpecVersion version)
{
writer.WriteOptionalMap(
OpenApiConstants.Schemas,
- Schemas,
+ Schemas!,
(w, key, s) => { w.WriteJsonSchema(s, version); });
}
writer.WriteEndObject();
diff --git a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
index d42f46638..ca1e276d6 100644
--- a/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
+++ b/src/Microsoft.OpenApi/Models/OpenApiDocument.cs
@@ -239,7 +239,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
if (loops.TryGetValue(typeof(JsonSchema), out List
public class OpenApiExternalDocs : IOpenApiSerializable, IOpenApiExtensible
{
+ private const string DefaultUrl = "https://example.com";
+
///
/// A short description of the target documentation.
///
- public string Description { get; set; }
+ public string? Description { get; set; }
///
/// REQUIRED. The URL for the target documentation. Value MUST be in the format of a URL.
///
- public Uri Url { get; set; }
+ public Uri Url { get; set; } = new Uri(DefaultUrl);
///
/// This object MAY be extended with Specification Extensions.
///
- public IDictionary Extensions { get; set; } = new Dictionary();
+ public IDictionary? Extensions { get; set; } = new Dictionary();
///
/// Parameter-less constructor
@@ -39,7 +41,7 @@ public OpenApiExternalDocs() { }
public OpenApiExternalDocs(OpenApiExternalDocs externalDocs)
{
Description = externalDocs?.Description ?? Description;
- Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString, UriKind.RelativeOrAbsolute) : null;
+ Url = externalDocs?.Url != null ? new Uri(externalDocs.Url.OriginalString, UriKind.RelativeOrAbsolute) : new Uri(DefaultUrl);
Extensions = externalDocs?.Extensions != null ? new Dictionary(externalDocs.Extensions) : null;
}
@@ -74,13 +76,16 @@ private void WriteInternal(IOpenApiWriter writer, OpenApiSpecVersion specVersion
writer.WriteStartObject();
// description
- writer.WriteProperty(OpenApiConstants.Description, Description);
+ if (!string.IsNullOrEmpty(Description))
+ {
+ writer.WriteProperty(OpenApiConstants.Description, Description!);
+ }
// url
- writer.WriteProperty(OpenApiConstants.Url, Url?.OriginalString);
+ writer.WriteProperty(OpenApiConstants.Url, Url.OriginalString);
// extensions
- writer.WriteExtensions(Extensions, specVersion);
+ writer.WriteExtensions(Extensions!, specVersion);
writer.WriteEndObject();
}
diff --git a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
index e2ec7bdc9..b0ddd5a61 100644
--- a/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
+++ b/src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs
@@ -31,7 +31,7 @@ static OpenApiModelFactory()
/// The path to the OpenAPI file.
/// The OpenApi reader settings.
/// An OpenAPI document instance.
- public static ReadResult Load(string url, OpenApiReaderSettings settings = null)
+ public static ReadResult Load(string url, OpenApiReaderSettings? settings = null)
{
return LoadAsync(url, settings).GetAwaiter().GetResult();
}
@@ -45,7 +45,7 @@ public static ReadResult Load(string url, OpenApiReaderSettings settings = null)
/// An OpenAPI document instance.
public static ReadResult Load(Stream stream,
string format,
- OpenApiReaderSettings settings = null)
+ OpenApiReaderSettings? settings = null)
{
settings ??= new OpenApiReaderSettings();
@@ -67,7 +67,7 @@ public static ReadResult Load(Stream stream,
/// An OpenAPI document instance.
public static ReadResult Load(TextReader input,
string format,
- OpenApiReaderSettings settings = null)
+ OpenApiReaderSettings? settings = null)
{
return LoadAsync(input, format, settings).GetAwaiter().GetResult();
}
@@ -78,11 +78,11 @@ public static ReadResult Load(TextReader input,
/// The path to the OpenAPI file
/// The OpenApi reader settings.
///
- public static async Task LoadAsync(string url, OpenApiReaderSettings settings = null)
+ public static async Task LoadAsync(string url, OpenApiReaderSettings? settings = null)
{
var format = GetFormat(url);
var stream = await GetStream(url);
- return await LoadAsync(stream, format, settings);
+ return await LoadAsync(stream, format!, settings);
}
///
@@ -93,7 +93,7 @@ public static async Task LoadAsync(string url, OpenApiReaderSettings
/// Propagates notification that operations should be cancelled.
/// The Open API format
///
- public static async Task LoadAsync(Stream input, string format, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
+ public static async Task LoadAsync(Stream input, string format, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default)
{
Utils.CheckArgumentNull(format, nameof(format));
settings ??= new OpenApiReaderSettings();
@@ -124,7 +124,7 @@ public static async Task LoadAsync(Stream input, string format, Open
/// The OpenApi reader settings.
/// Propagates notification that operations should be cancelled.
///
- public static async Task LoadAsync(TextReader input, string format, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default)
+ public static async Task LoadAsync(TextReader input, string format, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default)
{
Utils.CheckArgumentNull(format, nameof(format));
var reader = OpenApiReaderRegistry.GetReader(format);
@@ -139,8 +139,8 @@ public static async Task LoadAsync(TextReader input, string format,
/// The OpenApi reader settings.
/// An OpenAPI document instance.
public static ReadResult Parse(string input,
- string format = null,
- OpenApiReaderSettings settings = null)
+ string? format = null,
+ OpenApiReaderSettings? settings = null)
{
format ??= OpenApiConstants.Json;
settings ??= new OpenApiReaderSettings();
@@ -160,8 +160,8 @@ public static ReadResult Parse(string input,
public static T Parse(string input,
OpenApiSpecVersion version,
out OpenApiDiagnostic diagnostic,
- string format = null,
- OpenApiReaderSettings settings = null) where T : IOpenApiElement
+ string? format = null,
+ OpenApiReaderSettings? settings = null) where T : IOpenApiElement
{
format ??= OpenApiConstants.Json;
settings ??= new OpenApiReaderSettings();
@@ -179,12 +179,12 @@ public static T Parse(string input,
/// The OpenApiReader settings.
/// Instance of newly created IOpenApiElement.
/// The OpenAPI element.
- public static T Load(string url, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
+ public static T Load(string url, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement
{
var format = GetFormat(url);
settings ??= new OpenApiReaderSettings();
var stream = GetStream(url).GetAwaiter().GetResult();
- return Load(stream, version, format, out diagnostic, settings);
+ return Load(stream, version, format!, out diagnostic, settings);
}
///
@@ -198,7 +198,7 @@ public static T Load(string url, OpenApiSpecVersion version, out OpenApiDiagn
/// The OpenApiReader settings.
/// Instance of newly created IOpenApiElement.
/// The OpenAPI element.
- public static T Load(Stream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement
+ public static T Load(Stream input, OpenApiSpecVersion version, string format, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement
{
format ??= OpenApiConstants.Json;
using var reader = new StreamReader(input);
@@ -216,14 +216,14 @@ public static T Load(Stream input, OpenApiSpecVersion version, string format,
/// The OpenApiReader settings.
/// Instance of newly created IOpenApiElement.
/// The OpenAPI element.
- public static T Load(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, string format, OpenApiReaderSettings settings = null) where T : IOpenApiElement
+ public static T Load(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, string format, OpenApiReaderSettings? settings = null) where T : IOpenApiElement
{
format ??= OpenApiConstants.Json;
return OpenApiReaderRegistry.GetReader(format).ReadFragment(input, version, out diagnostic, settings);
}
- private static string GetContentType(string url)
+ private static string? GetContentType(string url)
{
if (!string.IsNullOrEmpty(url))
{
@@ -240,7 +240,7 @@ private static string GetContentType(string url)
///
/// The input URL.
/// The OpenAPI format.
- public static string GetFormat(string url)
+ public static string? GetFormat(string url)
{
if (!string.IsNullOrEmpty(url))
{
@@ -250,7 +250,7 @@ public static string GetFormat(string url)
var path = new Uri(url);
var urlSuffix = path.Segments[path.Segments.Length - 1].Split('.').LastOrDefault();
- return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url).Split('/').LastOrDefault();
+ return !string.IsNullOrEmpty(urlSuffix) ? urlSuffix : GetContentType(url)?.Split('/').LastOrDefault();
}
else
{