Skip to content

Commit a755dd8

Browse files
committed
chore: additional NRT fixes batch
Signed-off-by: Vincent Biret <[email protected]>
1 parent 176ff31 commit a755dd8

File tree

8 files changed

+91
-77
lines changed

8 files changed

+91
-77
lines changed

src/Microsoft.OpenApi.OData.Reader/Edm/ODataPath.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ internal bool SupportHttpMethod(string method)
204204
/// <returns>The whole path object.</returns>
205205
internal ODataPath Push(ODataSegment segment)
206206
{
207-
if (Segments == null)
208-
{
209-
Segments = new List<ODataSegment>();
210-
}
207+
Segments ??= [];
211208

212209
_pathKind = null;
213210
_defaultPathItemName = null;

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiParameterGenerator.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
456456
Utils.CheckArgumentNull(target, nameof(target));
457457
Utils.CheckArgumentNull(document, nameof(document));
458458

459-
SearchRestrictionsType search = context.Model.GetRecord<SearchRestrictionsType>(target, CapabilitiesConstants.SearchRestrictions);
460-
if (search == null || search.IsSearchable)
459+
if (context.Model.GetRecord<SearchRestrictionsType>(target, CapabilitiesConstants.SearchRestrictions) is not {} search || search.IsSearchable)
461460
{
462461
return new OpenApiParameterReference("search", document);
463462
}
@@ -497,8 +496,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
497496
Utils.CheckArgumentNull(target, nameof(target));
498497
Utils.CheckArgumentNull(document, nameof(document));
499498

500-
CountRestrictionsType count = context.Model.GetRecord<CountRestrictionsType>(target, CapabilitiesConstants.CountRestrictions);
501-
if (count == null || count.IsCountable)
499+
if (context.Model.GetRecord<CountRestrictionsType>(target, CapabilitiesConstants.CountRestrictions) is not {} count || count.IsCountable)
502500
{
503501
return new OpenApiParameterReference("count", document);
504502
}
@@ -539,8 +537,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
539537
Utils.CheckArgumentNull(target, nameof(target));
540538
Utils.CheckArgumentNull(document, nameof(document));
541539

542-
FilterRestrictionsType filter = context.Model.GetRecord<FilterRestrictionsType>(target, CapabilitiesConstants.FilterRestrictions);
543-
if (filter == null || filter.IsFilterable)
540+
if (context.Model.GetRecord<FilterRestrictionsType>(target, CapabilitiesConstants.FilterRestrictions) is not {} filter || filter.IsFilterable)
544541
{
545542
return new OpenApiParameterReference("filter", document);
546543
}
@@ -630,7 +627,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
630627
Utils.CheckArgumentNull(target, nameof(target));
631628
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
632629

633-
SortRestrictionsType sort = context.Model.GetRecord<SortRestrictionsType>(target, CapabilitiesConstants.SortRestrictions);
630+
var sort = context.Model.GetRecord<SortRestrictionsType>(target, CapabilitiesConstants.SortRestrictions);
634631
if (sort != null && !sort.IsSortable)
635632
{
636633
return null;
@@ -746,7 +743,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
746743
Utils.CheckArgumentNull(target, nameof(target));
747744
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
748745

749-
NavigationRestrictionsType navigation = context.Model.GetRecord<NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);
746+
var navigation = context.Model.GetRecord<NavigationRestrictionsType>(target, CapabilitiesConstants.NavigationRestrictions);
750747
if (navigation != null && !navigation.IsNavigable)
751748
{
752749
return null;
@@ -852,7 +849,7 @@ public static void AppendParameter(this IList<IOpenApiParameter> parameters, IOp
852849
Utils.CheckArgumentNull(target, nameof(target));
853850
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
854851

855-
ExpandRestrictionsType expand = context.Model.GetRecord<ExpandRestrictionsType>(target, CapabilitiesConstants.ExpandRestrictions);
852+
var expand = context.Model.GetRecord<ExpandRestrictionsType>(target, CapabilitiesConstants.ExpandRestrictions);
856853
if (expand != null && !expand.IsExpandable)
857854
{
858855
return null;

src/Microsoft.OpenApi.OData.Reader/Operation/DollarCountGetOperationHandler.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,20 @@ protected override void SetParameters(OpenApiOperation operation)
187187
{
188188
base.SetParameters(operation);
189189

190-
IOpenApiParameter parameter;
191190

192-
parameter = Context.CreateSearch(TargetPath, _document) ?? (annotatables.Count == 0 ? null : annotatables.Select(x => Context.CreateSearch(x, _document)).FirstOrDefault(static x => x is not null));
191+
var parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context?.CreateSearch(TargetPath, _document)) ??
192+
(annotatables.Count == 0 ? null : annotatables.Select(x => Context?.CreateSearch(x, _document)).FirstOrDefault(static x => x is not null));
193193
if (parameter != null)
194194
{
195+
operation.Parameters ??= [];
195196
operation.Parameters.Add(parameter);
196197
}
197198

198-
parameter = Context.CreateFilter(TargetPath, _document) ?? (annotatables.Count == 0 ? null : annotatables.Select(x => Context.CreateFilter(x, _document)).FirstOrDefault(static x => x is not null));
199+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context?.CreateFilter(TargetPath, _document)) ??
200+
(annotatables.Count == 0 ? null : annotatables.Select(x => Context?.CreateFilter(x, _document)).FirstOrDefault(static x => x is not null));
199201
if (parameter != null)
200202
{
203+
operation.Parameters ??= [];
201204
operation.Parameters.Add(parameter);
202205
}
203206
}

src/Microsoft.OpenApi.OData.Reader/Operation/EntitySetGetOperationHandler.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
7+
using System.Collections.Generic;
68
using System.Linq;
79
using System.Net.Http;
810
using System.Text.Json.Nodes;
911
using Microsoft.OData.Edm;
1012
using Microsoft.OpenApi.Any;
13+
using Microsoft.OpenApi.Interfaces;
1114
using Microsoft.OpenApi.Models;
1215
using Microsoft.OpenApi.Models.References;
1316
using Microsoft.OpenApi.OData.Common;
@@ -36,16 +39,20 @@ public EntitySetGetOperationHandler(OpenApiDocument document) : base(document)
3639
/// <inheritdoc/>
3740
public override HttpMethod OperationType => HttpMethod.Get;
3841

39-
private ReadRestrictionsType _readRestrictions;
42+
private ReadRestrictionsType? _readRestrictions;
4043

4144
protected override void Initialize(ODataContext context, ODataPath path)
4245
{
4346
base.Initialize(context, path);
4447

45-
_readRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
46-
var entityReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(EntitySet, CapabilitiesConstants.ReadRestrictions);
47-
_readRestrictions?.MergePropertiesIfNull(entityReadRestrictions);
48-
_readRestrictions ??= entityReadRestrictions;
48+
if (!string.IsNullOrEmpty(TargetPath))
49+
_readRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
50+
if (Context is not null)
51+
{
52+
var entityReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(EntitySet, CapabilitiesConstants.ReadRestrictions);
53+
_readRestrictions?.MergePropertiesIfNull(entityReadRestrictions);
54+
_readRestrictions ??= entityReadRestrictions;
55+
}
4956
}
5057

5158
/// <inheritdoc/>
@@ -54,10 +61,10 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5461
// Summary and Descriptions
5562
string placeHolder = "Get entities from " + EntitySet.Name;
5663
operation.Summary = _readRestrictions?.Description ?? placeHolder;
57-
operation.Description = _readRestrictions?.LongDescription ?? Context.Model.GetDescriptionAnnotation(EntitySet);
64+
operation.Description = _readRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(EntitySet);
5865

5966
// OperationId
60-
if (Context.Settings.EnableOperationId)
67+
if (Context is {Settings.EnableOperationId: true})
6168
{
6269
string typeName = EntitySet.EntityType.Name;
6370
operation.OperationId = EntitySet.Name + "." + typeName + ".List" + Utils.UpperFirstChar(typeName);
@@ -66,13 +73,14 @@ protected override void SetBasicInfo(OpenApiOperation operation)
6673

6774
protected override void SetExtensions(OpenApiOperation operation)
6875
{
69-
if (Context.Settings.EnablePagination)
76+
if (Context is {Settings.EnablePagination: true})
7077
{
71-
JsonObject extension = new JsonObject
78+
var extension = new JsonObject
7279
{
7380
{ "nextLinkName", "@odata.nextLink"},
7481
{ "operationName", Context.Settings.PageableOperationName}
7582
};
83+
operation.Extensions ??= new Dictionary<string, IOpenApiExtension>();
7684
operation.Extensions.Add(Constants.xMsPageable, new OpenApiAny(extension));
7785

7886
base.SetExtensions(operation);
@@ -83,41 +91,43 @@ protected override void SetExtensions(OpenApiOperation operation)
8391
protected override void SetParameters(OpenApiOperation operation)
8492
{
8593
base.SetParameters(operation);
94+
if (Context is null) return;
8695

8796
// The parameters array contains Parameter Objects for all system query options allowed for this collection,
8897
// and it does not list system query options not allowed for this collection, see terms
8998
// Capabilities.TopSupported, Capabilities.SkipSupported, Capabilities.SearchRestrictions,
9099
// Capabilities.FilterRestrictions, and Capabilities.CountRestrictions
91100
// $top
92-
var parameter = Context.CreateTop(TargetPath, _document) ?? Context.CreateTop(EntitySet, _document);
101+
operation.Parameters ??= [];
102+
var parameter = (TargetPath is null ? null : Context.CreateTop(TargetPath, _document)) ?? Context.CreateTop(EntitySet, _document);
93103
if (parameter != null)
94104
{
95105
operation.Parameters.Add(parameter);
96106
}
97107

98108
// $skip
99-
parameter = Context.CreateSkip(TargetPath, _document) ?? Context.CreateSkip(EntitySet, _document);
109+
parameter = (TargetPath is null ? null : Context.CreateSkip(TargetPath, _document)) ?? Context.CreateSkip(EntitySet, _document);
100110
if (parameter != null)
101111
{
102112
operation.Parameters.Add(parameter);
103113
}
104114

105115
// $search
106-
parameter = Context.CreateSearch(TargetPath, _document) ?? Context.CreateSearch(EntitySet, _document);
116+
parameter = (TargetPath is null ? null : Context.CreateSearch(TargetPath, _document)) ?? Context.CreateSearch(EntitySet, _document);
107117
if (parameter != null)
108118
{
109119
operation.Parameters.Add(parameter);
110120
}
111121

112122
// $filter
113-
parameter = Context.CreateFilter(TargetPath, _document) ?? Context.CreateFilter(EntitySet, _document);
123+
parameter = (TargetPath is null ? null : Context.CreateFilter(TargetPath, _document)) ?? Context.CreateFilter(EntitySet, _document);
114124
if (parameter != null)
115125
{
116126
operation.Parameters.Add(parameter);
117127
}
118128

119129
// $count
120-
parameter = Context.CreateCount(TargetPath, _document) ?? Context.CreateCount(EntitySet, _document);
130+
parameter = (TargetPath is null ? null : Context.CreateCount(TargetPath, _document)) ?? Context.CreateCount(EntitySet, _document);
121131
if (parameter != null)
122132
{
123133
operation.Parameters.Add(parameter);
@@ -128,21 +138,21 @@ protected override void SetParameters(OpenApiOperation operation)
128138
// of just providing a comma-separated list of properties can be expressed via an array-valued
129139
// parameter with an enum constraint
130140
// $order
131-
parameter = Context.CreateOrderBy(TargetPath, EntitySet.EntityType) ?? Context.CreateOrderBy(EntitySet);
141+
parameter = (TargetPath is null ? null : Context.CreateOrderBy(TargetPath, EntitySet.EntityType)) ?? Context.CreateOrderBy(EntitySet);
132142
if (parameter != null)
133143
{
134144
operation.Parameters.Add(parameter);
135145
}
136146

137147
// $select
138-
parameter = Context.CreateSelect(TargetPath, EntitySet.EntityType) ?? Context.CreateSelect(EntitySet);
148+
parameter = (TargetPath is null ? null : Context.CreateSelect(TargetPath, EntitySet.EntityType)) ?? Context.CreateSelect(EntitySet);
139149
if (parameter != null)
140150
{
141151
operation.Parameters.Add(parameter);
142152
}
143153

144154
// $expand
145-
parameter = Context.CreateExpand(TargetPath, EntitySet.EntityType) ?? Context.CreateExpand(EntitySet);
155+
parameter = (TargetPath is null ? null : Context.CreateExpand(TargetPath, EntitySet.EntityType)) ?? Context.CreateExpand(EntitySet);
146156
if (parameter != null)
147157
{
148158
operation.Parameters.Add(parameter);
@@ -155,12 +165,13 @@ protected override void SetResponses(OpenApiOperation operation)
155165
operation.Responses = new OpenApiResponses
156166
{
157167
{
158-
Context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
168+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
159169
new OpenApiResponseReference($"{EntitySet.EntityType.FullName()}{Constants.CollectionSchemaSuffix}", _document)
160170
}
161171
};
162172

163-
operation.AddErrorResponses(Context.Settings, _document, false);
173+
if (Context is not null)
174+
operation.AddErrorResponses(Context.Settings, _document, false);
164175

165176
base.SetResponses(operation);
166177
}
@@ -172,7 +183,7 @@ protected override void SetSecurity(OpenApiOperation operation)
172183
return;
173184
}
174185

175-
operation.Security = Context.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
186+
operation.Security = Context?.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
176187
}
177188

178189
protected override void AppendCustomParameters(OpenApiOperation operation)

0 commit comments

Comments
 (0)