-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Use constants for default values.
- Loading branch information
Showing
1 changed file
with
25 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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, | ||
|
@@ -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; | ||
} | ||
} | ||
} |