Skip to content

Commit 3ae498c

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

File tree

6 files changed

+109
-88
lines changed

6 files changed

+109
-88
lines changed

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ protected override void Initialize(ODataContext context, ODataPath path)
4040
base.Initialize(context, path);
4141

4242
_readRestrictions = string.IsNullOrEmpty(TargetPath) ? null : Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
43-
var complexPropertyReadRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.ReadRestrictions);
44-
_readRestrictions?.MergePropertiesIfNull(complexPropertyReadRestrictions);
45-
_readRestrictions ??= complexPropertyReadRestrictions;
43+
44+
if (ComplexPropertySegment is not null)
45+
{
46+
var complexPropertyReadRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(ComplexPropertySegment.Property, CapabilitiesConstants.ReadRestrictions);
47+
_readRestrictions?.MergePropertiesIfNull(complexPropertyReadRestrictions);
48+
_readRestrictions ??= complexPropertyReadRestrictions;
49+
}
4650
}
4751

4852
/// <inheritdoc/>
@@ -51,14 +55,15 @@ protected override void SetBasicInfo(OpenApiOperation operation)
5155
// OperationId
5256
if (Context is {Settings.EnableOperationId: true} && Path is not null)
5357
{
54-
string prefix = ComplexPropertySegment.Property.Type.IsCollection() ? "List" : "Get";
58+
string prefix = ComplexPropertySegment is not null && ComplexPropertySegment.Property.Type.IsCollection() ? "List" : "Get";
5559
operation.OperationId = EdmModelHelper.GenerateComplexPropertyPathOperationId(Path, Context, prefix);
5660
}
5761

5862
// Summary and Description
59-
string placeHolder = $"Get {ComplexPropertySegment.Property.Name} property value";
63+
string placeHolder = $"Get {ComplexPropertySegment?.Property.Name} property value";
6064
operation.Summary = _readRestrictions?.Description ?? placeHolder;
61-
operation.Description = _readRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(ComplexPropertySegment.Property);
65+
operation.Description = _readRestrictions?.LongDescription ??
66+
(ComplexPropertySegment is null ? null : Context?.Model.GetDescriptionAnnotation(ComplexPropertySegment.Property));
6267

