Skip to content

Commit 38f35c2

Browse files
committed
feat: Added nullable enable to OpenApiOperation.
1 parent 9146269 commit 38f35c2

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public override void Visit(ref JsonSchema schema)
5454

5555
public override void Visit(OpenApiPathItem pathItem)
5656
{
57-
if (pathItem.Operations.TryGetValue(OperationType.Put, out var value))
57+
if (pathItem.Operations.TryGetValue(OperationType.Put, out var value) &&
58+
value.OperationId != null)
5859
{
5960
var operationId = value.OperationId;
6061
pathItem.Operations[OperationType.Put].OperationId = ResolvePutOperationId(operationId);
@@ -69,14 +70,14 @@ public override void Visit(OpenApiOperation operation)
6970
throw new ArgumentException($"OperationId is required {PathString}", nameof(operation));
7071

7172
var operationId = operation.OperationId;
72-
var operationTypeExtension = operation.Extensions.GetExtension("x-ms-docs-operation-type");
73+
var operationTypeExtension = operation.Extensions?.GetExtension("x-ms-docs-operation-type");
7374
if (operationTypeExtension.IsEquals("function"))
74-
operation.Parameters = ResolveFunctionParameters(operation.Parameters);
75+
operation.Parameters = ResolveFunctionParameters(operation.Parameters ?? new List<OpenApiParameter>());
7576

7677
// Order matters. Resolve operationId.
7778
operationId = RemoveHashSuffix(operationId);
7879
if (operationTypeExtension.IsEquals("action") || operationTypeExtension.IsEquals("function"))
79-
operationId = RemoveKeyTypeSegment(operationId, operation.Parameters);
80+
operationId = RemoveKeyTypeSegment(operationId, operation.Parameters ?? new List<OpenApiParameter>());
8081
operationId = SingularizeAndDeduplicateOperationId(operationId.SplitByChar('.'));
8182
operationId = ResolveODataCastOperationId(operationId);
8283
operationId = ResolveByRefOperationId(operationId);

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.OpenApi.Models.References;
99
using Microsoft.OpenApi.Writers;
1010

11+
#nullable enable
12+
1113
namespace Microsoft.OpenApi.Models
1214
{
1315
/// <summary>
@@ -24,51 +26,51 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible
2426
/// A list of tags for API documentation control.
2527
/// Tags can be used for logical grouping of operations by resources or any other qualifier.
2628
/// </summary>
27-
public IList<OpenApiTag> Tags { get; set; } = new List<OpenApiTag>();
29+
public IList<OpenApiTag>? Tags { get; set; } = new List<OpenApiTag>();
2830

2931
/// <summary>
3032
/// A short summary of what the operation does.
3133
/// </summary>
32-
public string Summary { get; set; }
34+
public string? Summary { get; set; }
3335

3436
/// <summary>
3537
/// A verbose explanation of the operation behavior.
3638
/// CommonMark syntax MAY be used for rich text representation.
3739
/// </summary>
38-
public string Description { get; set; }
40+
public string? Description { get; set; }
3941

4042
/// <summary>
4143
/// Additional external documentation for this operation.
4244
/// </summary>
43-
public OpenApiExternalDocs ExternalDocs { get; set; }
45+
public OpenApiExternalDocs? ExternalDocs { get; set; }
4446

4547
/// <summary>
4648
/// Unique string used to identify the operation. The id MUST be unique among all operations described in the API.
4749
/// Tools and libraries MAY use the operationId to uniquely identify an operation, therefore,
4850
/// it is RECOMMENDED to follow common programming naming conventions.
4951
/// </summary>
50-
public string OperationId { get; set; }
52+
public string? OperationId { get; set; }
5153

5254
/// <summary>
5355
/// A list of parameters that are applicable for this operation.
5456
/// If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
5557
/// The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location.
5658
/// The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.
5759
/// </summary>
58-
public IList<OpenApiParameter> Parameters { get; set; } = new List<OpenApiParameter>();
60+
public IList<OpenApiParameter>? Parameters { get; set; } = new List<OpenApiParameter>();
5961

6062
/// <summary>
6163
/// The request body applicable for this operation.
6264
/// The requestBody is only supported in HTTP methods where the HTTP 1.1 specification RFC7231
6365
/// has explicitly defined semantics for request bodies.
6466
/// In other cases where the HTTP spec is vague, requestBody SHALL be ignored by consumers.
6567
/// </summary>
66-
public OpenApiRequestBody RequestBody { get; set; }
68+
public OpenApiRequestBody? RequestBody { get; set; }
6769

6870
/// <summary>
6971
/// REQUIRED. The list of possible responses as they are returned from executing this operation.
7072
/// </summary>
71-
public OpenApiResponses Responses { get; set; } = new();
73+
public OpenApiResponses? Responses { get; set; } = new();
7274

7375
/// <summary>
7476
/// A map of possible out-of band callbacks related to the parent operation.
@@ -78,7 +80,7 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible
7880
/// The key value used to identify the callback object is an expression, evaluated at runtime,
7981
/// that identifies a URL to use for the callback operation.
8082
/// </summary>
81-
public IDictionary<string, OpenApiCallback> Callbacks { get; set; } = new Dictionary<string, OpenApiCallback>();
83+
public IDictionary<string, OpenApiCallback>? Callbacks { get; set; } = new Dictionary<string, OpenApiCallback>();
8284

8385
/// <summary>
8486
/// Declares this operation to be deprecated. Consumers SHOULD refrain from usage of the declared operation.
@@ -92,19 +94,19 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible
9294
/// This definition overrides any declared top-level security.
9395
/// To remove a top-level security declaration, an empty array can be used.
9496
/// </summary>
95-
public IList<OpenApiSecurityRequirement> Security { get; set; } = new List<OpenApiSecurityRequirement>();
97+
public IList<OpenApiSecurityRequirement>? Security { get; set; } = new List<OpenApiSecurityRequirement>();
9698

9799
/// <summary>
98100
/// An alternative server array to service this operation.
99101
/// If an alternative server object is specified at the Path Item Object or Root level,
100102
/// it will be overridden by this value.
101103
/// </summary>
102-
public IList<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
104+
public IList<OpenApiServer>? Servers { get; set; } = new List<OpenApiServer>();
103105

104106
/// <summary>
105107
/// This object MAY be extended with Specification Extensions.
106108
/// </summary>
107-
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
109+
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
108110

109111
/// <summary>
110112
/// Parameterless constructor
@@ -114,9 +116,9 @@ public OpenApiOperation() { }
114116
/// <summary>
115117
/// Initializes a copy of an <see cref="OpenApiOperation"/> object
116118
/// </summary>
117-
public OpenApiOperation(OpenApiOperation operation)
119+
public OpenApiOperation(OpenApiOperation? operation)
118120
{
119-
Tags = operation?.Tags != null ? new List<OpenApiTag>(operation?.Tags) : null;
121+
Tags = operation?.Tags != null ? new List<OpenApiTag>(operation.Tags) : null;
120122
Summary = operation?.Summary ?? Summary;
121123
Description = operation?.Description ?? Description;
122124
ExternalDocs = operation?.ExternalDocs != null ? new(operation?.ExternalDocs) : null;

test/Microsoft.OpenApi.Hidi.Tests/Formatters/PowerShellFormatterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void ResolveFunctionParameters()
8585
var walker = new OpenApiWalker(powerShellFormatter);
8686
walker.Walk(openApiDocument);
8787

88-
var idsParameter = openApiDocument.Paths?["/foo"].Operations[OperationType.Get].Parameters.Where(static p => p.Name == "ids").FirstOrDefault();
88+
var idsParameter = openApiDocument.Paths?["/foo"].Operations[OperationType.Get].Parameters?.Where(static p => p.Name == "ids").FirstOrDefault();
8989

9090
// Assert
9191
Assert.Null(idsParameter?.Content);

0 commit comments

Comments
 (0)