Skip to content

Commit c4ee9ff

Browse files
committed
feat: add switch to use put as default update verb
1 parent 3cb8977 commit c4ee9ff

File tree

7 files changed

+76
-4
lines changed

7 files changed

+76
-4
lines changed

src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ public string? PathPrefix
337337
/// </summary>
338338
public int ComposableFunctionsExpansionDepth { get; set; } = 1;
339339

340+
/// <summary>
341+
/// Gets/sets a value indicating whether to use HTTP PUT method for update operations by default
342+
/// instead of PATCH when no UpdateRestrictions annotation is present in the CSDL.
343+
/// If false (default), PATCH will be used for updates.
344+
/// If true, PUT will be used for updates.
345+
/// This setting is ignored when UpdateRestrictions annotations are present in the CSDL.
346+
/// </summary>
347+
public bool UseHttpPutForUpdate { get; set; } = false;
348+
340349
internal OpenApiConvertSettings Clone()
341350
{
342351
var newSettings = new OpenApiConvertSettings
@@ -392,7 +401,8 @@ internal OpenApiConvertSettings Clone()
392401
SemVerVersion = this.SemVerVersion,
393402
EnableAliasForOperationSegments = this.EnableAliasForOperationSegments,
394403
UseStringArrayForQueryOptionsSchema = this.UseStringArrayForQueryOptionsSchema,
395-
ComposableFunctionsExpansionDepth = this.ComposableFunctionsExpansionDepth
404+
ComposableFunctionsExpansionDepth = this.ComposableFunctionsExpansionDepth,
405+
UseHttpPutForUpdate = this.UseHttpPutForUpdate
396406
};
397407

398408
return newSettings;

src/Microsoft.OpenApi.OData.Reader/PathItem/ComplexPropertyItemHandler.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,16 @@ public void AddUpdateOperation(OpenApiPathItem item)
9999
}
100100
else
101101
{
102-
AddOperation(item, HttpMethod.Patch);
102+
// When no explicit update method is specified in UpdateRestrictions,
103+
// use the UseHttpPutForUpdate setting to determine the default method
104+
if (Context?.Settings?.UseHttpPutForUpdate == true)
105+
{
106+
AddOperation(item, HttpMethod.Put);
107+
}
108+
else
109+
{
110+
AddOperation(item, HttpMethod.Patch);
111+
}
103112
}
104113
}
105114
}

src/Microsoft.OpenApi.OData.Reader/PathItem/EntityPathItemHandler.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ protected override void SetOperations(OpenApiPathItem item)
6767
}
6868
else
6969
{
70-
AddOperation(item, HttpMethod.Patch);
70+
// When no explicit update method is specified in UpdateRestrictions,
71+
// use the UseHttpPutForUpdate setting to determine the default method
72+
if (Context?.Settings?.UseHttpPutForUpdate == true)
73+
{
74+
AddOperation(item, HttpMethod.Put);
75+
}
76+
else
77+
{
78+
AddOperation(item, HttpMethod.Patch);
79+
}
7180
}
7281
}
7382

src/Microsoft.OpenApi.OData.Reader/PathItem/NavigationPropertyPathItemHandler.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,16 @@ private void AddUpdateOperation(OpenApiPathItem item, UpdateRestrictionsType? up
279279
}
280280
else
281281
{
282-
AddOperation(item, HttpMethod.Patch);
282+
// When no explicit update method is specified in UpdateRestrictions,
283+
// use the UseHttpPutForUpdate setting to determine the default method
284+
if (Context?.Settings?.UseHttpPutForUpdate == true)
285+
{
286+
AddOperation(item, HttpMethod.Put);
287+
}
288+
else
289+
{
290+
AddOperation(item, HttpMethod.Patch);
291+
}
283292
}
284293
}
285294

