|
5 | 5 | using System.Linq;
|
6 | 6 | using System.Reflection;
|
7 | 7 | using Microsoft.OpenApi.Attributes;
|
| 8 | +using Microsoft.OpenApi.Models; |
8 | 9 |
|
9 | 10 | namespace Microsoft.OpenApi.Extensions
|
10 | 11 | {
|
@@ -42,5 +43,107 @@ public static string GetDisplayName(this Enum enumValue)
|
42 | 43 | var attribute = enumValue.GetAttributeOfType<DisplayAttribute>();
|
43 | 44 | return attribute == null ? enumValue.ToString() : attribute.Name;
|
44 | 45 | }
|
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets the enum display name for <see typeparamref="T"/> without the use of reflection. |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="T">The type of the enum value.</typeparam> |
| 51 | + /// <param name="enumValue">The enum value.</param> |
| 52 | + /// <returns>The display string to use.</returns> |
| 53 | + public static string GetDisplayName<T>(this T enumValue) where T : Enum |
| 54 | + { |
| 55 | + return enumValue switch |
| 56 | + { |
| 57 | + ParameterStyle parameterStyle => parameterStyle.GetDisplayName(), |
| 58 | + ParameterLocation parameterLocation => parameterLocation.GetDisplayName(), |
| 59 | + ReferenceType referenceType => referenceType.GetDisplayName(), |
| 60 | + OperationType operationType => operationType.GetDisplayName(), |
| 61 | + SecuritySchemeType securitySchemeType => securitySchemeType.GetDisplayName(), |
| 62 | + _ => enumValue.ToString() |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Gets the enum display for name <see cref="ParameterStyle" /> without the use of reflection. |
| 68 | + /// </summary> |
| 69 | + /// <param name="parameterStyle">The enum value.</param> |
| 70 | + /// <returns>The display string to use.</returns> |
| 71 | + internal static string GetDisplayName(this ParameterStyle parameterStyle) => parameterStyle switch |
| 72 | + { |
| 73 | + ParameterStyle.Matrix => "matrix", |
| 74 | + ParameterStyle.Label => "label", |
| 75 | + ParameterStyle.Form => "form", |
| 76 | + ParameterStyle.Simple => "simple", |
| 77 | + ParameterStyle.SpaceDelimited => "spaceDelimited", |
| 78 | + ParameterStyle.PipeDelimited => "pipeDelimited", |
| 79 | + ParameterStyle.DeepObject => "deepObject", |
| 80 | + _ => parameterStyle.ToString() |
| 81 | + }; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets the enum display for name <see cref="ParameterLocation" /> without the use of reflection. |
| 85 | + /// </summary> |
| 86 | + /// <param name="parameterLocation">The enum value.</param> |
| 87 | + /// <returns>The display string to use.</returns> |
| 88 | + internal static string GetDisplayName(this ParameterLocation parameterLocation) => parameterLocation switch |
| 89 | + { |
| 90 | + ParameterLocation.Query => "query", |
| 91 | + ParameterLocation.Header => "header", |
| 92 | + ParameterLocation.Path => "path", |
| 93 | + ParameterLocation.Cookie => "cookie", |
| 94 | + _ => parameterLocation.ToString() |
| 95 | + }; |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Gets the enum display for name <see cref="ReferenceType" /> without the use of reflection. |
| 99 | + /// </summary> |
| 100 | + /// <param name="referenceType">The enum value.</param> |
| 101 | + /// <returns>The display string to use.</returns> |
| 102 | + internal static string GetDisplayName(this ReferenceType referenceType) => referenceType switch |
| 103 | + { |
| 104 | + ReferenceType.Schema => "schemas", |
| 105 | + ReferenceType.Response => "responses", |
| 106 | + ReferenceType.Parameter => "parameters", |
| 107 | + ReferenceType.Example => "examples", |
| 108 | + ReferenceType.RequestBody => "requestBodies", |
| 109 | + ReferenceType.Header => "headers", |
| 110 | + ReferenceType.SecurityScheme => "securitySchemes", |
| 111 | + ReferenceType.Link => "links", |
| 112 | + ReferenceType.Callback => "callbacks", |
| 113 | + ReferenceType.Tag => "tags", |
| 114 | + _ => referenceType.ToString() |
| 115 | + }; |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Gets the enum display for name <see cref="OperationType" /> without the use of reflection. |
| 119 | + /// </summary> |
| 120 | + /// <param name="operationType">The enum value.</param> |
| 121 | + /// <returns>The display string to use.</returns> |
| 122 | + internal static string GetDisplayName(this OperationType operationType) => operationType switch |
| 123 | + { |
| 124 | + OperationType.Get => "get", |
| 125 | + OperationType.Put => "put", |
| 126 | + OperationType.Post => "post", |
| 127 | + OperationType.Delete => "delete", |
| 128 | + OperationType.Options => "options", |
| 129 | + OperationType.Head => "head", |
| 130 | + OperationType.Patch => "patch", |
| 131 | + OperationType.Trace => "trace", |
| 132 | + _ => operationType.ToString() |
| 133 | + }; |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Gets the enum display for name <see cref="SecuritySchemeType" /> without the use of reflection. |
| 137 | + /// </summary> |
| 138 | + /// <param name="securitySchemeType">The enum value.</param> |
| 139 | + /// <returns>The display string to use.</returns> |
| 140 | + internal static string GetDisplayName(this SecuritySchemeType securitySchemeType) => securitySchemeType switch |
| 141 | + { |
| 142 | + SecuritySchemeType.ApiKey => "apiKey", |
| 143 | + SecuritySchemeType.Http => "http", |
| 144 | + SecuritySchemeType.OAuth2 => "oauth2", |
| 145 | + SecuritySchemeType.OpenIdConnect => "openIdConnect", |
| 146 | + _ => securitySchemeType.ToString() |
| 147 | + }; |
45 | 148 | }
|
46 | 149 | }
|
0 commit comments