Skip to content

Commit 102ee5b

Browse files
committed
refactor entity set update (patch and put) operation handler classes to
use same base class
1 parent 6b30bb0 commit 102ee5b

File tree

4 files changed

+126
-219
lines changed

4 files changed

+126
-219
lines changed

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

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

6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using Microsoft.OData.Edm;
96
using Microsoft.OpenApi.Models;
10-
using Microsoft.OpenApi.OData.Common;
11-
using Microsoft.OpenApi.OData.Edm;
12-
using Microsoft.OpenApi.OData.Generator;
13-
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
147

158
namespace Microsoft.OpenApi.OData.Operation
169
{
@@ -19,110 +12,9 @@ namespace Microsoft.OpenApi.OData.Operation
1912
/// The Path Item Object for the entity set contains the keyword patch with an Operation Object as value
2013
/// that describes the capabilities for updating the entity.
2114
/// </summary>
22-
internal class EntityPatchOperationHandler : EntitySetOperationHandler
15+
internal class EntityPatchOperationHandler : EntityUpdateOperationHandler
2316
{
2417
/// <inheritdoc/>
2518
public override OperationType OperationType => OperationType.Patch;
26-
27-
/// <inheritdoc/>
28-
protected override void SetBasicInfo(OpenApiOperation operation)
29-
{
30-
// Summary
31-
operation.Summary = "Update entity in " + EntitySet.Name;
32-
33-
IEdmEntityType entityType = EntitySet.EntityType();
34-
35-
// Description
36-
operation.Description = Context.Model.GetDescriptionAnnotation(entityType);
37-
38-
// OperationId
39-
if (Context.Settings.EnableOperationId)
40-
{
41-
string typeName = entityType.Name;
42-
operation.OperationId = EntitySet.Name + "." + typeName + ".Update" + Utils.UpperFirstChar(typeName);
43-
}
44-
}
45-
46-
/// <inheritdoc/>
47-
protected override void SetRequestBody(OpenApiOperation operation)
48-
{
49-
OpenApiSchema schema = null;
50-
51-
if (Context.Settings.EnableDerivedTypesReferencesForRequestBody)
52-
{
53-
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(EntitySet.EntityType(), Context.Model);
54-
}
55-
56-
if (schema == null)
57-
{
58-
schema = new OpenApiSchema
59-
{
60-
Reference = new OpenApiReference
61-
{
62-
Type = ReferenceType.Schema,
63-
Id = EntitySet.EntityType().FullName()
64-
}
65-
};
66-
}
67-
68-
operation.RequestBody = new OpenApiRequestBody
69-
{
70-
Required = true,
71-
Description = "New property values",
72-
Content = new Dictionary<string, OpenApiMediaType>
73-
{
74-
{
75-
Constants.ApplicationJsonMediaType, new OpenApiMediaType
76-
{
77-
Schema = schema
78-
}
79-
}
80-
}
81-
};
82-
83-
base.SetRequestBody(operation);
84-
}
85-
86-
/// <inheritdoc/>
87-
protected override void SetResponses(OpenApiOperation operation)
88-
{
89-
operation.Responses = new OpenApiResponses
90-
{
91-
{ Constants.StatusCode204, Constants.StatusCode204.GetResponse() },
92-
{ Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse() }
93-
};
94-
95-
base.SetResponses(operation);
96-
}
97-
98-
protected override void SetSecurity(OpenApiOperation operation)
99-
{
100-
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
101-
if (update == null || update.Permissions == null)
102-
{
103-
return;
104-
}
105-
106-
operation.Security = Context.CreateSecurityRequirements(update.Permissions).ToList();
107-
}
108-
109-
protected override void AppendCustomParameters(OpenApiOperation operation)
110-
{
111-
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
112-
if (update == null)
113-
{
114-
return;
115-
}
116-
117-
if (update.CustomHeaders != null)
118-
{
119-
AppendCustomParameters(operation, update.CustomHeaders, ParameterLocation.Header);
120-
}
121-
122-
if (update.CustomQueryOptions != null)
123-
{
124-
AppendCustomParameters(operation, update.CustomQueryOptions, ParameterLocation.Query);
125-
}
126-
}
12719
}
12820
}

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

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

