| | | 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 | | { |
| | 0 | 18 | | _ = attr switch |
| | 0 | 19 | | { |
| | 0 | 20 | | ValidateRangeAttribute a => ApplyValidateRangeAttribute(a, schema), |
| | 0 | 21 | | |
| | 0 | 22 | | ValidateLengthAttribute a => ApplyValidateLengthAttribute(a, schema), |
| | 0 | 23 | | |
| | 0 | 24 | | ValidateSetAttribute a => ApplyValidateSetAttribute(a, schema), |
| | 0 | 25 | | |
| | 0 | 26 | | ValidatePatternAttribute a => ApplyValidatePatternAttribute(a, schema), |
| | 0 | 27 | | |
| | 0 | 28 | | ValidateCountAttribute a => ApplyValidateCountAttribute(a, schema), |
| | 0 | 29 | | |
| | 0 | 30 | | ValidateNotNullOrEmptyAttribute => ApplyNotNullOrEmpty(schema), |
| | 0 | 31 | | ValidateNotNullAttribute => ApplyNotNull(schema), |
| | 0 | 32 | | ValidateNotNullOrWhiteSpaceAttribute => ApplyNotNullOrWhiteSpace(schema), |
| | 0 | 33 | | _ => null |
| | 0 | 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 | | { |
| | 27 | 44 | | if (s is not OpenApiSchema sc) |
| | | 45 | | { |
| | | 46 | | // constraints only applicable on a concrete schema, not a $ref proxy |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Only pick PowerShell cmdlet metadata / validation attributes; |
| | | 51 | | // no magic string on Type.Name needed. |
| | 54 | 52 | | foreach (var attr in p.GetCustomAttributes<CmdletMetadataAttribute>(inherit: false)) |
| | | 53 | | { |
| | 0 | 54 | | ApplyPowerShellAttribute(attr, sc); |
| | | 55 | | } |
| | 27 | 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 | | { |
| | 0 | 66 | | var min = attr.MinRange; |
| | 0 | 67 | | var max = attr.MaxRange; |
| | 0 | 68 | | if (min is not null) |
| | | 69 | | { |
| | 0 | 70 | | schema.Minimum = min.ToString(); |
| | | 71 | | } |
| | 0 | 72 | | if (max is not null) |
| | | 73 | | { |
| | 0 | 74 | | schema.Maximum = max.ToString(); |
| | | 75 | | } |
| | 0 | 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 | | { |
| | 0 | 87 | | var minLen = attr.MinLength; |
| | 0 | 88 | | var maxLen = attr.MaxLength; |
| | 0 | 89 | | if (minLen >= 0) |
| | | 90 | | { |
| | 0 | 91 | | schema.MinLength = minLen; |
| | | 92 | | } |
| | 0 | 93 | | if (maxLen >= 0) |
| | | 94 | | { |
| | 0 | 95 | | schema.MaxLength = maxLen; |
| | | 96 | | } |
| | 0 | 97 | | return null; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Applies a ValidateSetAttribute to an OpenApiSchema. |
| | | 102 | | /// </summary> |
| | | 103 | | /// <param name="attr">The ValidateSetAttribute to apply.</param> |
| | | 104 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 105 | | /// <returns>Returns always null.</returns> |
| | | 106 | | private static object? ApplyValidateSetAttribute(ValidateSetAttribute attr, OpenApiSchema sc) |
| | | 107 | | { |
| | 0 | 108 | | var vals = attr.ValidValues; |
| | 0 | 109 | | if (vals is not null) |
| | | 110 | | { |
| | 0 | 111 | | var list = new List<JsonNode>(); |
| | 0 | 112 | | foreach (var node in vals.Select(OpenApiDocDescriptor.ToNode)) |
| | | 113 | | { |
| | 0 | 114 | | if (node is not null) |
| | | 115 | | { |
| | 0 | 116 | | list.Add(node); |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | if (list.Count > 0) |
| | | 121 | | { |
| | 0 | 122 | | var existing = sc.Enum?.ToList() ?? []; |
| | 0 | 123 | | existing.AddRange(list); |
| | 0 | 124 | | sc.Enum = existing; |
| | | 125 | | } |
| | | 126 | | } |
| | 0 | 127 | | return null; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Applies a ValidatePatternAttribute to an OpenApiSchema. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <param name="attr">The ValidatePatternAttribute to apply.</param> |
| | | 134 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 135 | | /// <returns>Returns always null.</returns> |
| | | 136 | | private static object? ApplyValidatePatternAttribute(ValidatePatternAttribute attr, OpenApiSchema sc) |
| | | 137 | | { |
| | 0 | 138 | | if (string.IsNullOrWhiteSpace(sc.Pattern)) |
| | | 139 | | { |
| | 0 | 140 | | sc.Pattern = attr.RegexPattern; |
| | | 141 | | } |
| | 0 | 142 | | return null; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Applies a ValidateCountAttribute to an OpenApiSchema. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="attr">The ValidateCountAttribute to apply.</param> |
| | | 149 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 150 | | /// <returns>Returns always null.</returns> |
| | | 151 | | private static object? ApplyValidateCountAttribute(ValidateCountAttribute attr, OpenApiSchema sc) |
| | | 152 | | { |
| | 0 | 153 | | if (attr.MinLength >= 0) |
| | | 154 | | { |
| | 0 | 155 | | sc.MinItems = attr.MinLength; |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (attr.MaxLength >= 0) |
| | | 159 | | { |
| | 0 | 160 | | sc.MaxItems = attr.MaxLength; |
| | | 161 | | } |
| | 0 | 162 | | return null; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Applies a ValidateNotNullOrEmptyAttribute to an OpenApiSchema. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 169 | | /// <returns>Returns always null.</returns> |
| | | 170 | | private static object? ApplyNotNullOrEmpty(OpenApiSchema sc) |
| | | 171 | | { |
| | | 172 | | // string → minLength >= 1 |
| | 0 | 173 | | if (sc.Type == JsonSchemaType.String && (sc.MinLength is null or < 1)) |
| | | 174 | | { |
| | 0 | 175 | | sc.MinLength = 1; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | // array → minItems >= 1 |
| | 0 | 179 | | if (sc.Type == JsonSchemaType.Array && (sc.MinItems is null or < 1)) |
| | | 180 | | { |
| | 0 | 181 | | sc.MinItems = 1; |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | return null; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Applies a ValidateNotNullOrWhiteSpaceAttribute to an OpenApiSchema. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="sc">The OpenApiSchema to modify.</param> |
| | | 191 | | /// <returns>Returns always null.</returns> |
| | | 192 | | private static object? ApplyNotNullOrWhiteSpace(OpenApiSchema sc) |
| | | 193 | | { |
| | 0 | 194 | | if (sc.Type == JsonSchemaType.String) |
| | | 195 | | { |
| | 0 | 196 | | if (sc.MinLength is null or < 1) |
| | | 197 | | { |
| | 0 | 198 | | sc.MinLength = 1; |
| | | 199 | | } |
| | 0 | 200 | | if (string.IsNullOrEmpty(sc.Pattern)) |
| | | 201 | | { |
| | | 202 | | // No existing pattern → just require a non-whitespace character |
| | 0 | 203 | | sc.Pattern = @"\S"; |
| | | 204 | | } |
| | | 205 | | } |
| | 0 | 206 | | return null; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Applies a ValidateNotNullAttribute to an OpenApiSchema. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <param name="schema">The OpenApiSchema to modify.</param> |
| | | 213 | | /// <returns>Returns always null.</returns> |
| | 0 | 214 | | private static object? ApplyNotNull(OpenApiSchema schema) => ApplyNotNullOrEmpty(schema); |
| | | 215 | | } |