6368
base.SetBasicInfo(operation);
6469
}
@@ -67,7 +72,7 @@ protected override void SetParameters(OpenApiOperation operation)
6772
{
6873
base.SetParameters(operation);
6974

70-
if (Context is null) return;
75+
if (Context is null || ComplexPropertySegment is null) return;
7176

7277
IOpenApiParameter? parameter;
7378
operation.Parameters ??= [];
@@ -144,7 +149,9 @@ protected override void SetParameters(OpenApiOperation operation)
144149

145150
protected override void SetExtensions(OpenApiOperation operation)
146151
{
147-
if (Context is {Settings.EnablePagination: true} && ComplexPropertySegment.Property.Type.IsCollection())
152+
if (Context is {Settings.EnablePagination: true} &&
153+
ComplexPropertySegment is not null &&
154+
ComplexPropertySegment.Property.Type.IsCollection())
148155
{
149156
JsonObject extension = new()
150157
{
@@ -160,16 +167,17 @@ protected override void SetExtensions(OpenApiOperation operation)
160167
/// <inheritdoc/>
161168
protected override void SetResponses(OpenApiOperation operation)
162169
{
163-
if (ComplexPropertySegment.Property.Type.IsCollection())
164-
{
165-
SetCollectionResponse(operation, ComplexPropertySegment.ComplexType.FullName());
166-
}
167-
else
168-
{
169-
var schema = new OpenApiSchemaReference(ComplexPropertySegment.ComplexType.FullName(), _document);
170+
if (ComplexPropertySegment is not null)
171+
if (ComplexPropertySegment.Property.Type.IsCollection())
172+
{
173+
SetCollectionResponse(operation, ComplexPropertySegment.ComplexType.FullName());
174+
}
175+
else
176+
{
177+
var schema = new OpenApiSchemaReference(ComplexPropertySegment.ComplexType.FullName(), _document);
170178

171-
SetSingleResponse(operation, schema);
172-
}
179+
SetSingleResponse(operation, schema);
180+
}
173181

174182
if (Context is not null)
175183
operation.AddErrorResponses(Context.Settings, _document, false);

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

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected override void Initialize(ODataContext context, ODataPath path)
4444
if (!string.IsNullOrEmpty(TargetPath))
4545
_readRestrictions = Context?.Model.GetRecord<ReadRestrictionsType>(TargetPath, CapabilitiesConstants.ReadRestrictions);
4646

47-
if (Context is not null)
47+
if (Context is not null && Singleton is not null)
4848
{
4949
var singletonReadRestrictions = Context.Model.GetRecord<ReadRestrictionsType>(Singleton, CapabilitiesConstants.ReadRestrictions);
5050
_readRestrictions?.MergePropertiesIfNull(singletonReadRestrictions);
@@ -56,12 +56,12 @@ protected override void Initialize(ODataContext context, ODataPath path)
5656
protected override void SetBasicInfo(OpenApiOperation operation)
5757
{
5858
// Summary and Descriptions
59-
string placeHolder = "Get " + Singleton.Name;
59+
string placeHolder = "Get " + Singleton?.Name;
6060
operation.Summary = _readRestrictions?.Description ?? placeHolder;
6161
operation.Description = _readRestrictions?.LongDescription ?? Context?.Model.GetDescriptionAnnotation(Singleton);
6262

6363
// OperationId, it should be unique among all operations described in the API.
64-
if (Context is {Settings.EnableOperationId: true})
64+
if (Context is {Settings.EnableOperationId: true} && Singleton is not null)
6565
{
6666
string typeName = Singleton.EntityType.Name;
6767
operation.OperationId = Singleton.Name + "." + typeName + ".Get" + Utils.UpperFirstChar(typeName);
@@ -72,6 +72,8 @@ protected override void SetBasicInfo(OpenApiOperation operation)
7272
protected override void SetParameters(OpenApiOperation operation)
7373
{
7474
base.SetParameters(operation);
75+
76+
if (Singleton is null) return;
7577

7678
// $select
7779
var parameter = Context?.CreateSelect(Singleton);
@@ -92,43 +94,46 @@ protected override void SetParameters(OpenApiOperation operation)
9294
/// <inheritdoc/>
9395
protected override void SetResponses(OpenApiOperation operation)
9496
{
95-
IOpenApiSchema? schema = null;
96-
IDictionary<string, IOpenApiLink>? links = null;
97-
98-
if (Context is {Settings.EnableDerivedTypesReferencesForResponses: true})
97+
if (Singleton is not null)
9998
{
100-
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(Singleton.EntityType, Context.Model, _document);
101-
}
99+
IOpenApiSchema? schema = null;
100+
IDictionary<string, IOpenApiLink>? links = null;
102101

103-
if (Context is {Settings.ShowLinks: true} && Path is not null)
104-
{
105-
links = Context.CreateLinks(entityType: Singleton.EntityType, entityName: Singleton.Name,
106-
entityKind: Singleton.ContainerElementKind.ToString(), path: Path, parameters: PathParameters);
107-
}
102+
if (Context is {Settings.EnableDerivedTypesReferencesForResponses: true})
103+
{
104+
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(Singleton.EntityType, Context.Model, _document);
105+
}
108106

109-
schema ??= new OpenApiSchemaReference(Singleton.EntityType.FullName(), _document);
107+
if (Context is {Settings.ShowLinks: true} && Path is not null)
108+
{
109+
links = Context.CreateLinks(entityType: Singleton.EntityType, entityName: Singleton.Name,
110+
entityKind: Singleton.ContainerElementKind.ToString(), path: Path, parameters: PathParameters);
111+
}
110112

111-
operation.Responses = new OpenApiResponses
112-
{
113+
schema ??= new OpenApiSchemaReference(Singleton.EntityType.FullName(), _document);
114+
115+
operation.Responses = new OpenApiResponses
113116
{
114-
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
115-
new OpenApiResponse
116117
{
117-
Description = "Retrieved entity",
118-
Content = new Dictionary<string, OpenApiMediaType>
118+
Context?.Settings.UseSuccessStatusCodeRange ?? false ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
119+
new OpenApiResponse
119120
{
121+
Description = "Retrieved entity",
122+
Content = new Dictionary<string, OpenApiMediaType>
120123
{
121-
Constants.ApplicationJsonMediaType,
122-
new OpenApiMediaType
123124
{
124-
Schema = schema
125+
Constants.ApplicationJsonMediaType,
126+
new OpenApiMediaType
127+
{
128+
Schema = schema
129+
}
125130
}
126-
}
127-
},
128-
Links = links
131+
},
132+
Links = links
133+
}
129134
}
130-
}
131-
};
135+
};
136+
}
132137

133138
if (Context is not null)
134139
operation.AddErrorResponses(Context.Settings, _document, false);

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

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

66
using Microsoft.OData.Edm;
77
using Microsoft.OpenApi.Any;
8+
using Microsoft.OpenApi.Interfaces;
89
using Microsoft.OpenApi.Models;
910
using Microsoft.OpenApi.Models.References;
1011
using Microsoft.OpenApi.OData.Common;
@@ -30,27 +31,26 @@ protected SingletonOperationHandler(OpenApiDocument document):base(document)
3031
/// <summary>
3132
/// Gets the <see cref="IEdmSingleton"/>.
3233
/// </summary>
33-
protected IEdmSingleton Singleton { get; private set; }
34+
protected IEdmSingleton? Singleton { get; private set; }
3435

3536
/// <inheritdoc/>
3637
protected override void Initialize(ODataContext context, ODataPath path)
3738
{
3839
// Base Initialize should be called at top of this method.
3940
base.Initialize(context, path);
4041

41-
ODataNavigationSourceSegment navigationSourceSegment = path.FirstSegment as ODataNavigationSourceSegment;
42-
43-
Singleton = navigationSourceSegment.NavigationSource as IEdmSingleton;
42+
if (path.FirstSegment is ODataNavigationSourceSegment {NavigationSource: IEdmSingleton source})
43+
Singleton = source;
4444
}
4545

4646
/// <inheritdoc/>
4747
protected override void SetTags(OpenApiOperation operation)
4848
{
4949
// In this SDK, we use "[Singleton Name].[Singleton Entity Type Name]
5050
// For example: "Me.User"
51-
var tagName = Singleton.Name + "." + Singleton.EntityType.Name;
51+
var tagName = Singleton?.Name + "." + Singleton?.EntityType.Name;
5252

53-
Context.AddExtensionToTag(tagName, Constants.xMsTocType, new OpenApiAny("page"), () => new OpenApiTag()
53+
Context?.AddExtensionToTag(tagName, Constants.xMsTocType, new OpenApiAny("page"), () => new OpenApiTag()
5454
{
5555
Name = tagName
5656
});
@@ -64,6 +64,7 @@ protected override void SetTags(OpenApiOperation operation)
6464
/// <inheritdoc/>
6565
protected override void SetExtensions(OpenApiOperation operation)
6666
{
67+
operation.Extensions ??= new Dictionary<string, IOpenApiExtension>();
6768
operation.Extensions.Add(Constants.xMsDosOperationType, new OpenApiAny("operation"));
6869

6970
base.SetExtensions(operation);
@@ -72,10 +73,10 @@ protected override void SetExtensions(OpenApiOperation operation)
7273
/// <inheritdoc/>
7374
protected override void SetExternalDocs(OpenApiOperation operation)
7475
{
75-
if (Context.Settings.ShowExternalDocs)
76+
if (Context is {Settings.ShowExternalDocs: true} && CustomLinkRel is not null)
7677
{
77-
var externalDocs = Context.Model.GetLinkRecord(TargetPath, CustomLinkRel) ??
78-
Context.Model.GetLinkRecord(Singleton, CustomLinkRel);
78+
var externalDocs = (string.IsNullOrEmpty(TargetPath) ? null : Context.Model.GetLinkRecord(TargetPath, CustomLinkRel)) ??
79+
(Singleton is null ? null : Context.Model.GetLinkRecord(Singleton, CustomLinkRel));
7980

8081
if (externalDocs != null)
8182
{

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,33 @@ public SingletonPatchOperationHandler(OpenApiDocument document) : base(document)
3535
/// <inheritdoc/>
3636
public override HttpMethod OperationType => HttpMethod.Patch;
3737

38-
private UpdateRestrictionsType _updateRestrictions;
38+
private UpdateRestrictionsType? _updateRestrictions;
3939

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

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

5055
/// <inheritdoc/>
5156
protected override void SetBasicInfo(OpenApiOperation operation)
5257
{
5358
// Summary and Descriptions
54-
string placeHolder = "Update " + Singleton.Name;
59+
string placeHolder = "Update " + Singleton?.Name;
5560
operation.Summary = _updateRestrictions?.Description ?? placeHolder;
5661
operation.Description = _updateRestrictions?.LongDescription;
5762

5863
// OperationId
59-
if (Context.Settings.EnableOperationId)
64+
if (Context is {Settings.EnableOperationId: true} && Singleton is not null)
6065
{
6166
string typeName = Singleton.EntityType.Name;
6267
operation.OperationId = Singleton.Name + "." + typeName + ".Update" + Utils.UpperFirstChar(typeName);
@@ -87,7 +92,8 @@ protected override void SetRequestBody(OpenApiOperation operation)
8792
/// <inheritdoc/>
8893
protected override void SetResponses(OpenApiOperation operation)
8994
{
90-
operation.AddErrorResponses(Context.Settings, _document, true, GetOpenApiSchema());
95+
if (Context is not null && GetOpenApiSchema() is {} schema)
96+
operation.AddErrorResponses(Context.Settings, _document, true, schema);
9197
base.SetResponses(operation);
9298
}
9399

@@ -99,7 +105,7 @@ protected override void SetSecurity(OpenApiOperation operation)
99105
return;
100106
}
101107

102-
operation.Security = Context.CreateSecurityRequirements(_updateRestrictions.Permissions, _document).ToList();
108+
operation.Security = Context?.CreateSecurityRequirements(_updateRestrictions.Permissions, _document).ToList();
103109
}
104110

105111
/// <inheritdoc/>
@@ -121,9 +127,10 @@ protected override void AppendCustomParameters(OpenApiOperation operation)
121127
}
122128
}
123129

124-
private IOpenApiSchema GetOpenApiSchema()
130+
private IOpenApiSchema? GetOpenApiSchema()
125131
{
126-
return Context.Settings.EnableDerivedTypesReferencesForRequestBody ?
132+
if (Singleton is null) return null;
133+
return Context is {Settings.EnableDerivedTypesReferencesForRequestBody: true} ?
127134
EdmModelHelper.GetDerivedTypesReferenceSchema(Singleton.EntityType, Context.Model, _document) :
128135
new OpenApiSchemaReference(Singleton.EntityType.FullName(), _document);
129136
}

0 commit comments

Comments
 (0)