in src/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration/InternalOpenApiGenerator.cs [460:617]
private IList<OperationGenerationDiagnostic> GenerateSpecificationDocuments(
GenerationContext generationContext,
IList<XElement> operationElements,
XElement operationConfigElement,
string documentVariantElementName,
out IDictionary<DocumentVariantInfo, OpenApiDocument> specificationDocuments)
{
specificationDocuments = new Dictionary<DocumentVariantInfo, OpenApiDocument>();
var operationGenerationResults = new List<OperationGenerationDiagnostic>();
var referenceRegistryManagerMap = new Dictionary<DocumentVariantInfo, ReferenceRegistryManager>();
foreach (var operationElement in operationElements)
{
string url;
OperationType operationMethod;
try
{
url = OperationHandler.GetUrl(operationElement);
}
catch (InvalidUrlException e)
{
operationGenerationResults.Add(
new OperationGenerationDiagnostic
{
Errors =
{
new GenerationError
{
ExceptionType = e.GetType().Name,
Message = e.Message
}
},
OperationMethod = SpecificationGenerationMessages.OperationMethodNotParsedGivenUrlIsInvalid,
Path = e.Url
});
continue;
}
try
{
operationMethod = OperationHandler.GetOperationMethod(url, operationElement);
}
catch (InvalidVerbException e)
{
operationGenerationResults.Add(
new OperationGenerationDiagnostic
{
Errors =
{
new GenerationError
{
ExceptionType = e.GetType().Name,
Message = e.Message
}
},
OperationMethod = e.Verb,
Path = url
});
continue;
}
var operationGenerationErrors = new List<GenerationError>();
try
{
AddOperation(
specificationDocuments,
referenceRegistryManagerMap,
operationGenerationErrors,
DocumentVariantInfo.Default,
operationElement,
operationConfigElement,
generationContext);
}
catch (Exception e)
{
operationGenerationErrors.Add(
new GenerationError
{
ExceptionType = e.GetType().Name,
Message = e.Message
});
}
var customElements = operationElement.Descendants(documentVariantElementName);
foreach (var customElement in customElements)
{
try
{
var documentVariantInfo = new DocumentVariantInfo
{
Categorizer = customElement.Name.LocalName.Trim(),
Title = customElement.Value.Trim()
};
AddOperation(
specificationDocuments,
referenceRegistryManagerMap,
operationGenerationErrors,
documentVariantInfo,
operationElement,
operationConfigElement,
generationContext);
}
catch (Exception e)
{
operationGenerationErrors.Add(
new GenerationError
{
ExceptionType = e.GetType().Name,
Message = e.Message
});
}
}
var operationGenerationResult = new OperationGenerationDiagnostic
{
OperationMethod = operationMethod.ToString(),
Path = url
};
if (operationGenerationErrors.Any())
{
foreach (var error in operationGenerationErrors)
{
operationGenerationResult.Errors.Add(new GenerationError(error));
}
}
operationGenerationResults.Add(operationGenerationResult);
}
foreach (var documentVariantInfo in generationContext.VariantSchemaReferenceMap.Keys)
{
var references = generationContext.VariantSchemaReferenceMap[documentVariantInfo].ToDictionary(
k => k.Key,
k => k.Value);
if (specificationDocuments.ContainsKey(documentVariantInfo))
{
references.CopyInto(specificationDocuments[documentVariantInfo].Components.Schemas);
}
}
foreach (var documentVariantInfo in specificationDocuments.Keys)
{
referenceRegistryManagerMap[documentVariantInfo]
.SecuritySchemeReferenceRegistry.References.CopyInto(
specificationDocuments[documentVariantInfo].Components.SecuritySchemes);
}
return operationGenerationResults;
}