Skip to content

Commit 05faaeb

Browse files
committed
chore: additional NRT fixes
Signed-off-by: Vincent Biret <[email protected]>
1 parent 46722a9 commit 05faaeb

File tree

9 files changed

+134
-106
lines changed

9 files changed

+134
-106
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected ComplexPropertyBaseOperationHandler(OpenApiDocument document) : base(d
2323
{
2424

2525
}
26-
protected ODataComplexPropertySegment ComplexPropertySegment;
26+
protected ODataComplexPropertySegment? ComplexPropertySegment;
2727

2828
/// <inheritdoc/>
2929
protected override void Initialize(ODataContext context, ODataPath path)
@@ -35,15 +35,18 @@ protected override void Initialize(ODataContext context, ODataPath path)
3535
/// <inheritdoc/>
3636
protected override void SetTags(OpenApiOperation operation)
3737
{
38-
string tagName = EdmModelHelper.GenerateComplexPropertyPathTagName(Path, Context);
39-
operation.Tags ??= new HashSet<OpenApiTagReference>();
40-
if (!string.IsNullOrEmpty(tagName))
38+
if (Context is not null && Path is not null)
4139
{
42-
Context.AddExtensionToTag(tagName, Constants.xMsTocType, new OpenApiAny("page"), () => new OpenApiTag()
43-
{
44-
Name = tagName
45-
});
46-
operation.Tags.Add(new OpenApiTagReference(tagName, _document));
40+
string tagName = EdmModelHelper.GenerateComplexPropertyPathTagName(Path, Context);
41+
operation.Tags ??= new HashSet<OpenApiTagReference>();
42+
if (!string.IsNullOrEmpty(tagName))
43+
{
44+
Context.AddExtensionToTag(tagName, Constants.xMsTocType, new OpenApiAny("page"), () => new OpenApiTag()
45+
{
46+
Name = tagName
47+
});
48+
operation.Tags.Add(new OpenApiTagReference(tagName, _document));
49+
}
4750
}
4851

4952
base.SetTags(operation);
@@ -52,10 +55,10 @@ protected override void SetTags(OpenApiOperation operation)
5255
/// <inheritdoc/>
5356
protected override void SetExternalDocs(OpenApiOperation operation)
5457
{
55-
if (Context.Settings.ShowExternalDocs)
58+
if (Context is {Settings.ShowExternalDocs: true} && CustomLinkRel is not null)
5659
{
57-
var externalDocs = Context.Model.GetLinkRecord(TargetPath, CustomLinkRel) ??
58-
Context.Model.GetLinkRecord(ComplexPropertySegment.Property, CustomLinkRel);
60+
var externalDocs = (string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetLinkRecord(TargetPath, CustomLinkRel)) ??
61+
(ComplexPropertySegment is null ? null : Context.Model.GetLinkRecord(ComplexPropertySegment.Property, CustomLinkRel));
5962

6063
if (externalDocs != null)
6164
{

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

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

6+
using System.Collections.Generic;
67
using System.Linq;
78
using System.Net.Http;
89
using System.Text.Json.Nodes;
910
using Microsoft.OData.Edm;
1011
using Microsoft.OpenApi.Any;
12+
using Microsoft.OpenApi.Interfaces;
1113
using Microsoft.OpenApi.Models;
1214
using Microsoft.OpenApi.Models.Interfaces;
1315
using Microsoft.OpenApi.Models.References;
@@ -31,14 +33,14 @@ public ComplexPropertyGetOperationHandler(OpenApiDocument document):base(documen
3133
/// <inheritdoc />
3234
public override HttpMethod OperationType => HttpMethod.Get;
3335

34-
private ReadRestrictionsType _readRestrictions;
36+
private ReadRestrictionsType? _readRestrictions;
3537

3638
protected override void Initialize(ODataContext context, ODataPath path)
3739
{
3840
base.Initialize(context, path);
3941

40-
_readRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
41-
var complexPropertyReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.ReadRestrictions);
42+
_readRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
43+
var complexPropertyReadRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.ReadRestrictions);
4244
_readRestrictions?.MergePropertiesIfNull(complexPropertyReadRestrictions);
4345
_readRestrictions ??= complexPropertyReadRestrictions;
4446
}
@@ -47,7 +49,7 @@ protected override void Initialize(ODataContext context, ODataPath path)
4749
protected override void SetBasicInfo(OpenApiOperation operation)
4850
{
4951
// OperationId
50-
if (Context.Settings.EnableOperationId)
52+
if (Context is {Settings.EnableOperationId: true} && Path is not null)
5153
{
5254
string prefix = ComplexPropertySegment.Property.Type.IsCollection() ? "List" : "Get";
5355
operation.OperationId = EdmModelHelper.GenerateComplexPropertyPathOperationId(Path, Context, prefix);
@@ -56,7 +58,7 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5658
// Summary and Description
5759
string placeHolder = $"Get {ComplexPropertySegment.Property.Name} property value";
5860
operation.Summary = _readRestrictions?.Description ?? placeHolder;
59-
operation.Description = _readRestrictions?.LongDescription ?? Context.Model.GetDescriptionAnnotation(ComplexPropertySegment.Property);
61+
operation.Description = _readRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(ComplexPropertySegment.Property);
6062

6163
base.SetBasicInfo(operation);
6264
}
@@ -65,43 +67,46 @@ protected override void SetParameters(OpenApiOperation operation)
6567
{
6668
base.SetParameters(operation);
6769

68-
IOpenApiParameter parameter;
70+
if (Context is null) return;
71+
72+
IOpenApiParameter? parameter;
73+
operation.Parameters ??= [];
6974
if(ComplexPropertySegment.Property.Type.IsCollection())
7075
{
7176
// The parameters array contains Parameter Objects for all system query options allowed for this collection,
7277
// and it does not list system query options not allowed for this collection, see terms
7378
// Capabilities.TopSupported, Capabilities.SkipSupported, Capabilities.SearchRestrictions,
7479
// Capabilities.FilterRestrictions, and Capabilities.CountRestrictions
7580
// $top
76-
parameter = Context.CreateTop(TargetPath, _document) ?? Context.CreateTop(ComplexPropertySegment.Property, _document);
81+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateTop(TargetPath, _document)) ?? Context.CreateTop(ComplexPropertySegment.Property, _document);
7782
if (parameter != null)
7883
{
7984
operation.Parameters.Add(parameter);
8085
}
8186

8287
// $skip
83-
parameter = Context.CreateSkip(TargetPath, _document) ?? Context.CreateSkip(ComplexPropertySegment.Property, _document);
88+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateSkip(TargetPath, _document)) ?? Context.CreateSkip(ComplexPropertySegment.Property, _document);
8489
if (parameter != null)
8590
{
8691
operation.Parameters.Add(parameter);
8792
}
8893

8994
// $search
90-
parameter = Context.CreateSearch(TargetPath, _document) ?? Context.CreateSearch(ComplexPropertySegment.Property, _document);
95+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateSearch(TargetPath, _document)) ?? Context.CreateSearch(ComplexPropertySegment.Property, _document);
9196
if (parameter != null)
9297
{
9398
operation.Parameters.Add(parameter);
9499
}
95100

96101
// $filter
97-
parameter = Context.CreateFilter(TargetPath, _document) ?? Context.CreateFilter(ComplexPropertySegment.Property, _document);
102+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateFilter(TargetPath, _document)) ?? Context.CreateFilter(ComplexPropertySegment.Property, _document);
98103
if (parameter != null)
99104
{
100105
operation.Parameters.Add(parameter);
101106
}
102107

103108
// $count
104-
parameter = Context.CreateCount(TargetPath, _document) ?? Context.CreateCount(ComplexPropertySegment.Property, _document);
109+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateCount(TargetPath, _document)) ?? Context.CreateCount(ComplexPropertySegment.Property, _document);
105110
if (parameter != null)
106111
{
107112
operation.Parameters.Add(parameter);
@@ -112,7 +117,7 @@ protected override void SetParameters(OpenApiOperation operation)
112117
// of just providing a comma-separated list of properties can be expressed via an array-valued
113118
// parameter with an enum constraint
114119
// $order
115-
parameter = Context.CreateOrderBy(TargetPath, ComplexPropertySegment.ComplexType)
120+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateOrderBy(TargetPath, ComplexPropertySegment.ComplexType))
116121
?? Context.CreateOrderBy(ComplexPropertySegment.Property, ComplexPropertySegment.ComplexType);
117122
if (parameter != null)
118123
{
@@ -121,15 +126,15 @@ protected override void SetParameters(OpenApiOperation operation)
121126
}
122127

123128
// $select
124-
parameter = Context.CreateSelect(TargetPath, ComplexPropertySegment.ComplexType)
129+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateSelect(TargetPath, ComplexPropertySegment.ComplexType))
125130
?? Context.CreateSelect(ComplexPropertySegment.Property, ComplexPropertySegment.ComplexType);
126131
if (parameter != null)
127132
{
128133
operation.Parameters.Add(parameter);
129134
}
130135

131136
// $expand
132-
parameter = Context.CreateExpand(TargetPath, ComplexPropertySegment.ComplexType)
137+
parameter = (string.IsNullOrEmpty(TargetPath) ? null : Context.CreateExpand(TargetPath, ComplexPropertySegment.ComplexType))
133138
?? Context.CreateExpand(ComplexPropertySegment.Property, ComplexPropertySegment.ComplexType);
134139
if (parameter != null)
135140
{
@@ -139,13 +144,14 @@ protected override void SetParameters(OpenApiOperation operation)
139144

140145
protected override void SetExtensions(OpenApiOperation operation)
141146
{
142-
if (Context.Settings.EnablePagination && ComplexPropertySegment.Property.Type.IsCollection())
147+
if (Context is {Settings.EnablePagination: true} && ComplexPropertySegment.Property.Type.IsCollection())
143148
{
144149
JsonObject extension = new()
145150
{
146151
{ "nextLinkName", "@odata.nextLink"},
147152
{ "operationName", Context.Settings.PageableOperationName}
148153
};
154+
operation.Extensions ??= new Dictionary<string, IOpenApiExtension>();
149155
operation.Extensions.Add(Constants.xMsPageable, new OpenApiAny(extension));
150156

151157
base.SetExtensions(operation);
@@ -165,7 +171,8 @@ protected override void SetResponses(OpenApiOperation operation)
165171
SetSingleResponse(operation, schema);
166172
}
167173

168-
operation.AddErrorResponses(Context.Settings, _document, false);
174+
if (Context is not null)
175+
operation.AddErrorResponses(Context.Settings, _document, false);
169176
base.SetResponses(operation);
170177
}
171178

@@ -176,7 +183,7 @@ protected override void SetSecurity(OpenApiOperation operation)
176183
return;
177184
}
178185

179-
operation.Security = Context.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
186+
operation.Security = Context?.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
180187
}
181188

