Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables null reference types #1569

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Interfaces/IOpenApiExtensible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public interface IOpenApiExtensible : IOpenApiElement
/// <summary>
/// Specification extensions.
/// </summary>
IDictionary<string, IOpenApiExtension> Extensions { get; set; }
IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Microsoft.OpenApi/Interfaces/IOpenApiReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IOpenApiReader
/// <param name="settings"> The OpenApi reader settings.</param>
/// <param name="cancellationToken">Propagates notification that an operation should be cancelled.</param>
/// <returns></returns>
Task<ReadResult> ReadAsync(TextReader input, OpenApiReaderSettings settings = null, CancellationToken cancellationToken = default);
Task<ReadResult> ReadAsync(TextReader input, OpenApiReaderSettings? settings = null, CancellationToken cancellationToken = default);

/// <summary>
/// Parses the JsonNode input into an Open API document.
Expand All @@ -31,7 +31,7 @@ public interface IOpenApiReader
/// <param name="cancellationToken">Propagates notifications that operations should be cancelled.</param>
/// <param name="format">The OpenAPI format.</param>
/// <returns></returns>
Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string format = null, CancellationToken cancellationToken = default);
Task<ReadResult> ReadAsync(JsonNode jsonNode, OpenApiReaderSettings settings, string? format = null, CancellationToken cancellationToken = default);

/// <summary>
/// Reads the TextReader input and parses the fragment of an OpenAPI description into an Open API Element.
Expand All @@ -41,7 +41,7 @@ public interface IOpenApiReader
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
/// <returns>Instance of newly created IOpenApiElement.</returns>
T ReadFragment<T>(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
T ReadFragment<T>(TextReader input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement;

/// <summary>
/// Reads the JsonNode input and parses the fragment of an OpenAPI description into an Open API Element.
Expand All @@ -51,6 +51,6 @@ public interface IOpenApiReader
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <param name="settings">The OpenApiReader settings.</param>
/// <returns>Instance of newly created IOpenApiElement.</returns>
T ReadFragment<T>(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings settings = null) where T : IOpenApiElement;
T ReadFragment<T>(JsonNode input, OpenApiSpecVersion version, out OpenApiDiagnostic diagnostic, OpenApiReaderSettings? settings = null) where T : IOpenApiElement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IOpenApiReferenceable : IOpenApiSerializable
/// <summary>
/// Reference object.
/// </summary>
OpenApiReference Reference { get; set; }
OpenApiReference? Reference { get; set; }

/// <summary>
/// Serialize to OpenAPI V31 document without using reference.
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.6.11</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
Expand Down
21 changes: 12 additions & 9 deletions src/Microsoft.OpenApi/Models/OpenApiCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class OpenApiCallback : IOpenApiReferenceable, IOpenApiExtensible, IEffec
/// <summary>
/// A Path Item Object used to define a callback request and expected responses.
/// </summary>
public virtual Dictionary<RuntimeExpression, OpenApiPathItem> PathItems { get; set; }
public virtual Dictionary<RuntimeExpression, OpenApiPathItem>? PathItems { get; set; }
= new();

/// <summary>
Expand All @@ -28,12 +28,12 @@ public class OpenApiCallback : IOpenApiReferenceable, IOpenApiExtensible, IEffec
/// <summary>
/// Reference pointer.
/// </summary>
public OpenApiReference Reference { get; set; }
public OpenApiReference? Reference { get; set; }

/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public virtual IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
public virtual IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
Expand All @@ -45,9 +45,9 @@ public OpenApiCallback() { }
/// </summary>
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<string, IOpenApiExtension>(callback.Extensions) : null;
}

Expand Down Expand Up @@ -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();
}
Expand Down
46 changes: 23 additions & 23 deletions src/Microsoft.OpenApi/Models/OpenApiComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,60 +19,60 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
/// <summary>
/// An object to hold reusable <see cref="JsonSchema"/> Objects.
/// </summary>
public IDictionary<string, JsonSchema> Schemas { get; set; } = new Dictionary<string, JsonSchema>();
public IDictionary<string, JsonSchema>? Schemas { get; set; } = new Dictionary<string, JsonSchema>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiResponse"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiResponse> Responses { get; set; } = new Dictionary<string, OpenApiResponse>();
public virtual IDictionary<string, OpenApiResponse>? Responses { get; set; } = new Dictionary<string, OpenApiResponse>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiParameter"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiParameter> Parameters { get; set; } =
public virtual IDictionary<string, OpenApiParameter>? Parameters { get; set; } =
new Dictionary<string, OpenApiParameter>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiExample"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiExample> Examples { get; set; } = new Dictionary<string, OpenApiExample>();
public virtual IDictionary<string, OpenApiExample>? Examples { get; set; } = new Dictionary<string, OpenApiExample>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiRequestBody"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiRequestBody> RequestBodies { get; set; } =
public virtual IDictionary<string, OpenApiRequestBody>? RequestBodies { get; set; } =
new Dictionary<string, OpenApiRequestBody>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiHeader"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiHeader> Headers { get; set; } = new Dictionary<string, OpenApiHeader>();
public virtual IDictionary<string, OpenApiHeader>? Headers { get; set; } = new Dictionary<string, OpenApiHeader>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiSecurityScheme"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiSecurityScheme> SecuritySchemes { get; set; } =
public virtual IDictionary<string, OpenApiSecurityScheme>? SecuritySchemes { get; set; } =
new Dictionary<string, OpenApiSecurityScheme>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiLink"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiLink> Links { get; set; } = new Dictionary<string, OpenApiLink>();
public virtual IDictionary<string, OpenApiLink>? Links { get; set; } = new Dictionary<string, OpenApiLink>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiCallback"/> Objects.
/// </summary>
public virtual IDictionary<string, OpenApiCallback> Callbacks { get; set; } = new Dictionary<string, OpenApiCallback>();
public virtual IDictionary<string, OpenApiCallback>? Callbacks { get; set; } = new Dictionary<string, OpenApiCallback>();

/// <summary>
/// An object to hold reusable <see cref="OpenApiPathItem"/> Object.
/// </summary>
public virtual IDictionary<string, OpenApiPathItem> PathItems { get; set; } = new Dictionary<string, OpenApiPathItem>();
public virtual IDictionary<string, OpenApiPathItem>? PathItems { get; set; } = new Dictionary<string, OpenApiPathItem>();

/// <summary>
/// This object MAY be extended with Specification Extensions.
/// </summary>
public virtual IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
public virtual IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// Parameter-less constructor
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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();
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -331,7 +331,7 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
});

// extensions
writer.WriteExtensions(Extensions, version);
writer.WriteExtensions(Extensions!, version);
writer.WriteEndObject();
}

Expand All @@ -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();
Expand Down
Loading
Loading