This repository was archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathCreateOperationMetaFilter.cs
81 lines (75 loc) · 3.54 KB
/
CreateOperationMetaFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Models;
using Microsoft.OpenApi.Models;
namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.PreprocessingOperationFilters
{
/// <summary>
/// Filter to initialize OpenApi operation based on the annotation XML element.
/// It does not do the creation itself but forwards the call to the real generator filters.
/// The first filter which is applicable is executed in order to generate the operation.
/// </summary>
public class CreateOperationMetaFilter : IPreProcessingOperationFilter
{
private List<ICreateOperationPreProcessingOperationFilter> createOperationFilters;
/// <summary>
/// Initializes a new instance of the <see cref="CreateOperationMetaFilter"/>.
/// </summary>
/// <remarks>
/// Using this constructor, the following filter list is used:
/// If the XML element contains 'operationId' tag, it is used as unique identifier
/// of the operation. Otherwise, the id is generated using the path of the operation.
/// In the latter case, multiple operation could be generated, if the path has optional
/// parameters.
/// </remarks>
public CreateOperationMetaFilter() :
this(new List<ICreateOperationPreProcessingOperationFilter> {
new UsePredefinedOperationIdFilter(), new BranchOptionalPathParametersFilter()})
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CreateOperationMetaFilter"/>.
/// </summary>
/// <param name="createOperationFilters">List of generator filters.</param>
internal CreateOperationMetaFilter(
List<ICreateOperationPreProcessingOperationFilter> createOperationFilters)
{
this.createOperationFilters = createOperationFilters;
}
/// <summary>
/// Initializes the OpenApi operation based on the annotation XML element.
/// It applies the first applicable filter to create operations.
/// </summary>
/// <param name="paths">The paths to be updated.</param>
/// <param name="element">The xml element representing an operation in the annotation xml.</param>
/// <param name="settings">The operation filter settings.</param>
/// <returns>The list of generation errors, if any produced when processing the filter.</returns>
public IList<GenerationError> Apply(
OpenApiPaths paths,
XElement element,
PreProcessingOperationFilterSettings settings)
{
foreach (var filter in this.createOperationFilters)
{
if (filter.IsApplicable(element))
{
return filter.Apply(paths, element, settings);
}
}
// If none of the filters could be applied --> error
return new List<GenerationError>
{
new GenerationError
{
Message = "Failed to apply any operation creation filter.",
ExceptionType = nameof(InvalidOperationException)
}
};
}
}
}