in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/InternalOpenApiGenerator.cs [73:193]
private void AddOperation(
IDictionary<DocumentVariantInfo, OpenApiDocument> specificationDocuments,
IDictionary<DocumentVariantInfo, ReferenceRegistryManager> referenceRegistryManagerMap,
List<GenerationError> operationGenerationErrors,
DocumentVariantInfo documentVariantInfo,
XElement operationElement,
XElement operationConfigElement,
GenerationContext generationContext)
{
var paths = new OpenApiPaths();
foreach (var preprocessingOperationFilter in _preProcessingOperationFilters)
{
var generationErrors = preprocessingOperationFilter.Apply(
paths,
operationElement,
new PreProcessingOperationFilterSettings());
operationGenerationErrors.AddRange(generationErrors);
}
if (!referenceRegistryManagerMap.ContainsKey(documentVariantInfo))
{
referenceRegistryManagerMap[documentVariantInfo] =
new ReferenceRegistryManager(_openApiDocumentGenerationSettings);
}
foreach (var pathToPathItem in paths)
{
var path = pathToPathItem.Key;
var pathItem = pathToPathItem.Value;
foreach (var operationMethodToOperation in pathItem.Operations)
{
var operationMethod = operationMethodToOperation.Key;
var operation = operationMethodToOperation.Value;
var operationFilterSettings = new OperationFilterSettings
{
GenerationContext = generationContext,
ReferenceRegistryManager = referenceRegistryManagerMap[documentVariantInfo],
Path = path,
OperationMethod = operationMethod.ToString(),
RemoveRoslynDuplicateStringFromParamName = _openApiDocumentGenerationSettings
.RemoveRoslynDuplicateStringFromParamName
};
// Apply all the operation-related filters to extract information related to the operation.
// It is important that these are applied before the config filters below
// since the config filters may rely on information generated from operation filters.
foreach (var operationFilter in _operationFilters)
{
var generationErrors = operationFilter.Apply(
operation,
operationElement,
operationFilterSettings);
operationGenerationErrors.AddRange(generationErrors);
}
if (operationConfigElement != null)
{
// Apply the config-related filters to extract information from the config xml
// that can be applied to the operations.
foreach (var configFilter in _operationConfigFilters)
{
var generationErrors = configFilter.Apply(
operation,
operationConfigElement,
new OperationConfigFilterSettings
{
OperationFilterSettings = operationFilterSettings,
OperationFilters = _operationFilters
});
operationGenerationErrors.AddRange(generationErrors);
}
}
// Add the processed operation to the specification document.
if (!specificationDocuments.ContainsKey(documentVariantInfo))
{
specificationDocuments.Add(
documentVariantInfo,
new OpenApiDocument
{
Components = new OpenApiComponents(),
Paths = new OpenApiPaths()
});
}
// Copy operations from local Paths object to the Paths in the specification document.
var documentPaths = specificationDocuments[documentVariantInfo].Paths;
if (!documentPaths.ContainsKey(path))
{
documentPaths.Add(
path,
new OpenApiPathItem
{
Operations =
{
[operationMethod] = operation
}
});
}
else
{
if (documentPaths[path].Operations.ContainsKey(operationMethod))
{
throw new DuplicateOperationException(
path,
operationMethod.ToString(),
documentVariantInfo.Title);
}
documentPaths[path].Operations.Add(operationMethod, operation);
}
}
}
}