-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for QueryResult in Swagger
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Source/DotNET/Applications/Queries/QueryMethodExtensions.cs
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,27 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using Cratis.Reflection; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Cratis.Applications.Queries; | ||
|
||
/// <summary> | ||
/// Extension methods for methods representing commands. | ||
/// </summary> | ||
public static class QueryMethodExtensions | ||
{ | ||
/// <summary> | ||
/// Check if a method is a command. | ||
/// </summary> | ||
/// <param name="methodInfo">The <see cref="MethodInfo"/> to check.</param> | ||
/// <returns>True if it is a command, false if not.</returns> | ||
public static bool IsQuery(this MethodInfo methodInfo) => | ||
methodInfo.HasAttribute<HttpGetAttribute>() && | ||
methodInfo.ReturnType != typeof(void) && | ||
methodInfo.ReturnType != typeof(Task) && | ||
methodInfo.ReturnType != typeof(ValueTask) && | ||
!methodInfo.HasAttribute<AspNetResultAttribute>() && | ||
(!methodInfo.DeclaringType?.HasAttribute<AspNetResultAttribute>() ?? false); | ||
} |
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,43 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Cratis.Applications.Queries; | ||
using Cratis.Concepts; | ||
using Cratis.Reflection; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Cratis.Applications.Swagger; | ||
|
||
/// <summary> | ||
/// Represents an implementation of <see cref="IOperationFilter"/> that adds the command result to the operation for command methods. | ||
/// </summary> | ||
/// <param name="schemaGenerator">The <see cref="ISchemaGenerator"/> to use.</param> | ||
public class QueryResultOperationFilter(ISchemaGenerator schemaGenerator) : IOperationFilter | ||
{ | ||
/// <inheritdoc/> | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
if (!context.MethodInfo.IsQuery()) return; | ||
|
||
var returnType = context.MethodInfo.GetActualReturnType(); | ||
|
||
if (returnType.IsConcept()) | ||
{ | ||
returnType = returnType.GetConceptValueType(); | ||
} | ||
|
||
var queryResultType = typeof(QueryResult<>).MakeGenericType(returnType); | ||
|
||
var schema = schemaGenerator.GenerateSchema(queryResultType, context.SchemaRepository); | ||
var response = operation.Responses.First().Value; | ||
if (response.Content.ContainsKey("application/json")) | ||
{ | ||
operation.Responses.First().Value.Content["application/json"].Schema = schema; | ||
} | ||
else | ||
{ | ||
response.Content.Add(new("application/json", new() { Schema = schema })); | ||
} | ||
} | ||
} |