Skip to content

Commit 0868da2

Browse files
committed
chore: additional NRT fixes batch
Signed-off-by: Vincent Biret <[email protected]>
1 parent 255a84b commit 0868da2

7 files changed

+60
-60
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal static class OpenApiLinkGenerator
3131
/// <returns>The created dictionary of <see cref="OpenApiLink"/> object.</returns>
3232
public static IDictionary<string, IOpenApiLink> CreateLinks(this ODataContext context,
3333
IEdmEntityType entityType, string entityName, string entityKind, ODataPath path,
34-
IList<IOpenApiParameter> parameters = null, string navPropOperationId = null)
34+
IList<IOpenApiParameter>? parameters = null, string? navPropOperationId = null)
3535
{
3636
Utils.CheckArgumentNull(context, nameof(context));
3737
Utils.CheckArgumentNull(entityType, nameof(entityType));

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public EntityDeleteOperationHandler(OpenApiDocument document) : base(document)
3232
/// <inheritdoc/>
3333
public override HttpMethod OperationType => HttpMethod.Delete;
3434

35-
private DeleteRestrictionsType _deleteRestrictions;
35+
private DeleteRestrictionsType? _deleteRestrictions;
3636

3737
protected override void Initialize(ODataContext context, ODataPath path)
3838
{
3939
base.Initialize(context, path);
4040

41-
_deleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
41+
if (Context is null) return;
42+
if (!string.IsNullOrEmpty(TargetPath))
43+
_deleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
4244
var entityDeleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(EntitySet, CapabilitiesConstants.DeleteRestrictions);
4345
_deleteRestrictions?.MergePropertiesIfNull(entityDeleteRestrictions);
4446
_deleteRestrictions ??= entityDeleteRestrictions;
@@ -48,25 +50,24 @@ protected override void Initialize(ODataContext context, ODataPath path)
4850
protected override void SetBasicInfo(OpenApiOperation operation)
4951
{
5052
IEdmEntityType entityType = EntitySet.EntityType;
51-
ODataKeySegment keySegment = Path.LastSegment as ODataKeySegment;
5253

5354
// Description
5455
string placeHolder = $"Delete entity from {EntitySet.Name}";
55-
if (keySegment.IsAlternateKey)
56+
if (Path is {LastSegment: ODataKeySegment {IsAlternateKey: true} keySegment})
5657
{
5758
placeHolder = $"{placeHolder} by {keySegment.Identifier}";
5859
}
5960
operation.Summary = _deleteRestrictions?.Description ?? placeHolder;
6061
operation.Description = _deleteRestrictions?.LongDescription;
6162

6263
// OperationId
63-
if (Context.Settings.EnableOperationId)
64+
if (Context is { Settings.EnableOperationId: true})
6465
{
6566
string typeName = entityType.Name;
6667
string operationName =$"Delete{Utils.UpperFirstChar(typeName)}";
67-
if (keySegment.IsAlternateKey)
68+
if (Path is {LastSegment: ODataKeySegment {IsAlternateKey: true} keySegment2})
6869
{
69-
string alternateKeyName = string.Join("", keySegment.Identifier.Split(',').Select(static x => Utils.UpperFirstChar(x)));
70+
string alternateKeyName = string.Join("", keySegment2.Identifier.Split(',').Select(static x => Utils.UpperFirstChar(x)));
7071
operationName = $"{operationName}By{alternateKeyName}";
7172
}
7273
operation.OperationId = $"{EntitySet.Name}.{typeName}.{operationName}";
@@ -78,6 +79,7 @@ protected override void SetParameters(OpenApiOperation operation)
7879
{
7980
base.SetParameters(operation);
8081

82+
operation.Parameters ??= [];
8183
operation.Parameters.Add(new OpenApiParameter
8284
{
8385
Name = "If-Match",
@@ -94,7 +96,7 @@ protected override void SetParameters(OpenApiOperation operation)
9496
protected override void SetResponses(OpenApiOperation operation)
9597
{
9698
// Response for Delete methods should be 204 No Content
97-
OpenApiConvertSettings settings = Context.Settings.Clone();
99+
OpenApiConvertSettings settings = Context?.Settings.Clone() ?? new();
98100
settings.UseSuccessStatusCodeRange = false;
99101

100102
operation.AddErrorResponses(settings, _document, true);
@@ -108,7 +110,7 @@ protected override void SetSecurity(OpenApiOperation operation)
108110
return;
109111
}
110112

111-
operation.Security = Context.CreateSecurityRequirements(_deleteRestrictions.Permissions, _document).ToList();
113+
operation.Security = Context?.CreateSecurityRequirements(_deleteRestrictions.Permissions, _document).ToList() ?? [];
112114
}
113115

114116
protected override void AppendCustomParameters(OpenApiOperation operation)

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@ public MediaEntityDeleteOperationHandler(OpenApiDocument document) : base(docume
2222
/// <inheritdoc/>
2323
public override HttpMethod OperationType => HttpMethod.Delete;
2424

25-
private DeleteRestrictionsType _deleteRestrictions;
25+
private DeleteRestrictionsType? _deleteRestrictions;
2626

2727
protected override void Initialize(ODataContext context, ODataPath path)
2828
{
2929
base.Initialize(context, path);
3030

3131
if (Property != null)
3232
{
33-
_deleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
33+
if (!string.IsNullOrEmpty(TargetPath))
34+
_deleteRestrictions = Context?.Model.GetRecord<DeleteRestrictionsType>(TargetPath, CapabilitiesConstants.DeleteRestrictions);
3435
if (Property is IEdmNavigationProperty)
3536
{
36-
var navigationDeleteRestrictions = Context.Model.GetRecord<NavigationRestrictionsType>(Property, CapabilitiesConstants.NavigationRestrictions)?
37+
var navigationDeleteRestrictions = Context?.Model.GetRecord<NavigationRestrictionsType>(Property, CapabilitiesConstants.NavigationRestrictions)?
3738
.RestrictedProperties?.FirstOrDefault()?.DeleteRestrictions;
3839
_deleteRestrictions?.MergePropertiesIfNull(navigationDeleteRestrictions);
3940
_deleteRestrictions ??= navigationDeleteRestrictions;
4041
}
4142
else
4243
{
43-
var propertyDeleteRestrictions = Context.Model.GetRecord<DeleteRestrictionsType>(Property, CapabilitiesConstants.DeleteRestrictions);
44+
var propertyDeleteRestrictions = Context?.Model.GetRecord<DeleteRestrictionsType>(Property, CapabilitiesConstants.DeleteRestrictions);
4445
_deleteRestrictions?.MergePropertiesIfNull(propertyDeleteRestrictions);
4546
_deleteRestrictions ??= propertyDeleteRestrictions;
4647
}
@@ -51,19 +52,19 @@ protected override void Initialize(ODataContext context, ODataPath path)
5152
protected override void SetBasicInfo(OpenApiOperation operation)
5253
{
5354
// Summary
54-
string placeholderValue = LastSegmentIsStreamPropertySegment ? Path.LastSegment.Identifier : "media content";
55+
string placeholderValue = LastSegmentIsStreamPropertySegment && Path is not null ? Path.LastSegment.Identifier : "media content";
5556
operation.Summary = _deleteRestrictions?.Description;
5657
operation.Summary ??= IsNavigationPropertyPath
5758
? $"Delete {placeholderValue} for the navigation property {NavigationProperty.Name} in {NavigationSourceSegment.NavigationSource.Name}"
5859
: $"Delete {placeholderValue} for {NavigationSourceSegment.EntityType.Name} in {NavigationSourceSegment.Identifier}";
5960

6061
// Description
61-
operation.Description = _deleteRestrictions?.LongDescription ?? Context.Model.GetDescriptionAnnotation(Property);
62+
operation.Description = _deleteRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(Property);
6263

6364
// OperationId
64-
if (Context.Settings.EnableOperationId)
65+
if (Context is {Settings.EnableOperationId: true})
6566
{
66-
string identifier = LastSegmentIsStreamPropertySegment ? Path.LastSegment.Identifier : "Content";
67+
string identifier = LastSegmentIsStreamPropertySegment && Path is not null ? Path.LastSegment.Identifier : "Content";
6768
operation.OperationId = GetOperationId("Delete", identifier);
6869
}
6970

@@ -75,6 +76,7 @@ protected override void SetParameters(OpenApiOperation operation)
7576
{
7677
base.SetParameters(operation);
7778

79+
operation.Parameters ??= [];
7880
operation.Parameters.Add(new OpenApiParameter
7981
{
8082
Name = "If-Match",
@@ -91,7 +93,7 @@ protected override void SetParameters(OpenApiOperation operation)
9193
protected override void SetResponses(OpenApiOperation operation)
9294
{
9395
// Response for Delete methods should be 204 No Content
94-
OpenApiConvertSettings settings = Context.Settings.Clone();
96+
OpenApiConvertSettings settings = Context?.Settings.Clone() ?? new();
9597
settings.UseSuccessStatusCodeRange = false;
9698

9799
operation.AddErrorResponses(settings, _document, true);
@@ -105,7 +107,7 @@ protected override void SetSecurity(OpenApiOperation operation)
105107
return;
106108
}
107109

108-
operation.Security = Context.CreateSecurityRequirements(_deleteRestrictions.Permissions, _document).ToList();
110+
operation.Security = Context?.CreateSecurityRequirements(_deleteRestrictions.Permissions, _document).ToList() ?? [];
109111
}
110112

111113
/// <inheritdoc/>

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public NavigationPropertyDeleteOperationHandler(OpenApiDocument document):base(d
3131
/// <inheritdoc/>
3232
public override HttpMethod OperationType => HttpMethod.Delete;
3333

34-
private DeleteRestrictionsType _deleteRestriction;
34+
private DeleteRestrictionsType? _deleteRestriction;
3535

3636
/// <inheritdoc/>
3737
protected override void Initialize(ODataContext context, ODataPath path)
@@ -44,12 +44,12 @@ protected override void Initialize(ODataContext context, ODataPath path)
4444
protected override void SetBasicInfo(OpenApiOperation operation)
4545
{
4646
// Summary and Description
47-
string placeHolder = "Delete navigation property " + NavigationProperty.Name + " for " + NavigationSource.Name;
47+
string placeHolder = "Delete navigation property " + NavigationProperty?.Name + " for " + NavigationSource?.Name;
4848
operation.Summary = _deleteRestriction?.Description ?? placeHolder;
4949
operation.Description = _deleteRestriction?.LongDescription;
5050

5151
// OperationId
52-
if (Context.Settings.EnableOperationId)
52+
if (Context is { Settings.EnableOperationId: true})
5353
{
5454
string prefix = "Delete";
5555
operation.OperationId = GetOperationId(prefix);
@@ -63,6 +63,7 @@ protected override void SetParameters(OpenApiOperation operation)
6363
{
6464
base.SetParameters(operation);
6565

66+
operation.Parameters ??= [];
6667
operation.Parameters.Add(new OpenApiParameter
6768
{
6869
Name = "If-Match",
@@ -83,14 +84,14 @@ protected override void SetSecurity(OpenApiOperation operation)
8384
return;
8485
}
8586

86-
operation.Security = Context.CreateSecurityRequirements(_deleteRestriction.Permissions, _document).ToList();
87+
operation.Security = Context?.CreateSecurityRequirements(_deleteRestriction.Permissions, _document).ToList() ?? [];
8788
}
8889

8990
/// <inheritdoc/>
9091
protected override void SetResponses(OpenApiOperation operation)
9192
{
9293
// Response for Delete methods should be 204 No Content
93-
OpenApiConvertSettings settings = Context.Settings.Clone();
94+
OpenApiConvertSettings settings = Context?.Settings.Clone() ?? new();
9495
settings.UseSuccessStatusCodeRange = false;
9596

9697
operation.AddErrorResponses(settings, _document, true);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ protected override void Initialize(ODataContext context, ODataPath path)
5252
protected override void SetBasicInfo(OpenApiOperation operation)
5353
{
5454
// Summary and Description
55-
string placeHolder = "Get " + NavigationProperty.Name + " from " + NavigationSource.Name;
55+
string placeHolder = "Get " + NavigationProperty?.Name + " from " + NavigationSource?.Name;
5656
operation.Summary = (LastSegmentIsKeySegment ? _readRestriction?.ReadByKeyRestrictions?.Description : _readRestriction?.Description) ?? placeHolder;
5757
operation.Description = (LastSegmentIsKeySegment ? _readRestriction?.ReadByKeyRestrictions?.LongDescription : _readRestriction?.LongDescription)
58-
?? Context.Model.GetDescriptionAnnotation(NavigationProperty);
58+
?? Context?.Model.GetDescriptionAnnotation(NavigationProperty);
5959

6060
// OperationId
6161
if (Context is { Settings.EnableOperationId: true })
@@ -93,7 +93,7 @@ protected override void SetExtensions(OpenApiOperation operation)
9393
protected override void SetResponses(OpenApiOperation operation)
9494
{
9595
IDictionary<string, IOpenApiLink>? links = null;
96-
if (Context is { Settings.ShowLinks: true })
96+
if (Context is { Settings.ShowLinks: true } && NavigationProperty is not null && Path is not null)
9797
{
9898
string operationId = GetOperationId();
9999

@@ -107,7 +107,7 @@ protected override void SetResponses(OpenApiOperation operation)
107107
operation.Responses = new OpenApiResponses
108108
{
109109
{
110-
Context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
110+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
111111
new OpenApiResponseReference($"{NavigationProperty.ToEntityType().FullName()}{Constants.CollectionSchemaSuffix}", _document)
112112
}
113113
};
@@ -117,7 +117,7 @@ protected override void SetResponses(OpenApiOperation operation)
117117
IOpenApiSchema? schema = null;
118118
var entityType = NavigationProperty.ToEntityType();
119119

120-
if (Context.Settings.EnableDerivedTypesReferencesForResponses)
120+
if (Context is { Settings.EnableDerivedTypesReferencesForResponses: true })
121121
{
122122
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(entityType, Context.Model, _document);
123123
}
@@ -127,7 +127,7 @@ protected override void SetResponses(OpenApiOperation operation)
127127
operation.Responses = new OpenApiResponses
128128
{
129129
{
130-
Context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
130+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
131131
new OpenApiResponse
132132
{
133133
Description = "Retrieved navigation property",
@@ -147,7 +147,8 @@ protected override void SetResponses(OpenApiOperation operation)
147147
};
148148
}
149149

150-
operation.AddErrorResponses(Context.Settings, _document, false);
150+
if (Context is not null)
151+
operation.AddErrorResponses(Context.Settings, _document, false);
151152

152153
base.SetResponses(operation);
153154
}

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public NavigationPropertyPostOperationHandler(OpenApiDocument document):base(doc
3232
/// <inheritdoc/>
3333
public override HttpMethod OperationType => HttpMethod.Post;
3434

35-
private InsertRestrictionsType _insertRestriction;
35+
private InsertRestrictionsType? _insertRestriction;
3636

3737
/// <inheritdoc/>
3838
protected override void Initialize(ODataContext context, ODataPath path)
@@ -45,12 +45,12 @@ protected override void Initialize(ODataContext context, ODataPath path)
4545
protected override void SetBasicInfo(OpenApiOperation operation)
4646
{
4747
// Summary and Description
48-
string placeHolder = "Create new navigation property to " + NavigationProperty.Name + " for " + NavigationSource.Name;
48+
string placeHolder = "Create new navigation property to " + NavigationProperty?.Name + " for " + NavigationSource?.Name;
4949
operation.Summary = _insertRestriction?.Description ?? placeHolder;
5050
operation.Description = _insertRestriction?.LongDescription;
5151

5252
// OperationId
53-
if (Context.Settings.EnableOperationId)
53+
if (Context is {Settings.EnableOperationId: true})
5454
{
5555
string prefix = "Create";
5656
operation.OperationId = GetOperationId(prefix);
@@ -62,12 +62,9 @@ protected override void SetBasicInfo(OpenApiOperation operation)
6262
/// <inheritdoc/>
6363
protected override void SetRequestBody(OpenApiOperation operation)
6464
{
65-
OpenApiSchema schema = null;
66-
67-
if (Context.Settings.EnableDerivedTypesReferencesForRequestBody)
68-
{
69-
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(NavigationProperty.ToEntityType(), Context.Model, _document);
70-
}
65+
var schema = Context is {Settings.EnableDerivedTypesReferencesForRequestBody: true} ?
66+
EdmModelHelper.GetDerivedTypesReferenceSchema(NavigationProperty.ToEntityType(), Context.Model, _document) :
67+
null;
7168

7269
operation.RequestBody = new OpenApiRequestBody
7370
{
@@ -82,25 +79,23 @@ protected override void SetRequestBody(OpenApiOperation operation)
8279
/// <inheritdoc/>
8380
protected override void SetResponses(OpenApiOperation operation)
8481
{
85-
OpenApiSchema schema = null;
86-
87-
if (Context.Settings.EnableDerivedTypesReferencesForResponses)
88-
{
89-
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(NavigationProperty.ToEntityType(), Context.Model, _document);
90-
}
82+
var schema = Context is {Settings.EnableDerivedTypesReferencesForResponses: true} ?
83+
EdmModelHelper.GetDerivedTypesReferenceSchema(NavigationProperty.ToEntityType(), Context.Model, _document) :
84+
null;
9185

9286
operation.Responses = new OpenApiResponses
9387
{
9488
{
95-
Context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode201,
89+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode201,
9690
new OpenApiResponse
9791
{
9892
Description = "Created navigation property.",
9993
Content = GetContent(schema, _insertRestriction?.ResponseContentTypes)
10094
}
10195
}
10296
};
103-
operation.AddErrorResponses(Context.Settings, _document, false);
97+
if (Context is not null)
98+
operation.AddErrorResponses(Context.Settings, _document, false);
10499

105100
base.SetResponses(operation);
106101
}
@@ -112,7 +107,7 @@ protected override void SetSecurity(OpenApiOperation operation)
112107
return;
113108
}
114109

115-
operation.Security = Context.CreateSecurityRequirements(_insertRestriction.Permissions, _document).ToList();
110+
operation.Security = Context?.CreateSecurityRequirements(_insertRestriction.Permissions ?? [], _document).ToList() ?? [];
116111
}
117112

118113
protected override void AppendCustomParameters(OpenApiOperation operation)

0 commit comments

Comments
 (0)