182189
protected override void AppendCustomParameters(OpenApiOperation operation)

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@ public SingletonGetOperationHandler(OpenApiDocument document) : base(document)
3535
/// <inheritdoc/>
3636
public override HttpMethod OperationType => HttpMethod.Get;
3737

38-
private ReadRestrictionsType _readRestrictions;
38+
private ReadRestrictionsType? _readRestrictions;
3939

4040
protected override void Initialize(ODataContext context, ODataPath path)
4141
{
4242
base.Initialize(context, path);
4343

44-
_readRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
45-
var singletonReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(Singleton, CapabilitiesConstants.ReadRestrictions);
46-
_readRestrictions?.MergePropertiesIfNull(singletonReadRestrictions);
47-
_readRestrictions ??= singletonReadRestrictions;
44+
if (!string.IsNullOrEmpty(TargetPath))
45+
_readRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
46+
47+
if (Context is not null)
48+
{
49+
var singletonReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(Singleton, CapabilitiesConstants.ReadRestrictions);
50+
_readRestrictions?.MergePropertiesIfNull(singletonReadRestrictions);
51+
_readRestrictions ??= singletonReadRestrictions;
52+
}
4853
}
4954

5055
/// <inheritdoc/>
@@ -53,10 +58,10 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5358
// Summary and Descriptions
5459
string placeHolder = "Get " + Singleton.Name;
5560
operation.Summary = _readRestrictions?.Description ?? placeHolder;
56-
operation.Description = _readRestrictions?.LongDescription ?? Context.Model.GetDescriptionAnnotation(Singleton);
61+
operation.Description = _readRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(Singleton);
5762