6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using Microsoft.OData.Edm;
96
using Microsoft.OpenApi.Models;
10-
using Microsoft.OpenApi.OData.Common;
11-
using Microsoft.OpenApi.OData.Edm;
12-
using Microsoft.OpenApi.OData.Generator;
13-
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
147

158
namespace Microsoft.OpenApi.OData.Operation
169
{
@@ -19,110 +12,9 @@ namespace Microsoft.OpenApi.OData.Operation
1912
/// The Path Item Object for the entity set contains the keyword put with an Operation Object as value
2013
/// that describes the capabilities for updating the entity.
2114
/// </summary>
22-
internal class EntityPutOperationHandler : EntitySetOperationHandler
15+
internal class EntityPutOperationHandler : EntityUpdateOperationHandler
2316
{
2417
/// <inheritdoc/>
2518
public override OperationType OperationType => OperationType.Put;
26-
27-
/// <inheritdoc/>
28-
protected override void SetBasicInfo(OpenApiOperation operation)
29-
{
30-
// Summary
31-
operation.Summary = "Update entity in " + EntitySet.Name;
32-
33-
IEdmEntityType entityType = EntitySet.EntityType();
34-
35-
// Description
36-
operation.Description = Context.Model.GetDescriptionAnnotation(entityType);
37-
38-
// OperationId
39-
if (Context.Settings.EnableOperationId)
40-
{
41-
string typeName = entityType.Name;
42-
operation.OperationId = EntitySet.Name + "." + typeName + ".Update" + Utils.UpperFirstChar(typeName);
43-
}
44-
}
45-
46-
/// <inheritdoc/>
47-
protected override void SetRequestBody(OpenApiOperation operation)
48-
{
49-
OpenApiSchema schema = null;
50-
51-
if (Context.Settings.EnableDerivedTypesReferencesForRequestBody)
52-
{
53-
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(EntitySet.EntityType(), Context.Model);
54-
}
55-
56-
if (schema == null)
57-
{
58-
schema = new OpenApiSchema
59-
{
60-
Reference = new OpenApiReference
61-
{
62-
Type = ReferenceType.Schema,
63-
Id = EntitySet.EntityType().FullName()
64-
}
65-
};
66-
}
67-
68-
operation.RequestBody = new OpenApiRequestBody
69-
{
70-
Required = true,
71-
Description = "New property values",
72-
Content = new Dictionary<string, OpenApiMediaType>
73-
{
74-
{
75-
Constants.ApplicationJsonMediaType, new OpenApiMediaType
76-
{
77-
Schema = schema
78-
}
79-
}
80-
}
81-
};
82-
83-
base.SetRequestBody(operation);
84-
}
85-
86-
/// <inheritdoc/>
87-
protected override void SetResponses(OpenApiOperation operation)
88-
{
89-
operation.Responses = new OpenApiResponses
90-
{
91-
{ Constants.StatusCode204, Constants.StatusCode204.GetResponse() },
92-
{ Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse() }
93-
};
94-
95-
base.SetResponses(operation);
96-
}
97-
98-
protected override void SetSecurity(OpenApiOperation operation)
99-
{
100-
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
101-
if (update == null || update.Permissions == null)
102-
{
103-
return;
104-
}
105-
106-
operation.Security = Context.CreateSecurityRequirements(update.Permissions).ToList();
107-
}
108-
109-
protected override void AppendCustomParameters(OpenApiOperation operation)
110-
{
111-
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
112-
if (update == null)
113-
{
114-
return;
115-
}
116-
117-
if (update.CustomHeaders != null)
118-
{
119-
AppendCustomParameters(operation, update.CustomHeaders, ParameterLocation.Header);
120-
}
121-
122-
if (update.CustomQueryOptions != null)
123-
{
124-
AppendCustomParameters(operation, update.CustomQueryOptions, ParameterLocation.Query);
125-
}
126-
}
12719
}
12820
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// ------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
4+
// ------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Microsoft.OData.Edm;
9+
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.OData.Common;
11+
using Microsoft.OpenApi.OData.Edm;
12+
using Microsoft.OpenApi.OData.Generator;
13+
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
14+
15+
namespace Microsoft.OpenApi.OData.Operation
16+
{
17+
/// <summary>
18+
/// Base class for entity set update (patch or put) operation.
19+
/// </summary>
20+
internal abstract class EntityUpdateOperationHandler : EntitySetOperationHandler
21+
{
22+
/// <inheritdoc/>
23+
protected override void SetBasicInfo(OpenApiOperation operation)
24+
{
25+
// Summary
26+
operation.Summary = "Update entity in " + EntitySet.Name;
27+
28+
IEdmEntityType entityType = EntitySet.EntityType();
29+
30+
// Description
31+
operation.Description = Context.Model.GetDescriptionAnnotation(entityType);
32+
33+
// OperationId
34+
if (Context.Settings.EnableOperationId)
35+
{
36+
string typeName = entityType.Name;
37+
operation.OperationId = EntitySet.Name + "." + typeName + ".Update" + Utils.UpperFirstChar(typeName);
38+
}
39+
}
40+
41+
/// <inheritdoc/>
42+
protected override void SetRequestBody(OpenApiOperation operation)
43+
{
44+
OpenApiSchema schema = null;
45+
46+
if (Context.Settings.EnableDerivedTypesReferencesForRequestBody)
47+
{
48+
schema = EdmModelHelper.GetDerivedTypesReferenceSchema(EntitySet.EntityType(), Context.Model);
49+
}
50+
51+
if (schema == null)
52+
{
53+
schema = new OpenApiSchema
54+
{
55+
Reference = new OpenApiReference
56+
{
57+
Type = ReferenceType.Schema,
58+
Id = EntitySet.EntityType().FullName()
59+
}
60+
};
61+
}
62+
63+
operation.RequestBody = new OpenApiRequestBody
64+
{
65+
Required = true,
66+
Description = "New property values",
67+
Content = new Dictionary<string, OpenApiMediaType>
68+
{
69+
{
70+
Constants.ApplicationJsonMediaType, new OpenApiMediaType
71+
{
72+
Schema = schema
73+
}
74+
}
75+
}
76+
};
77+
78+
base.SetRequestBody(operation);
79+
}
80+
81+
/// <inheritdoc/>
82+
protected override void SetResponses(OpenApiOperation operation)
83+
{
84+
operation.Responses = new OpenApiResponses
85+
{
86+
{ Constants.StatusCode204, Constants.StatusCode204.GetResponse() },
87+
{ Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse() }
88+
};
89+
90+
base.SetResponses(operation);
91+
}
92+
93+
protected override void SetSecurity(OpenApiOperation operation)
94+
{
95+
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
96+
if (update == null || update.Permissions == null)
97+
{
98+
return;
99+
}
100+
101+
operation.Security = Context.CreateSecurityRequirements(update.Permissions).ToList();
102+
}
103+
104+
protected override void AppendCustomParameters(OpenApiOperation operation)
105+
{
106+
UpdateRestrictionsType update = Context.Model.GetRecord<UpdateRestrictionsType>(EntitySet, CapabilitiesConstants.UpdateRestrictions);
107+
if (update == null)
108+
{
109+
return;
110+
}
111+
112+
if (update.CustomHeaders != null)
113+
{
114+
AppendCustomParameters(operation, update.CustomHeaders, ParameterLocation.Header);
115+
}
116+
117+
if (update.CustomQueryOptions != null)
118+
{
119+
AppendCustomParameters(operation, update.CustomQueryOptions, ParameterLocation.Query);
120+
}
121+
}
122+
}
123+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private readonly IDictionary<ODataPathKind, IDictionary<OperationType, IOperatio
2424
{OperationType.Post, new EntitySetPostOperationHandler() }
2525
}},
2626

27-
// entity (Get/Patch/Delete)
27+
// entity (Get/Patch/Put/Delete)
2828
{ODataPathKind.Entity, new Dictionary<OperationType, IOperationHandler>
2929
{
3030
{OperationType.Get, new EntityGetOperationHandler() },

0 commit comments

Comments
 (0)