Skip to content

Commit

Permalink
Update schema cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Sep 3, 2024
1 parent 1660050 commit 414ec00
Show file tree
Hide file tree
Showing 6 changed files with 957 additions and 999 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static async Task<FileResponse> GetAssetAsync(ISession session, AssetDto
{
try
{
return await session.Client.Assets.GetAssetContentBySlugAsync(asset.Id, string.Empty);
return await session.Client.Assets.GetAssetContentBySlugAsync(asset.Id);
}
catch (SquidexException ex) when (ex.StatusCode == 404)
{
Expand Down
5 changes: 0 additions & 5 deletions cli/Squidex.CLI/Squidex.CLI/Commands/App_Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public async Task Create(CreateArguments arguments)

while (!tcs.Token.IsCancellationRequested)
{
#pragma warning disable CS0612 // Type or member is obsolete
var backups = await session.Client.Backups.GetBackupsAsync(tcs.Token);
#pragma warning restore CS0612 // Type or member is obsolete
var backup = backups.Items.Find(x => x.Started >= backupStarted);

if (backup?.Stopped != null)
Expand All @@ -68,7 +66,6 @@ public async Task Create(CreateArguments arguments)
await using (var fs = new FileStream(arguments.File, mode))
{
using (var download = await session.Client.Backups.GetBackupContentAsync(foundBackup.Id))
#pragma warning restore CS0612 // Type or member is obsolete
{
await download.Stream.CopyToAsync(fs);
}
Expand All @@ -78,9 +75,7 @@ public async Task Create(CreateArguments arguments)
{
log.WriteLine("Removing backup from app");

#pragma warning disable CS0612 // Type or member is obsolete
await session.Client.Backups.DeleteBackupAsync(foundBackup.Id);
#pragma warning restore CS0612 // Type or member is obsolete
}

log.Completed("Backup Download completed.");
Expand Down
18 changes: 8 additions & 10 deletions csharp/Squidex.ClientLibrary/CodeGeneration/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,6 @@
{% if pathPart contains "}" -%}
{% assign pathParameterParts = pathPart | split: "}" -%}
{% assign pathParameterFound = true -%}
{% if pathParameterParts[0] == "app" -%}
urlBuilder_.Append(_options.AppName);
{% endif -%}
{% for parameter in operation.PathParameters -%}
{% if parameter.Name == pathParameterParts[0] -%}
{% if parameter.IsOptional -%}
Expand Down Expand Up @@ -331,20 +328,21 @@
{% endunless -%}
{% endif -%}
{% if operation.HasQueryParameters -%}
urlBuilder_.Append('?');
urlBuilder_.Append('?');
{% for parameter in operation.QueryParameters -%}
{% if parameter.IsOptional -%}
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.QueryParameter %}
}
if ({{ parameter.VariableName }} != null)
{
{% template Client.Class.QueryParameter %}
}
{% else -%}
{% template Client.Class.QueryParameter %}
{% endif -%}
{% endfor -%}
urlBuilder_.Length--;
urlBuilder_.Length--;
{% endif -%}

urlBuilder_.Replace("$app$", _options.AppName);

{% if GeneratePrepareRequestAndProcessResponseAsAsyncMethods %}
await PrepareRequestAsync(client_, request_, urlBuilder_, cancellationToken).ConfigureAwait(false);
{% else -%}
Expand Down
35 changes: 23 additions & 12 deletions csharp/Squidex.ClientLibrary/CodeGeneration/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ public static class Program
{
public static async Task Main()
{
var document = await OpenApiDocument.FromUrlAsync("https://localhost:5001/api/swagger/v1/swagger.json");

var rootFolder = Environment.GetEnvironmentVariable("SDKS_ROOT_FOLDER")!;

SchemaCleaner.AddExtensions(document);
if (string.IsNullOrEmpty(rootFolder))
{
Console.WriteLine("Configure the SDK root folder with 'SDKS_ROOT_FOLDER' environment variable.");
return;
}

var document = await OpenApiDocument.FromUrlAsync("https://localhost:5001/api/swagger/v1/swagger.json");

// We write a more complete schema for fern code generation.
File.WriteAllText(Path.Combine(rootFolder, "sdk-php/openapi.json"), document.ToJson().UseCloudUrl());
File.WriteAllText(Path.Combine(rootFolder, "sdk-fern/fern/openapi/openapi.json"), document.ToJson().UseCloudUrl());
File.WriteAllText(Path.Combine(rootFolder, "sdk-fern/other-generators/openapi.json"), document.ToJson().UseCloudUrl());
WriteToFile(rootFolder, document, "sdk-spec/openapi.json");

// This cleanup is only needed for .NET.
SchemaCleaner.RemoveAppName(document);
SchemaCleaner.AddExtensions(document);

// We also need a version without app name.
File.WriteAllText(Path.Combine(rootFolder, "sdk-fern/other-generators/openapi-noapp.json"), document.ToJson().UseCloudUrl());
WriteToFile(rootFolder, document, "sdk-java/openapi.json");
WriteToFile(rootFolder, document, "sdk-node/openapi.json");
WriteToFile(rootFolder, document, "sdk-php/openapi.json");

SchemaCleaner.RemoveUnusedSchemas(document);

Expand All @@ -38,6 +39,16 @@ public static async Task Main()
File.WriteAllText(Path.Combine(rootFolder, "samples/csharp/Squidex.ClientLibrary/Squidex.ClientLibrary/Generated.cs"), sourceCode);
}

private static void WriteToFile(string rootFolder, OpenApiDocument document, string path)
{
var targetFile = new FileInfo(Path.Combine(rootFolder, path));

if (targetFile.Directory!.Exists)
{
File.WriteAllText(targetFile.FullName, document.ToJson().UseCloudUrl());
}
}

private static string GenerateCode(OpenApiDocument document)
{
var generatorSettings = new CSharpClientGeneratorSettings();
Expand All @@ -48,7 +59,7 @@ private static string GenerateCode(OpenApiDocument document)
generatorSettings.CSharpGeneratorSettings.DictionaryBaseType = "System.Collections.Generic.Dictionary";
generatorSettings.CSharpGeneratorSettings.DictionaryInstanceType = "System.Collections.Generic.Dictionary";
generatorSettings.CSharpGeneratorSettings.DictionaryType = "System.Collections.Generic.Dictionary";
generatorSettings.CSharpGeneratorSettings.ExcludedTypeNames = new[] { "JsonInheritanceConverter" };
generatorSettings.CSharpGeneratorSettings.ExcludedTypeNames = ["JsonInheritanceConverter"];
generatorSettings.CSharpGeneratorSettings.Namespace = "Squidex.ClientLibrary";
generatorSettings.CSharpGeneratorSettings.PropertyNameGenerator = new CustomPropertyNameGenerator();
generatorSettings.CSharpGeneratorSettings.PropertySetterAccessModifier = string.Empty;
Expand Down
65 changes: 41 additions & 24 deletions csharp/Squidex.ClientLibrary/CodeGeneration/SchemaCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,69 @@ namespace CodeGeneration;

internal static class SchemaCleaner
{
private static readonly HashSet<string> PathParametersToRemove = ["app", "more"];

public static void AddExtensions(OpenApiDocument document)
{
document.Security = null;
document.Components.SecuritySchemes.Clear();

static void AddExtensions(OpenApiOperation operation)
{
operation.ExtensionData ??= new Dictionary<string, object?>();
operation.ExtensionData["x-fern-sdk-group-name"] = operation.Tags[0].ToCamelCase();
operation.ExtensionData["x-fern-sdk-method-name"] = operation.OperationId.Split('_').Last().ToCamelCase();
operation.ExtensionData["x-method-name"] = operation.OperationId.Split('_').Last().ToCamelCase();

foreach (var parameter in operation.Parameters)
foreach (var parameter in operation.Parameters.ToList())
{
if (parameter.Kind == OpenApiParameterKind.Path && parameter.Name == "app")
if (parameter.Kind == OpenApiParameterKind.Path && PathParametersToRemove.Contains(parameter.Name))
{
operation.Parameters.Remove(parameter);
}

if (parameter.Kind == OpenApiParameterKind.Header)
{
const string Prefix = "X-";

var name = parameter.Name;

parameter.ExtensionData ??= new Dictionary<string, object?>();
parameter.ExtensionData["x-fern-sdk-variable"] = "appName";
parameter.ExtensionData["x-header-name"] = name;

if (name.StartsWith(Prefix, StringComparison.Ordinal))
{
name = name[Prefix.Length..];
}

parameter.Name = name.ToCamelCase();
}

operation.Security = null;
}
}

foreach (var description in document.Operations)
{
description.Path = CleanupPath(description.Path);

AddExtensions(description.Operation);
}

document.ExtensionData ??= new Dictionary<string, object?>();
document.ExtensionData["x-fern-sdk-variables"] = new
foreach (var (path, item) in document.Paths.ToList())
{
appName = new
{
type = "string"
}
};
}
string newPath = CleanupPath(path);

public static void RemoveAppName(OpenApiDocument document)
{
foreach (var description in document.Operations.ToList())
document.Paths.Remove(path);
document.Paths[newPath] = item;
}

document.ExtensionData ??= new Dictionary<string, object?>();

static string CleanupPath(string path)
{
var parameters = description.Operation.Parameters;
path = path.Replace("{app}", "$app$");
path = path.Replace("{more}", string.Empty);

foreach (var parameter in parameters.ToList())
{
if (parameter.Kind == OpenApiParameterKind.Path && parameter.Name == "app")
{
parameters.Remove(parameter);
}
}
return path;
}
}

Expand Down
Loading

0 comments on commit 414ec00

Please sign in to comment.