Skip to content

Commit 3737e0b

Browse files
committed
chore: additional NRT fixes
Signed-off-by: Vincent Biret <[email protected]>
1 parent db8b41a commit 3737e0b

26 files changed

+100
-95
lines changed

src/Microsoft.OpenApi.OData.Reader/Common/OpenApiOperationExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,25 @@ public static void AddErrorResponses(this OpenApiOperation operation, OpenApiCon
5757
}
5858
};
5959
}
60-
operation.Responses.Add(Constants.StatusCodeClass2XX, response ?? Constants.StatusCodeClass2XX.GetResponse(document));
60+
if ((response ?? Constants.StatusCodeClass2XX.GetResponse(document)) is {} x2xxResponse)
61+
operation.Responses.Add(Constants.StatusCodeClass2XX, x2xxResponse);
6162
}
62-
else
63+
else if (Constants.StatusCode204.GetResponse(document) is {} x204Response)
6364
{
64-
operation.Responses.Add(Constants.StatusCode204, Constants.StatusCode204.GetResponse(document));
65+
operation.Responses.Add(Constants.StatusCode204, x204Response);
6566
}
6667
}
6768

68-
if (settings.ErrorResponsesAsDefault)
69+
if (settings.ErrorResponsesAsDefault && Constants.StatusCodeDefault.GetResponse(document) is {} defaultResponse)
6970
{
70-
operation.Responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse(document));
71+
operation.Responses.Add(Constants.StatusCodeDefault, defaultResponse);
7172
}
7273
else
7374
{
74-
operation.Responses.Add(Constants.StatusCodeClass4XX, Constants.StatusCodeClass4XX.GetResponse(document));
75-
operation.Responses.Add(Constants.StatusCodeClass5XX, Constants.StatusCodeClass5XX.GetResponse(document));
75+
if (Constants.StatusCodeClass4XX.GetResponse(document) is {} x4xxResponse)
76+
operation.Responses.Add(Constants.StatusCodeClass4XX, x4xxResponse);
77+
if (Constants.StatusCodeClass5XX.GetResponse(document) is {} x5xxResponse)
78+
operation.Responses.Add(Constants.StatusCodeClass5XX, x5xxResponse);
7679
}
7780
}
7881
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static bool IsUrlEscapeFunction(this IEdmModel model, IEdmFunction funct
4848
Utils.CheckArgumentNull(model, nameof(model));
4949
Utils.CheckArgumentNull(function, nameof(function));
5050

51-
IEdmVocabularyAnnotation annotation = model.FindVocabularyAnnotations<IEdmVocabularyAnnotation>(function,
51+
var annotation = model.FindVocabularyAnnotations<IEdmVocabularyAnnotation>(function,
5252
CommunityVocabularyModel.UrlEscapeFunctionTerm).FirstOrDefault();
5353
if (annotation != null)
5454
{
@@ -58,12 +58,11 @@ private static bool IsUrlEscapeFunction(this IEdmModel model, IEdmFunction funct
5858
return true;
5959
}
6060

61-
IEdmBooleanConstantExpression tagConstant = annotation.Value as IEdmBooleanConstantExpression;
62-
if (tagConstant != null)
63-
{
64-
return tagConstant.Value;
65-
}
66-
}
61+
if (annotation.Value is IEdmBooleanConstantExpression tagConstant)
62+
{
63+
return tagConstant.Value;
64+
}
65+
}
6766

6867
return false;
6968
}
@@ -110,11 +109,11 @@ public static Dictionary<IEdmEntityType, IList<IEdmNavigationSource>> LoadAllNav
110109
/// </summary>
111110
/// <param name="entityType">The given entity type.</param>
112111
/// <returns>All base types or null.</returns>
113-
public static IEnumerable<IEdmEntityType> FindAllBaseTypes(this IEdmEntityType entityType)
112+
public static IEnumerable<IEdmEntityType>? FindAllBaseTypes(this IEdmEntityType entityType)
114113
{
115114
if (entityType == null)
116115
{
117-
yield return null;
116+
yield break;
118117
}
119118

120119
IEdmEntityType current = entityType.BaseEntityType();
@@ -130,11 +129,11 @@ public static IEnumerable<IEdmEntityType> FindAllBaseTypes(this IEdmEntityType e
130129
/// </summary>
131130
/// <param name="complexType">The given complex type.</param>
132131
/// <returns>All base types or null.</returns>
133-
public static IEnumerable<IEdmComplexType> FindAllBaseTypes(this IEdmComplexType complexType)
132+
public static IEnumerable<IEdmComplexType>? FindAllBaseTypes(this IEdmComplexType complexType)
134133
{
135134
if (complexType == null)
136135
{
137-
yield return null;
136+
yield break;
138137
}
139138

140139
IEdmComplexType current = complexType.BaseComplexType();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ODataTypeCastSegment(IEdmStructuredType structuredType, IEdmModel model)
2727

2828
private readonly IEdmModel _model;
2929
/// <inheritdoc />
30-
public override IEdmEntityType EntityType => null;
30+
public override IEdmEntityType? EntityType => null;
3131

3232
/// <inheritdoc />
3333
public override ODataSegmentKind Kind => ODataSegmentKind.TypeCast;
@@ -43,7 +43,7 @@ public ODataTypeCastSegment(IEdmStructuredType structuredType, IEdmModel model)
4343
/// <inheritdoc />
4444
public override IEnumerable<IEdmVocabularyAnnotatable> GetAnnotables()
4545
{
46-
return [StructuredType as IEdmVocabularyAnnotatable];
46+
return StructuredType is IEdmVocabularyAnnotatable annotable ? [annotable] : [];
4747
}
4848

4949
/// <inheritdoc />

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static class RecordExpressionExtensions
4141
/// <param name="record">The given record.</param>
4242
/// <param name="propertyName">The property name.</param>
4343
/// <returns>The property string value.</returns>
44-
public static string GetString(this IEdmRecordExpression record, string propertyName)
44+
public static string? GetString(this IEdmRecordExpression record, string propertyName)
4545
{
4646
Utils.CheckArgumentNull(record, nameof(record));
4747
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
@@ -127,7 +127,7 @@ property.Value is IEdmEnumMemberExpression value &&
127127
/// <param name="record">The record expression.</param>
128128
/// <param name="propertyName">The property name.</param>
129129
/// <returns>The collection or null.</returns>
130-
public static T GetRecord<T>(this IEdmRecordExpression record, string propertyName)
130+
public static T? GetRecord<T>(this IEdmRecordExpression record, string propertyName)
131131
where T : IRecord, new()
132132
{
133133
Utils.CheckArgumentNull(record, nameof(record));
@@ -150,7 +150,7 @@ public static T GetRecord<T>(this IEdmRecordExpression record, string propertyNa
150150
/// <param name="record">The record expression.</param>
151151
/// <param name="propertyName">The property name.</param>
152152
/// <returns>The property path or null.</returns>
153-
public static string GetPropertyPath(this IEdmRecordExpression record, string propertyName)
153+
public static string? GetPropertyPath(this IEdmRecordExpression record, string propertyName)
154154
{
155155
Utils.CheckArgumentNull(record, nameof(record));
156156
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
@@ -167,7 +167,7 @@ public static string GetPropertyPath(this IEdmRecordExpression record, string pr
167167
/// <param name="record">The record expression.</param>
168168
/// <param name="propertyName">The property name.</param>
169169
/// <returns>The collection of property path or null.</returns>
170-
public static IList<string> GetCollectionPropertyPath(this IEdmRecordExpression record, string propertyName)
170+
public static IList<string>? GetCollectionPropertyPath(this IEdmRecordExpression record, string propertyName)
171171
{
172172
Utils.CheckArgumentNull(record, nameof(record));
173173
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
@@ -196,7 +196,7 @@ public static IList<string> GetCollectionPropertyPath(this IEdmRecordExpression
196196
/// <param name="record">The record expression.</param>
197197
/// <param name="propertyName">The property name.</param>
198198
/// <returns>The collection of string or null.</returns>
199-
public static IList<string> GetCollection(this IEdmRecordExpression record, string propertyName)
199+
public static IList<string>? GetCollection(this IEdmRecordExpression record, string propertyName)
200200
{
201201
Utils.CheckArgumentNull(record, nameof(record));
202202
Utils.CheckArgumentNull(propertyName, nameof(propertyName));
@@ -221,7 +221,7 @@ public static IList<string> GetCollection(this IEdmRecordExpression record, stri
221221
/// <param name="record">The record expression.</param>
222222
/// <param name="propertyName">The property name.</param>
223223
/// <returns>The collection or null.</returns>
224-
public static IList<T> GetCollection<T>(this IEdmRecordExpression record, string propertyName)
224+
public static IList<T>? GetCollection<T>(this IEdmRecordExpression record, string propertyName)
225225
where T : IRecord, new()
226226
{
227227
Utils.CheckArgumentNull(record, nameof(record));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ public static IDictionary<string, IOpenApiLink> CreateLinks(this ODataContext co
117117
{
118118
foreach (var operationPath in operationPaths)
119119
{
120+
if (operationPath.LastSegment?.Identifier is null) continue;
120121
OpenApiLink link = new()
121122
{
122123
OperationId = string.Join(".", operationPath.Segments.Select(x =>
123124
{
124-
return x.Kind.Equals(ODataSegmentKind.Key) ? x.EntityType.Name : x.Identifier;
125+
return x.Kind.Equals(ODataSegmentKind.Key) && x.EntityType is not null ? x.EntityType.Name : x.Identifier;
125126
}))
126127
};
127128

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// ------------------------------------------------------------
55

66
using System.Collections.Generic;
7-
using System.Linq;
87
using System.Net.Http;
98
using Microsoft.OData.Edm;
109
using Microsoft.OpenApi.Models;
@@ -46,8 +45,7 @@ public static void AddPathItemsToDocument(this ODataContext context, OpenApiDocu
4645
continue;
4746
}
4847

49-
OpenApiPathItem pathItem = handler.CreatePathItem(context, path);
50-
if (!pathItem.Operations.Any())
48+
if (handler.CreatePathItem(context, path) is not {Operations.Count: > 0} pathItem)
5149
{
5250
continue;
5351
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal static class OpenApiRequestBodyGenerator
2626
/// <param name="actionImport">The Edm action import.</param>
2727
/// <param name="document">The OpenApi document to lookup references.</param>
2828
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
29-
public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IEdmActionImport actionImport, OpenApiDocument document)
29+
public static OpenApiRequestBody? CreateRequestBody(this ODataContext context, IEdmActionImport actionImport, OpenApiDocument document)
3030
{
3131
Utils.CheckArgumentNull(context, nameof(context));
3232
Utils.CheckArgumentNull(actionImport, nameof(actionImport));
@@ -42,7 +42,7 @@ public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IE
4242
/// <param name="action">The Edm action.</param>
4343
/// <param name="document">The OpenApi document to lookup references.</param>
4444
/// <returns>The created <see cref="OpenApiRequestBody"/> or null.</returns>
45-
public static OpenApiRequestBody CreateRequestBody(this ODataContext context, IEdmAction action, OpenApiDocument document)
45+
public static OpenApiRequestBody? CreateRequestBody(this ODataContext context, IEdmAction action, OpenApiDocument document)
4646
{
4747
Utils.CheckArgumentNull(context, nameof(context));
4848
Utils.CheckArgumentNull(action, nameof(action));

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System;
77
using System.Collections.Generic;
8-
using System.Diagnostics.CodeAnalysis;
98
using System.Linq;
109
using Microsoft.OData.Edm;
1110
using Microsoft.OpenApi.Models;
@@ -28,7 +27,7 @@ internal static class OpenApiResponseGenerator
2827
/// <param name="statusCode">The status code.</param>
2928
/// <param name="document">The OpenApi document to lookup references.</param>
3029
/// <returns>The created <see cref="IOpenApiResponse"/>.</returns>
31-
public static IOpenApiResponse GetResponse(this string statusCode, OpenApiDocument document)
30+
public static IOpenApiResponse? GetResponse(this string statusCode, OpenApiDocument document)
3231
{
3332
return statusCode switch {
3433
Constants.StatusCodeDefault => new OpenApiResponseReference(Constants.Error, document),
@@ -82,8 +81,7 @@ public static void AddResponsesToDocument(this ODataContext context, OpenApiDocu
8281
foreach (IEdmOperation operation in context.Model.SchemaElements.OfType<IEdmOperation>()
8382
.Where(op => context.Model.OperationTargetsMultiplePaths(op)))
8483
{
85-
OpenApiResponse response = context.CreateOperationResponse(operation, document);
86-
if (response != null)
84+
if (context.CreateOperationResponse(operation, document) is {} response)
8785
responses[$"{operation.Name}Response"] = response;
8886
}
8987

@@ -134,26 +132,27 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
134132
new OpenApiResponseReference($"{operation.Name}Response", document)
135133
);
136134
}
137-
else
135+
else if (context.CreateOperationResponse(operation, document) is {} successResponse)
138136
{
139-
OpenApiResponse response = context.CreateOperationResponse(operation, document);
140-
responses.Add(context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200, response);
137+
responses.Add(context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200, successResponse);
141138
}
142139

143-
if (context.Settings.ErrorResponsesAsDefault)
140+
if (context.Settings.ErrorResponsesAsDefault && Constants.StatusCodeDefault.GetResponse(document) is {} defaultResponse)
144141
{
145-
responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse(document));
142+
responses.Add(Constants.StatusCodeDefault, defaultResponse);
146143
}
147144
else
148145
{
149-
responses.Add(Constants.StatusCodeClass4XX, Constants.StatusCodeClass4XX.GetResponse(document));
150-
responses.Add(Constants.StatusCodeClass5XX, Constants.StatusCodeClass5XX.GetResponse(document));
146+
if (Constants.StatusCodeClass4XX.GetResponse(document) is {} x4Response)
147+
responses.Add(Constants.StatusCodeClass4XX, x4Response);
148+
if (Constants.StatusCodeClass5XX.GetResponse(document) is {} x5Response)
149+
responses.Add(Constants.StatusCodeClass5XX, x5Response);
151150
}
152151

153152
return responses;
154153
}
155154

156-
public static OpenApiResponse CreateOperationResponse(this ODataContext context, IEdmOperation operation, OpenApiDocument document)
155+
public static OpenApiResponse? CreateOperationResponse(this ODataContext context, IEdmOperation operation, OpenApiDocument document)
157156
{
158157
if (operation.ReturnType == null)
159158
return null;
@@ -232,7 +231,7 @@ public static OpenApiResponse CreateOperationResponse(this ODataContext context,
232231
schema = context.CreateEdmTypeSchema(operation.ReturnType, document);
233232
}
234233

235-
string mediaType = Constants.ApplicationJsonMediaType;
234+
string? mediaType = Constants.ApplicationJsonMediaType;
236235
if (operation.ReturnType.AsPrimitive()?.PrimitiveKind() == EdmPrimitiveTypeKind.Stream)
237236
{
238237
mediaType = context.Model.GetString(operation, CoreConstants.MediaType);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ComplexPropertyPostOperationHandler(OpenApiDocument document):base(docume
3131
protected override void Initialize(ODataContext context, ODataPath path)
3232
{
3333
base.Initialize(context, path);
34-
if (!ComplexPropertySegment.Property.Type.IsCollection())
34+
if (ComplexPropertySegment is null || !ComplexPropertySegment.Property.Type.IsCollection())
3535
{
3636
throw new InvalidOperationException("OData conventions do not support POSTing to a complex property that is not a collection.");
3737
}
@@ -60,7 +60,7 @@ protected override void SetBasicInfo(OpenApiOperation operation)
6060
}
6161

6262
// Summary and Description
63-
string placeHolder = $"Sets a new value for the collection of {ComplexPropertySegment.ComplexType.Name}.";
63+
string placeHolder = $"Sets a new value for the collection of {ComplexPropertySegment?.ComplexType?.Name}.";
6464
operation.Summary = _insertRestrictions?.Description ?? placeHolder;
6565
operation.Description = _insertRestrictions?.LongDescription;
6666

@@ -140,8 +140,9 @@ protected override void AppendCustomParameters(OpenApiOperation operation)
140140
}
141141
}
142142

143-
private OpenApiSchema GetOpenApiSchema()
143+
private OpenApiSchema? GetOpenApiSchema()
144144
{
145+
if (ComplexPropertySegment is null) return null;
145146
return new()
146147
{
147148
Type = JsonSchemaType.Array,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ protected override void Initialize(ODataContext context, ODataPath path)
3737
if (!string.IsNullOrEmpty(TargetPath))
3838
_updateRestrictions = Context?.Model.GetRecord<UpdateRestrictionsType>(TargetPath, CapabilitiesConstants.UpdateRestrictions);
3939

40-
var complexPropertyUpdateRestrictions = Context?.Model.GetRecord<UpdateRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.UpdateRestrictions);
41-
_updateRestrictions?.MergePropertiesIfNull(complexPropertyUpdateRestrictions);
42-
_updateRestrictions ??= complexPropertyUpdateRestrictions;
40+
if (ComplexPropertySegment is not null)
41+
{
42+
var complexPropertyUpdateRestrictions = Context?.Model.GetRecord<UpdateRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.UpdateRestrictions);
43+
_updateRestrictions?.MergePropertiesIfNull(complexPropertyUpdateRestrictions);
44+
_updateRestrictions ??= complexPropertyUpdateRestrictions;
45+
}
4346
}
4447

4548
/// <inheritdoc/>
4649
protected override void SetBasicInfo(OpenApiOperation operation)
4750
{
4851
// Summary and Description
49-
string placeHolder = $"Update property {ComplexPropertySegment.Property.Name} value.";
52+
string placeHolder = $"Update property {ComplexPropertySegment?.Property.Name} value.";
5053
operation.Summary = _updateRestrictions?.Description ?? placeHolder;
5154
operation.Description = _updateRestrictions?.LongDescription;
5255

@@ -114,8 +117,9 @@ protected override void AppendCustomParameters(OpenApiOperation operation)
114117
}
115118
}
116119

117-
private IOpenApiSchema GetOpenApiSchema()
120+
private IOpenApiSchema? GetOpenApiSchema()
118121
{
122+
if (ComplexPropertySegment is null) return null;
119123
var schema = new OpenApiSchemaReference(ComplexPropertySegment.ComplexType.FullName(), _document);
120124

121125
if (ComplexPropertySegment.Property.Type.IsCollection())

0 commit comments

Comments
 (0)