|
| 1 | +// Copyright (c) Martin Costello, 2016. All rights reserved. |
| 2 | +// Licensed under the MIT license. See the LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Xml; |
| 5 | +using System.Xml.XPath; |
| 6 | +using Microsoft.AspNetCore.OpenApi; |
| 7 | +using Microsoft.OpenApi.Models; |
| 8 | + |
| 9 | +namespace MartinCostello.Api.OpenApi; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// A class that adds descriptions to the schemas of OpenAPI documents. This class cannot be inherited. |
| 13 | +/// </summary> |
| 14 | +public sealed class AddSchemaDescriptionsTransformer : IOpenApiSchemaTransformer |
| 15 | +{ |
| 16 | + /// <inheritdoc/> |
| 17 | + public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken) |
| 18 | + { |
| 19 | + if (schema.Description != null) |
| 20 | + { |
| 21 | + return Task.CompletedTask; |
| 22 | + } |
| 23 | + |
| 24 | + var thisAssembly = GetType().Assembly; |
| 25 | + |
| 26 | + if (context.JsonTypeInfo.Type.Assembly == thisAssembly) |
| 27 | + { |
| 28 | + var navigator = CreateNavigator(); |
| 29 | + var description = navigator.SelectSingleNode($"/doc/members/member[@name='T:{context.JsonTypeInfo.Type.FullName}']/summary"); |
| 30 | + |
| 31 | + if (!string.IsNullOrEmpty(description?.InnerXml)) |
| 32 | + { |
| 33 | + schema.Description = description.Value.Trim(); |
| 34 | + } |
| 35 | + } |
| 36 | + else if (context.JsonPropertyInfo?.DeclaringType.Assembly == thisAssembly) |
| 37 | + { |
| 38 | + var navigator = CreateNavigator(); |
| 39 | + |
| 40 | + var typeName = context.JsonPropertyInfo.DeclaringType.FullName; |
| 41 | + var propertyName = $"{char.ToUpperInvariant(context.JsonPropertyInfo.Name[0])}{context.JsonPropertyInfo.Name[1..]}"; |
| 42 | + |
| 43 | + var description = navigator.SelectSingleNode( |
| 44 | + $"/doc/members/member[@name='P:{typeName}{Type.Delimiter}{propertyName}']/summary"); |
| 45 | + |
| 46 | + if (!string.IsNullOrEmpty(description?.InnerXml)) |
| 47 | + { |
| 48 | + schema.Description = description.Value.Trim(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return Task.CompletedTask; |
| 53 | + } |
| 54 | + |
| 55 | + private static XPathNavigator CreateNavigator() |
| 56 | + { |
| 57 | + var path = Path.Combine(AppContext.BaseDirectory, "API.xml"); |
| 58 | + using var reader = XmlReader.Create(path); |
| 59 | + return new XPathDocument(reader).CreateNavigator(); |
| 60 | + } |
| 61 | +} |
0 commit comments