diff --git a/src/lib/TypeExtensions/OpenApiDocumentExtensions.cs b/src/lib/TypeExtensions/OpenApiDocumentExtensions.cs index 5095df9..7ebd89d 100644 --- a/src/lib/TypeExtensions/OpenApiDocumentExtensions.cs +++ b/src/lib/TypeExtensions/OpenApiDocumentExtensions.cs @@ -8,6 +8,9 @@ namespace Microsoft.OpenApi.ApiManifest.TypeExtensions { public static class OpenApiDocumentExtensions { + private const string DefaultPublisherName = "publisher-name"; + private const string DefaultPublisherEmail = "publisher-email@example.com"; + /// /// Converts an to an . /// @@ -16,27 +19,24 @@ public static class OpenApiDocumentExtensions /// The name of the application. /// The name of the API dependency. /// An . - internal static ApiManifestDocument ToApiManifest(this OpenApiDocument document, string? apiDescriptionUrl, string applicationName, string? apiDependencyName = default) + public static ApiManifestDocument ToApiManifest(this OpenApiDocument document, string? apiDescriptionUrl, string applicationName, string? apiDependencyName = default) { ArgumentNullException.ThrowIfNull(document); ValidationHelpers.ValidateNullOrWhitespace(nameof(apiDescriptionUrl), apiDescriptionUrl, nameof(ApiManifestDocument)); ValidationHelpers.ValidateNullOrWhitespace(nameof(applicationName), applicationName, nameof(ApiManifestDocument)); - var apiName = apiDependencyName ?? document.Info.Title.Trim().Replace(' ', '-'); // Normilize OpenAPI document title to API name by trimming and replacing spaces with dashes. - var publisherName = document.Info.Contact?.Name ?? "publisher-name"; - var publisherEmail = document.Info.Contact?.Email ?? "publisher-email@example.com"; + apiDependencyName = NormalizeApiName(apiDependencyName ?? document.Info.Title); + var publisherName = document.Info.Contact?.Name ?? DefaultPublisherName; + var publisherEmail = document.Info.Contact?.Email ?? DefaultPublisherEmail; - string? apiDeploymentBaseUrl = default; - var server = document.Servers.FirstOrDefault(); - if (server is not null) - apiDeploymentBaseUrl = !server.Url.EndsWith("/", StringComparison.Ordinal) ? $"{server.Url}/" : server.Url; + string? apiDeploymentBaseUrl = GetApiDeploymentBaseUrl(document.Servers.FirstOrDefault()); var apiManifest = new ApiManifestDocument(applicationName) { Publisher = new(publisherName, publisherEmail), ApiDependencies = new() { { - apiName, new() { + apiDependencyName, new() { ApiDescriptionUrl = apiDescriptionUrl, ApiDescriptionVersion = document.Info.Version, ApiDeploymentBaseUrl = apiDeploymentBaseUrl, @@ -54,10 +54,25 @@ internal static ApiManifestDocument ToApiManifest(this OpenApiDocument document, Method = operation.Key.ToString(), UriTemplate = apiDeploymentBaseUrl != default ? path.Key.TrimStart('/') : path.Key }; - apiManifest.ApiDependencies[apiName].Requests.Add(requestInfo); + apiManifest.ApiDependencies[apiDependencyName].Requests.Add(requestInfo); } } return apiManifest; } + + private static string NormalizeApiName(string apiName) + { + // Normalize OpenAPI document title to API name by trimming and replacing spaces with dashes. + return apiName.Trim().Replace(' ', '-'); + } + + private static string? GetApiDeploymentBaseUrl(OpenApiServer? server) + { + if (server is null) + return null; + + // Ensure the base URL ends with a slash. + return !server.Url.EndsWith("/", StringComparison.Ordinal) ? $"{server.Url}/" : server.Url; + } } }