| | | 1 | | |
| | | 2 | | using System.Management.Automation; |
| | | 3 | | using System.Management.Automation.Internal; |
| | | 4 | | using System.Reflection; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | using Kestrun.OpenApi; |
| | | 7 | | using Microsoft.OpenApi; |
| | | 8 | | |
| | | 9 | | internal static class PowerShellAttributes |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Applies PowerShell CmdletMetadataAttribute validations to an OpenAPI schema. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="attr">The CmdletMetadataAttribute to apply.</param> |
| | | 15 | | /// <param name="schema">The OpenAPI schema to modify.</param> |
| | | 16 | | internal static void ApplyPowerShellAttribute(CmdletMetadataAttribute attr, OpenApiSchema schema) |
| | | 17 | | { |
| | 6 | 18 | | _ = attr switch |
| | 6 | 19 | | { |
| | 1 | 20 | | ValidateRangeAttribute a => ApplyValidateRangeAttribute(a, schema), |
| | 6 | 21 | | |
| | 1 | 22 | | ValidateLengthAttribute a => ApplyValidateLengthAttribute(a, schema), |
| | 6 | 23 | | |
| | 1 | 24 | | ValidateSetAttribute a => ApplyValidateSetAttribute(a.ValidValues, schema), |
| | 6 | 25 | | |
| | 1 | 26 | | ValidatePatternAttribute a => ApplyValidatePatternAttribute(a, schema), |
| | 6 | 27 | | |
| | 1 | 28 | | ValidateCountAttribute a => ApplyValidateCountAttribute(a, schema), |
| | 6 | 29 | | |
| | 0 | 30 | | ValidateNotNullOrEmptyAttribute => ApplyNotNullOrEmpty(schema), |
| | 0 | 31 | | ValidateNotNullAttribute => ApplyNotNull(schema), |
| | 1 | 32 | | ValidateNotNullOrWhiteSpaceAttribute => ApplyNotNullOrWhiteSpace(schema), |
| | 0 | 33 | | _ => null |
| | 6 | 34 | | }; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Applies PowerShell validation attributes declared on a property to the specified schema. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="p">The property info to inspect for validation attributes.</param> |
| | | 41 | | /// <param name="s">The OpenAPI schema to apply constraints to.</param> |
| | | 42 | | internal static void ApplyPowerShellAttributes(PropertyInfo p, IOpenApiSchema s) |
| | | 43 | | { |
| | 156 | 44 | | if (s is not OpenApiSchema sc) |
| | | 45 | | { |
| | | 46 | | // constraints only applicable on a concrete schema, not a $ref proxy |
| | 8 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Only pick PowerShell cmdlet metadata / validation attributes; |
| | | 51 | | // no magic string on Type.Name needed. |
| | 308 | 52 | | foreach (var attr in p.GetCustomAttributes<CmdletMetadataAttribute>(inherit: false)) |
| | | 53 | | { |
| | 6 | 54 | | ApplyPowerShellAttribute(attr, sc); |
| | | 55 | | } |
| | 148 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Applies a ValidateRangeAttribute to an OpenApiSchema. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="attr">The ValidateRangeAttribute to apply.</param> |
| | | 62 | | /// <param name="schema">The OpenApiSchema to modify.</param> |
| | | 63 | | /// <returns>Returns always null.</returns> |
| | | 64 | | private static object? ApplyValidateRangeAttribute(ValidateRangeAttribute attr, OpenApiSchema schema) |
| | | 65 | | { |
| | 1 | 66 | | var min = attr.MinRange; |
| | 1 | 67 | | var max = attr.MaxRange; |
| | 1 | 68 | | if (min is not null) |
| | | 69 | | { |
| | 1 | 70 | | schema.Minimum = min.ToString(); |
| | | 71 | | } |
| | 1 | 72 | | if (max is not null) |
| | | 73 | | { |
| | 1 | 74 | | schema.Maximum = max.ToString(); |
| | | 75 | | } |
| | 1 | 76 | | return null; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Applies a ValidateLengthAttribute to an OpenApiSchema. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="attr">The ValidateLengthAttribute to apply.</param> |
| | | 83 | | /// <param name="schema">The OpenApiSchema to modify.</param> |
| | | 84 | | /// <returns>Returns always null.</returns> |
| | | 85 | | private static object? ApplyValidateLengthAttribute(ValidateLengthAttribute attr, OpenApiSchema schema) |
| | | 86 | | { |
| | 1 | 87 | | var minLen = attr.MinLength; |
| | 1 | 88 | | var maxLen = attr.MaxLength; |
| | 1 | 89 | | if (minLen >= 0) |
| | | 90 | | { |
| | 1 | 91 | | schema.MinLength = minLen; |
| | | 92 | | } |
| | 1 | 93 | | if (maxLen >= 0) |
| | | 94 | | { |
| | 1 | 95 | | schema.MaxLength = maxLen; |
| | | 96 | | } |
| | 1 | 97 | | return null; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Applies a ValidateSetAttribute to an OpenApiSchema. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="validValues">The list of valid values to apply.</param> |
| | | 104 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 105 | | /// <returns>Returns always null.</returns> |
| | | 106 | | internal static object? ApplyValidateSetAttribute(IList<string> validValues, OpenApiSchema sc) |
| | | 107 | | { |
| | 1 | 108 | | if (validValues is not null) |
| | | 109 | | { |
| | 1 | 110 | | var list = new List<JsonNode>(); |
| | 6 | 111 | | foreach (var node in validValues.Select(OpenApiJsonNodeFactory.ToNode)) |
| | | 112 | | { |
| | 2 | 113 | | if (node is not null) |
| | | 114 | | { |
| | 2 | 115 | | list.Add(node); |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | 1 | 119 | | if (list.Count > 0) |
| | | 120 | | { |
| | 1 | 121 | | var existing = sc.Enum?.ToList() ?? []; |
| | 1 | 122 | | existing.AddRange(list); |
| | 1 | 123 | | sc.Enum = existing; |
| | | 124 | | } |
| | | 125 | | } |
| | 1 | 126 | | return null; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Applies a ValidatePatternAttribute to an OpenApiSchema. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="attr">The ValidatePatternAttribute to apply.</param> |
| | | 133 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 134 | | /// <returns>Returns always null.</returns> |
| | | 135 | | private static object? ApplyValidatePatternAttribute(ValidatePatternAttribute attr, OpenApiSchema sc) |
| | | 136 | | { |
| | 1 | 137 | | if (string.IsNullOrWhiteSpace(sc.Pattern)) |
| | | 138 | | { |
| | 1 | 139 | | sc.Pattern = attr.RegexPattern; |
| | | 140 | | } |
| | 1 | 141 | | return null; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Applies a ValidateCountAttribute to an OpenApiSchema. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="attr">The ValidateCountAttribute to apply.</param> |
| | | 148 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 149 | | /// <returns>Returns always null.</returns> |
| | | 150 | | private static object? ApplyValidateCountAttribute(ValidateCountAttribute attr, OpenApiSchema sc) |
| | | 151 | | { |
| | 1 | 152 | | if (attr.MinLength >= 0) |
| | | 153 | | { |
| | 1 | 154 | | sc.MinItems = attr.MinLength; |
| | | 155 | | } |
| | | 156 | | |
| | 1 | 157 | | if (attr.MaxLength >= 0) |
| | | 158 | | { |
| | 1 | 159 | | sc.MaxItems = attr.MaxLength; |
| | | 160 | | } |
| | 1 | 161 | | return null; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Applies a ValidateNotNullOrEmptyAttribute to an OpenApiSchema. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 168 | | /// <returns>Returns always null.</returns> |
| | | 169 | | internal static object? ApplyNotNullOrEmpty(OpenApiSchema sc) |
| | | 170 | | { |
| | | 171 | | // string → minLength >= 1 |
| | 0 | 172 | | if (sc.Type == JsonSchemaType.String && (sc.MinLength is null or < 1)) |
| | | 173 | | { |
| | 0 | 174 | | sc.MinLength = 1; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | // array → minItems >= 1 |
| | 0 | 178 | | if (sc.Type == JsonSchemaType.Array && (sc.MinItems is null or < 1)) |
| | | 179 | | { |
| | 0 | 180 | | sc.MinItems = 1; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | return null; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Applies a ValidateNotNullOrWhiteSpaceAttribute to an OpenApiSchema. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 190 | | /// <returns>Returns always null.</returns> |
| | | 191 | | internal static object? ApplyNotNullOrWhiteSpace(OpenApiSchema sc) |
| | | 192 | | { |
| | 1 | 193 | | if (sc.Type == JsonSchemaType.String) |
| | | 194 | | { |
| | 1 | 195 | | if (sc.MinLength is null or < 1) |
| | | 196 | | { |
| | 1 | 197 | | sc.MinLength = 1; |
| | | 198 | | } |
| | 1 | 199 | | if (string.IsNullOrEmpty(sc.Pattern)) |
| | | 200 | | { |
| | | 201 | | // No existing pattern → just require a non-whitespace character |
| | 1 | 202 | | sc.Pattern = @"\S"; |
| | | 203 | | } |
| | | 204 | | } |
| | 1 | 205 | | return null; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Applies a ValidateNotNullAttribute to an OpenApiSchema. |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="schema">The OpenApiSchema to modify.</param> |
| | | 212 | | /// <returns>Returns always null.</returns> |
| | 0 | 213 | | internal static object? ApplyNotNull(OpenApiSchema schema) => ApplyNotNullOrEmpty(schema); |
| | | 214 | | } |