in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/OperationFilters/ParamToParameterFilter.cs [38:166]
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();
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),
Schema = schema,
Examples = examples.Any() ? examples : null
};
operation.Parameters.Add(openApiParameter);
}
}
catch(Exception ex)
{
generationErrors.Add(
new GenerationError
{
Message = ex.Message,
ExceptionType = ex.GetType().Name
});
}
return generationErrors;
}