Foxnouns.NET/Foxnouns.Backend/Utils/OpenApi/PropertyKeySchemaTransformer.cs

70 lines
2 KiB
C#
Raw Normal View History

using Foxnouns.Backend.Database;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;
namespace Foxnouns.Backend.Utils.OpenApi;
public class PropertyKeySchemaTransformer : IOpenApiSchemaTransformer
{
private static readonly DefaultContractResolver SnakeCaseConverter =
new() { NamingStrategy = new SnakeCaseNamingStrategy() };
public Task TransformAsync(
OpenApiSchema schema,
OpenApiSchemaTransformerContext context,
CancellationToken cancellationToken
)
{
Dictionary<string, OpenApiSchema> newProperties = new();
foreach (KeyValuePair<string, OpenApiSchema> property in schema.Properties)
{
newProperties[SnakeCaseConverter.GetResolvedPropertyName(property.Key)] =
property.Value;
}
schema.Properties = newProperties;
schema.Required = schema
.Required.Select(SnakeCaseConverter.GetResolvedPropertyName)
.ToHashSet();
return Task.CompletedTask;
}
}
public class ExampleFixingSchemaTransformer : IOpenApiSchemaTransformer
{
public Task TransformAsync(
OpenApiSchema schema,
OpenApiSchemaTransformerContext context,
CancellationToken cancellationToken
)
{
if (context.JsonTypeInfo.Type == typeof(Snowflake))
{
schema.Type = "string";
schema.Example = new OpenApiString("999999999999999999");
}
return Task.CompletedTask;
}
}
public class DocumentTransformer(Config config) : IOpenApiDocumentTransformer
{
public Task TransformAsync(
OpenApiDocument document,
OpenApiDocumentTransformerContext context,
CancellationToken cancellationToken
)
{
document.Info.Title = "pronouns.cc API";
document.Info.Version = "2.0.0";
document.Servers.Clear();
document.Servers.Add(new OpenApiServer { Url = config.BaseUrl });
return Task.CompletedTask;
}
}