src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Microsoft.OpenApi.OData.OpenApiConvertSettings.UseStringArrayForQueryOptionsSche
221221
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseStringArrayForQueryOptionsSchema.set -> void
222222
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseSuccessStatusCodeRange.get -> bool
223223
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseSuccessStatusCodeRange.set -> void
224+
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseHttpPutForUpdate.get -> bool
225+
Microsoft.OpenApi.OData.OpenApiConvertSettings.UseHttpPutForUpdate.set -> void
224226
Microsoft.OpenApi.OData.OpenApiConvertSettings.VerifyEdmModel.get -> bool
225227
Microsoft.OpenApi.OData.OpenApiConvertSettings.VerifyEdmModel.set -> void
226228
Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey

src/OoasUtil/ComLineProcessor.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public ComLineProcessor(string[] args)
9999
/// </summary>
100100
public bool? RequireDerivedTypesConstraint { get; private set; }
101101

102+
/// <summary>
103+
/// Use HTTP PUT method for update operations by default instead of PATCH.
104+
/// </summary>
105+
public bool? UseHttpPutForUpdate { get; private set; }
106+
102107
/// <summary>
103108
/// Process the arguments.
104109
/// </summary>
@@ -227,6 +232,14 @@ public bool Process()
227232
}
228233
break;
229234

235+
case "--useputforupdate":
236+
case "-put":
237+
if (!ProcessUseHttpPutForUpdate(true))
238+
{
239+
return false;
240+
}
241+
break;
242+
230243
default:
231244
PrintUsage();
232245
return false;
@@ -285,6 +298,11 @@ public bool Process()
285298
DisableSchemaExamples = false;
286299
}
287300

301+
if (UseHttpPutForUpdate == null)
302+
{
303+
UseHttpPutForUpdate = false;
304+
}
305+
288306
_continue = ValidateArguments();
289307
return _continue;
290308
}
@@ -419,6 +437,19 @@ private bool ProcessDisableSchemaExamples(bool disableSchemaExamples)
419437
return true;
420438
}
421439

440+
private bool ProcessUseHttpPutForUpdate(bool useHttpPutForUpdate)
441+
{
442+
if (UseHttpPutForUpdate != null)
443+
{
444+
Console.WriteLine("[Error:] Multiple [--useputforupdate|-put] are not allowed.\n");
445+
PrintUsage();
446+
return false;
447+
}
448+
449+
UseHttpPutForUpdate = useHttpPutForUpdate;
450+
return true;
451+
}
452+
422453
private bool ProcessTarget(int version)
423454
{
424455
if (Version != null)
@@ -484,6 +515,7 @@ public static void PrintUsage()
484515
sb.Append(" --enablepagination|-p\t\t\tSet the output to expose pagination for collections.\n");
485516
sb.Append(" --enableunqualifiedcall|-u\t\t\tSet the output to use unqualified calls for bound operations.\n");
486517
sb.Append(" --disableschemaexamples|-x\t\t\tDisable examples in the schema.\n");
518+
sb.Append(" --useputforupdate|-put\t\t\tUse HTTP PUT method for update operations instead of PATCH by default.\n");
487519
sb.Append(" --json|-j\t\t\tSet the output format as JSON.\n");
488520
sb.Append(" --yaml|-y\t\t\tSet the output format as YAML.\n");
489521
sb.Append(" --specversion|-s IntVersion\tSet the OpenApi Specification version of the output. Only 2 or 3 are supported.\n");

src/OoasUtil/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static async System.Threading.Tasks.Task<int> Main(string[] args)
4242
EnableUnqualifiedCall = processor.EnableUnqualifiedCall.Value,
4343
ShowSchemaExamples = !processor.DisableSchemaExamples.Value,
4444
OpenApiSpecVersion = processor.Version.Value,
45+
UseHttpPutForUpdate = processor.UseHttpPutForUpdate.Value,
4546
};
4647

4748
if (processor.IsLocalFile)

0 commit comments

Comments
 (0)