-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added MethodNamingConvention support.
- Loading branch information
Showing
17 changed files
with
235 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/libs/OpenApiGenerator.Core/Naming/Methods/IMethodNameGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace OpenApiGenerator.Core.Naming.Methods; | ||
|
||
public interface IMethodNameGenerator | ||
{ | ||
public string? TryGenerate(OpenApiOperation operation, string path, OperationType operationType); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/libs/OpenApiGenerator.Core/Naming/Methods/MethodAndPathGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.OpenApi.Models; | ||
using OpenApiGenerator.Core.Extensions; | ||
|
||
namespace OpenApiGenerator.Core.Naming.Methods; | ||
|
||
public class MethodAndPathGenerator : IMethodNameGenerator | ||
{ | ||
public string TryGenerate(OpenApiOperation operation, string path, OperationType operationType) | ||
{ | ||
path = path ?? throw new ArgumentNullException(nameof(path)); | ||
|
||
var prefix = operationType switch | ||
{ | ||
OperationType.Get => "get", | ||
OperationType.Post => "create", | ||
OperationType.Put => "put", | ||
OperationType.Delete => "delete", | ||
OperationType.Patch => "edit", | ||
OperationType.Head => "head", | ||
OperationType.Options => "options", | ||
OperationType.Trace => "trace", | ||
_ => throw new NotSupportedException($"OperationType {operationType} is not supported."), | ||
}; | ||
|
||
return $"{prefix}{path.TrimStart('/').ToPropertyName().UseWordSeparator('/')}"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/libs/OpenApiGenerator.Core/Naming/Methods/MethodNamingConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace OpenApiGenerator.Core.Naming.Methods; | ||
|
||
public enum MethodNamingConvention | ||
{ | ||
SimpleOperationId, | ||
MethodAndPath, | ||
OperationIdWithDots, | ||
} | ||
|
||
public static class MethodNamingConventionExtensions | ||
{ | ||
public static IMethodNameGenerator GetGenerator(this MethodNamingConvention operation) | ||
{ | ||
return operation switch | ||
{ | ||
MethodNamingConvention.SimpleOperationId => new SimpleOperationIdGenerator(), | ||
MethodNamingConvention.OperationIdWithDots => new OperationIdWithDotsGenerator(), | ||
MethodNamingConvention.MethodAndPath => new MethodAndPathGenerator(), | ||
_ => throw new ArgumentOutOfRangeException(nameof(operation), operation, null), | ||
}; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/libs/OpenApiGenerator.Core/Naming/Methods/OperationIdWithDotsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.OpenApi.Models; | ||
using OpenApiGenerator.Core.Extensions; | ||
|
||
namespace OpenApiGenerator.Core.Naming.Methods; | ||
|
||
public class OperationIdWithDotsGenerator : IMethodNameGenerator | ||
{ | ||
public string? TryGenerate(OpenApiOperation operation, string path, OperationType operationType) | ||
{ | ||
operation = operation ?? throw new ArgumentNullException(nameof(operation)); | ||
|
||
if (operation.OperationId is null || | ||
!operation.OperationId.Contains('.')) | ||
{ | ||
return null; | ||
} | ||
|
||
return string.Concat(operation.OperationId | ||
.Split('.') | ||
.Reverse() | ||
.Select(x => x.ToPropertyName())); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/libs/OpenApiGenerator.Core/Naming/Methods/SimpleOperationIdGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Microsoft.OpenApi.Models; | ||
using OpenApiGenerator.Core.Extensions; | ||
|
||
namespace OpenApiGenerator.Core.Naming.Methods; | ||
|
||
public class SimpleOperationIdGenerator : IMethodNameGenerator | ||
{ | ||
public string? TryGenerate(OpenApiOperation operation, string path, OperationType operationType) | ||
{ | ||
operation = operation ?? throw new ArgumentNullException(nameof(operation)); | ||
|
||
return operation.OperationId?.ToPropertyName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.