5863
// OperationId, it should be unique among all operations described in the API.
59-
if (Context.Settings.EnableOperationId)
64+
if (Context is {Settings.EnableOperationId: true})
6065
{
6166
string typeName = Singleton.EntityType.Name;
6267
operation.OperationId = Singleton.Name + "." + typeName + ".Get" + Utils.UpperFirstChar(typeName);
@@ -69,14 +74,15 @@ protected override void SetParameters(OpenApiOperation operation)
6974
base.SetParameters(operation);
7075

7176
// $select
72-
OpenApiParameter parameter = Context.CreateSelect(Singleton);
77+
var parameter = Context?.CreateSelect(Singleton);
78+
operation.Parameters ??= [];
7379
if (parameter != null)
7480
{
7581
operation.Parameters.Add(parameter);
7682
}
7783

7884
// $expand
79-
parameter = Context.CreateExpand(Singleton);
85+
parameter = Context?.CreateExpand(Singleton);
8086
if (parameter != null)
8187
{
8288
operation.Parameters.Add(parameter);
@@ -86,15 +92,15 @@ protected override void SetParameters(OpenApiOperation operation)
8692
/// <inheritdoc/>
8793
protected override void SetResponses(OpenApiOperation operation)
8894
{
89-
IOpenApiSchema schema = null;
90-
IDictionary<string, IOpenApiLink> links = null;
95+
IOpenApiSchema? schema = null;
96+
IDictionary<string, IOpenApiLink>? links = null;
9197

92-
if (Context.Settings.EnableDerivedTypesReferencesForResponses)
98+
if (Context is {Settings.EnableDerivedTypesReferencesForResponses: true})
9399
{
94100
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(Singleton.EntityType, Context.Model, _document);
95101
}
96102

97-
if (Context.Settings.ShowLinks)
103+
if (Context is {Settings.ShowLinks: true} && Path is not null)
98104
{
99105
links = Context.CreateLinks(entityType: Singleton.EntityType, entityName: Singleton.Name,
100106
entityKind: Singleton.ContainerElementKind.ToString(), path: Path, parameters: PathParameters);
@@ -105,7 +111,7 @@ protected override void SetResponses(OpenApiOperation operation)
105111
operation.Responses = new OpenApiResponses
106112
{
107113
{
108-
Context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
114+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
109115
new OpenApiResponse
110116
{
111117
Description = "Retrieved entity",
@@ -124,7 +130,8 @@ protected override void SetResponses(OpenApiOperation operation)
124130
}
125131
};
126132

127-
operation.AddErrorResponses(Context.Settings, _document, false);
133+
if (Context is not null)
134+
operation.AddErrorResponses(Context.Settings, _document, false);
128135

129136
base.SetResponses(operation);
130137
}
@@ -137,7 +144,7 @@ protected override void SetSecurity(OpenApiOperation operation)
137144
return;
138145
}
139146

140-
operation.Security = Context.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
147+
operation.Security = Context?.CreateSecurityRequirements(_readRestrictions.Permissions, _document).ToList();
141148
}
142149

143150
/// <inheritdoc/>

0 commit comments

Comments
 (0)