-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAPI descriptions for schemas from XML documentation.
- Loading branch information
1 parent
1b624c5
commit 12237af
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Martin Costello, 2016. All rights reserved. | ||
// Licensed under the MIT license. See the LICENSE file in the project root for full license information. | ||
|
||
using System.Xml; | ||
using System.Xml.XPath; | ||
using Microsoft.AspNetCore.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace MartinCostello.Api.OpenApi; | ||
|
||
/// <summary> | ||
/// A class that adds descriptions to the schemas of OpenAPI documents. This class cannot be inherited. | ||
/// </summary> | ||
public sealed class AddSchemaDescriptionsTransformer : IOpenApiSchemaTransformer | ||
{ | ||
/// <inheritdoc/> | ||
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
if (schema.Description != null) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
var thisAssembly = GetType().Assembly; | ||
|
||
if (context.JsonTypeInfo.Type.Assembly == thisAssembly) | ||
{ | ||
var navigator = CreateNavigator(); | ||
var description = navigator.SelectSingleNode($"/doc/members/member[@name='T:{context.JsonTypeInfo.Type.FullName}']/summary"); | ||
|
||
if (!string.IsNullOrEmpty(description?.InnerXml)) | ||
{ | ||
schema.Description = description.Value.Trim(); | ||
} | ||
} | ||
else if (context.JsonPropertyInfo?.DeclaringType.Assembly == thisAssembly) | ||
{ | ||
var navigator = CreateNavigator(); | ||
|
||
var typeName = context.JsonPropertyInfo.DeclaringType.FullName; | ||
var propertyName = $"{char.ToUpperInvariant(context.JsonPropertyInfo.Name[0])}{context.JsonPropertyInfo.Name[1..]}"; | ||
|
||
var description = navigator.SelectSingleNode( | ||
$"/doc/members/member[@name='P:{typeName}{Type.Delimiter}{propertyName}']/summary"); | ||
|
||
if (!string.IsNullOrEmpty(description?.InnerXml)) | ||
{ | ||
schema.Description = description.Value.Trim(); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private static XPathNavigator CreateNavigator() | ||
{ | ||
var path = Path.Combine(AppContext.BaseDirectory, "API.xml"); | ||
using var reader = XmlReader.Create(path); | ||
return new XPathDocument(reader).CreateNavigator(); | ||
} | ||
} |