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 pathParamToParameterFilter.cs
194 lines (164 loc) · 8.07 KB
/
ParamToParameterFilter.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Extensions;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Models;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Models.KnownStrings;
using Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.ReferenceRegistries;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.OperationFilters
{
/// <summary>
/// Parses the value of param tag in xml documentation and apply that as parameter in operation.
/// </summary>
public class ParamToParameterFilter : IOperationFilter
{
/// <summary>
/// Fetches the value of "param" tags from xml documentation and populates operation's parameters values.
/// </summary>
/// <param name="operation">The operation 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>
/// <remarks>
/// Care should be taken to not overwrite the existing value in Operation if already present.
/// This guarantees the predictable behavior that the first tag in the XML will be respected.
/// It also guarantees that common annotations in the config file do not overwrite the
/// annotations in the main documentation.
/// </remarks>
public IList<GenerationError> Apply(
OpenApiOperation operation,
XElement element,
OperationFilterSettings settings)
{
var generationErrors = new List<GenerationError>();
try
{
var paramElements = element.Elements()
.Where(
p => p.Name == KnownXmlStrings.Param)
.ToList();
// Query paramElements again to get all the parameter elements that have "in" attribute.
// This will include those whose "in" attribute were just populated in PopulateInAttributeFilter, but exclude
// those that have "in" attribute being "body" since they will be handled as a request body.
var paramElementsWithIn = paramElements.Where(
p =>
KnownXmlStrings.InValuesTranslatableToParameter.Contains(
p.Attribute(KnownXmlStrings.In)?.Value))
.ToList();
var generationContext = settings.GenerationContext;
foreach (var paramElement in paramElementsWithIn)
{
var inValue = paramElement.Attribute(KnownXmlStrings.In)?.Value.Trim();
var name = paramElement.Attribute(KnownXmlStrings.Name)?.Value.Trim();
var mediaType = paramElement.Attribute(KnownXmlStrings.Type)?.Value ?? "";
if (settings.RemoveRoslynDuplicateStringFromParamName)
{
name = name.RemoveRoslynDuplicateString();
}
if (inValue == KnownXmlStrings.Path &&
!settings.Path.Contains($"{{{name}}}", StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
var isRequired = paramElement.Attribute(KnownXmlStrings.Required)?.Value.Trim();
var cref = paramElement.Attribute(KnownXmlStrings.Cref)?.Value.Trim();
var description = paramElement.GetDescriptionTextFromLastTextNode();
var allListedTypes = paramElement.GetListedTypes();
OpenApiSchema schema = new OpenApiSchema();
if (!allListedTypes.Any())
{
// Set default schema as string.
schema = new OpenApiSchema()
{
Type = "string"
};
}
var crefKey = allListedTypes.ToCrefKey();
if (generationContext.CrefToSchemaMap.ContainsKey(crefKey))
{
var schemaInfo = generationContext.CrefToSchemaMap[crefKey];
if (schemaInfo.Error != null)
{
generationErrors.Add(schemaInfo.Error);
return generationErrors;
}
schemaInfo.Schema.CopyInto(schema);
}
var parameterLocation = GetParameterKind(inValue);
var examples = paramElement.ToOpenApiExamples(
generationContext.CrefToFieldValueMap,
generationErrors);
var schemaReferenceDefaultVariant = generationContext
.VariantSchemaReferenceMap[DocumentVariantInfo.Default];
if (examples.Any())
{
var firstExample = examples.First().Value?.Value;
if (firstExample != null)
{
if (schema.Reference != null)
{
if (schemaReferenceDefaultVariant.ContainsKey(schema.Reference.Id))
{
schemaReferenceDefaultVariant[schema.Reference.Id].Example = firstExample;
}
}
else
{
schema.Example = firstExample;
}
}
}
var openApiParameter = new OpenApiParameter
{
Name = name,
In = parameterLocation,
Description = description,
Required = parameterLocation == ParameterLocation.Path || Convert.ToBoolean(isRequired),
Examples = examples.Any() ? examples : null
};
// If media type is specified add parameter as content for complex serialization
if (!string.IsNullOrEmpty(mediaType)) {
openApiParameter.Content = new Dictionary<string, OpenApiMediaType>
{
[mediaType] = new OpenApiMediaType { Schema = schema }
};
}
else {
openApiParameter.Schema = schema;
}
operation.Parameters.Add(openApiParameter);
}
}
catch(Exception ex)
{
generationErrors.Add(
new GenerationError
{
Message = ex.Message,
ExceptionType = ex.GetType().Name
});
}
return generationErrors;
}
private static ParameterLocation GetParameterKind(string parameterKind)
{
switch (parameterKind)
{
case KnownXmlStrings.Header:
return ParameterLocation.Header;
case KnownXmlStrings.Query:
return ParameterLocation.Query;
default:
return ParameterLocation.Path;
}
}
}
}