Skip to content

Commit

Permalink
chore: Use constants for default values.
Browse files Browse the repository at this point in the history
  • Loading branch information
peombwa committed Nov 14, 2023
1 parent 8db5cef commit 95f68f6
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/lib/TypeExtensions/OpenApiDocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace Microsoft.OpenApi.ApiManifest.TypeExtensions
{
public static class OpenApiDocumentExtensions
{
private const string DefaultPublisherName = "publisher-name";
private const string DefaultPublisherEmail = "[email protected]";

/// <summary>
/// Converts an <see cref="OpenApiDocument"/> to an <see cref="ApiManifestDocument"/>.
/// </summary>
Expand All @@ -16,27 +19,24 @@ public static class OpenApiDocumentExtensions
/// <param name="applicationName">The name of the application.</param>
/// <param name="apiDependencyName">The name of the API dependency.</param>
/// <returns>An <see cref="ApiManifestDocument"/>.</returns>
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 ?? "[email protected]";
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,
Expand All @@ -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;
}
}
}

0 comments on commit 95f68f6

Please sign in to comment.