Skip to content

Commit

Permalink
- adds support for logo, privacy, legal urls
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Mar 28, 2024
1 parent 9193228 commit 643f9ee
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/Kiota.Builder/OpenApiDocumentDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ ex is SecurityException ||
settings.AddMicrosoftExtensionParsers();
settings.ExtensionParsers.TryAdd(OpenApiKiotaExtension.Name, static (i, _) => OpenApiKiotaExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiDescriptionForModelExtension.Name, static (i, _) => OpenApiDescriptionForModelExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiLogoExtension.Name, static (i, _) => OpenApiLogoExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiPrivacyPolicyUrlExtension.Name, static (i, _) => OpenApiPrivacyPolicyUrlExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiLegalInfoUrlExtension.Name, static (i, _) => OpenApiLegalInfoUrlExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiAiReasoningInstructionsExtension.Name, static (i, _) => OpenApiAiReasoningInstructionsExtension.Parse(i));
settings.ExtensionParsers.TryAdd(OpenApiAiRespondingInstructionsExtension.Name, static (i, _) => OpenApiAiRespondingInstructionsExtension.Parse(i));
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
using System;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
using Microsoft.OpenApi.Any;

namespace Kiota.Builder.OpenApiExtensions;

public class OpenApiDescriptionForModelExtension : IOpenApiExtension
public class OpenApiDescriptionForModelExtension : OpenApiSimpleStringExtension
{
public static string Name => "x-ai-description";
public string? Description
{
get; set;
}
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
ArgumentNullException.ThrowIfNull(writer);
if (!string.IsNullOrWhiteSpace(Description))
{
writer.WriteValue(Description);
}
}
protected override string? ValueSelector => Description;
public static OpenApiDescriptionForModelExtension Parse(IOpenApiAny source)
{
if (source is not OpenApiString rawString) throw new ArgumentOutOfRangeException(nameof(source));
return new OpenApiDescriptionForModelExtension
{
Description = rawString.Value
Description = ParseString(source)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.OpenApi.Any;

namespace Kiota.Builder.OpenApiExtensions;

public class OpenApiLegalInfoUrlExtension : OpenApiSimpleStringExtension
{
public static string Name => "x-legal-info-url";
public string? Legal
{
get; set;
}
protected override string? ValueSelector => Legal;
public static OpenApiLegalInfoUrlExtension Parse(IOpenApiAny source)
{
return new OpenApiLegalInfoUrlExtension
{
Legal = ParseString(source)
};
}
}
20 changes: 20 additions & 0 deletions src/Kiota.Builder/OpenApiExtensions/OpenApiLogoExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.OpenApi.Any;

namespace Kiota.Builder.OpenApiExtensions;

public class OpenApiLogoExtension : OpenApiSimpleStringExtension
{
public static string Name => "x-logo";
public string? Logo
{
get; set;
}
protected override string? ValueSelector => Logo;
public static OpenApiLogoExtension Parse(IOpenApiAny source)
{
return new OpenApiLogoExtension
{
Logo = ParseString(source)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.OpenApi.Any;

namespace Kiota.Builder.OpenApiExtensions;

public class OpenApiPrivacyPolicyUrlExtension : OpenApiSimpleStringExtension
{
public static string Name => "x-privacy-policy-url";
public string? Privacy
{
get; set;
}
protected override string? ValueSelector => Privacy;
public static OpenApiPrivacyPolicyUrlExtension Parse(IOpenApiAny source)
{
return new OpenApiPrivacyPolicyUrlExtension
{
Privacy = ParseString(source)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;

namespace Kiota.Builder.OpenApiExtensions;

public abstract class OpenApiSimpleStringExtension : IOpenApiExtension
{
protected abstract string? ValueSelector
{
get;
}
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
ArgumentNullException.ThrowIfNull(writer);
if (!string.IsNullOrWhiteSpace(ValueSelector))
{
writer.WriteValue(ValueSelector);
}
}
public static string ParseString(IOpenApiAny source)
{
if (source is not OpenApiString rawString) throw new ArgumentOutOfRangeException(nameof(source));
return rawString.Value;
}
}
22 changes: 18 additions & 4 deletions src/Kiota.Builder/Plugins/PluginsGenerationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,23 @@ private ManifestDocument GetManifestDocument(string openApiDocumentPath)
var (runtimes, functions) = GetRuntimesAndFunctionsFromTree(TreeNode, openApiDocumentPath);
var descriptionForHuman = OAIDocument.Info?.Description.CleanupXMLString() is string d && !string.IsNullOrEmpty(d) ? d : $"Description for {OAIDocument.Info?.Title.CleanupXMLString()}";
var descriptionForModel = descriptionForHuman;
if (OAIDocument.Info is not null &&
OAIDocument.Info.Extensions.TryGetValue(OpenApiDescriptionForModelExtension.Name, out var descriptionExtension) &&
string? legalUrl = null;
string? logoUrl = null;
string? privacyUrl = null;
if (OAIDocument.Info is not null)
{

if (OAIDocument.Info.Extensions.TryGetValue(OpenApiDescriptionForModelExtension.Name, out var descriptionExtension) &&
descriptionExtension is OpenApiDescriptionForModelExtension extension &&
!string.IsNullOrEmpty(extension.Description))
descriptionForModel = extension.Description.CleanupXMLString();
descriptionForModel = extension.Description.CleanupXMLString();
if (OAIDocument.Info.Extensions.TryGetValue(OpenApiLegalInfoUrlExtension.Name, out var legalExtension) && legalExtension is OpenApiLegalInfoUrlExtension legal)
legalUrl = legal.Legal;
if (OAIDocument.Info.Extensions.TryGetValue(OpenApiLogoExtension.Name, out var logoExtension) && logoExtension is OpenApiLogoExtension logo)
logoUrl = logo.Logo;
if (OAIDocument.Info.Extensions.TryGetValue(OpenApiPrivacyPolicyUrlExtension.Name, out var privacyExtension) && privacyExtension is OpenApiPrivacyPolicyUrlExtension privacy)
privacyUrl = privacy.Privacy;
}
return new ManifestDocument
{
SchemaVersion = "v2",
Expand All @@ -73,7 +85,9 @@ descriptionExtension is OpenApiDescriptionForModelExtension extension &&
DescriptionForModel = descriptionForModel,
ContactEmail = OAIDocument.Info?.Contact?.Email,
Namespace = Configuration.ClientClassName,
//TODO logo
LogoUrl = logoUrl,
LegalInfoUrl = legalUrl,
PrivacyPolicyUrl = privacyUrl,
Runtimes = [.. runtimes
.GroupBy(static x => x, _openAPIRuntimeComparer)
.Select(static x =>
Expand Down

0 comments on commit 643f9ee

Please sign in to comment.