| | | 1 | | using Microsoft.OpenApi; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.OpenApi; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extensions for <see cref="OaParameterLocation"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class OaParameterExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary>Convert to Microsoft.OpenApi ParameterLocation.</summary> |
| | | 11 | | /// <param name="location">The OaParameterLocation value.</param> |
| | | 12 | | /// <returns>The corresponding ParameterLocation value.</returns> |
| | | 13 | | public static ParameterLocation ToOpenApi(this OaParameterLocation location) |
| | 0 | 14 | | => location switch |
| | 0 | 15 | | { |
| | 0 | 16 | | OaParameterLocation.Query => ParameterLocation.Query, |
| | 0 | 17 | | OaParameterLocation.Header => ParameterLocation.Header, |
| | 0 | 18 | | OaParameterLocation.Path => ParameterLocation.Path, |
| | 0 | 19 | | OaParameterLocation.Cookie => ParameterLocation.Cookie, |
| | 0 | 20 | | _ => throw new ArgumentOutOfRangeException(nameof(location), location, null) |
| | 0 | 21 | | }; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Convert to Microsoft.OpenApi ParameterStyle. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="style"></param> |
| | | 27 | | /// <returns></returns> |
| | | 28 | | /// <exception cref="ArgumentOutOfRangeException"></exception> |
| | | 29 | | public static ParameterStyle ToOpenApi(this OaParameterStyle style) |
| | 1 | 30 | | => style switch |
| | 1 | 31 | | { |
| | 1 | 32 | | OaParameterStyle.Simple => ParameterStyle.Simple, |
| | 0 | 33 | | OaParameterStyle.Form => ParameterStyle.Form, |
| | 0 | 34 | | OaParameterStyle.Matrix => ParameterStyle.Matrix, |
| | 0 | 35 | | OaParameterStyle.Label => ParameterStyle.Label, |
| | 0 | 36 | | OaParameterStyle.SpaceDelimited => ParameterStyle.SpaceDelimited, |
| | 0 | 37 | | OaParameterStyle.PipeDelimited => ParameterStyle.PipeDelimited, |
| | 0 | 38 | | OaParameterStyle.DeepObject => ParameterStyle.DeepObject, |
| | 0 | 39 | | _ => throw new ArgumentOutOfRangeException(nameof(style), style, null) |
| | 1 | 40 | | }; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Convert to Microsoft.OpenApi ParameterLocation. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="location">The OaParameterLocation value.</param> |
| | | 46 | | /// <returns>The corresponding ParameterLocation value.</returns> |
| | | 47 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the location is not recognized.</exception> |
| | | 48 | | public static ParameterLocation ToParameterLocation(this OaParameterLocation location) |
| | | 49 | | { |
| | 0 | 50 | | return location switch |
| | 0 | 51 | | { |
| | 0 | 52 | | OaParameterLocation.Query => ParameterLocation.Query, |
| | 0 | 53 | | OaParameterLocation.Header => ParameterLocation.Header, |
| | 0 | 54 | | OaParameterLocation.Path => ParameterLocation.Path, |
| | 0 | 55 | | OaParameterLocation.Cookie => ParameterLocation.Cookie, |
| | 0 | 56 | | _ => throw new ArgumentOutOfRangeException(nameof(location), location, null) |
| | 0 | 57 | | }; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Convert to Microsoft.OpenApi ParameterStyle. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="style">The OaParameterStyle value.</param> |
| | | 64 | | /// <returns>The corresponding ParameterStyle value.</returns> |
| | | 65 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the style is not recognized.</exception> |
| | | 66 | | public static ParameterStyle ToParameterStyle(this OaParameterStyle? style) |
| | | 67 | | { |
| | 0 | 68 | | return style switch |
| | 0 | 69 | | { |
| | 0 | 70 | | OaParameterStyle.Simple => ParameterStyle.Simple, |
| | 0 | 71 | | OaParameterStyle.Form => ParameterStyle.Form, |
| | 0 | 72 | | OaParameterStyle.Matrix => ParameterStyle.Matrix, |
| | 0 | 73 | | OaParameterStyle.Label => ParameterStyle.Label, |
| | 0 | 74 | | OaParameterStyle.SpaceDelimited => ParameterStyle.SpaceDelimited, |
| | 0 | 75 | | OaParameterStyle.PipeDelimited => ParameterStyle.PipeDelimited, |
| | 0 | 76 | | OaParameterStyle.DeepObject => ParameterStyle.DeepObject, |
| | 0 | 77 | | _ => throw new ArgumentOutOfRangeException(nameof(style), style, null) |
| | 0 | 78 | | }; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Convert string to Microsoft.OpenApi ParameterLocation. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="location">The string representation of the parameter location.</param> |
| | | 85 | | /// <returns>The corresponding OpenApi ParameterLocation value.</returns> |
| | | 86 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the location is not recognized.</exception> |
| | | 87 | | public static ParameterLocation ToOpenApiParameterLocation(this string location) |
| | | 88 | | { |
| | 0 | 89 | | return location.ToLower() switch |
| | 0 | 90 | | { |
| | 0 | 91 | | "query" => ParameterLocation.Query, |
| | 0 | 92 | | "header" => ParameterLocation.Header, |
| | 0 | 93 | | "path" => ParameterLocation.Path, |
| | 0 | 94 | | "cookie" => ParameterLocation.Cookie, |
| | 0 | 95 | | _ => throw new ArgumentOutOfRangeException(nameof(location), location, null) |
| | 0 | 96 | | }; |
| | | 97 | | } |
| | | 98 | | } |