Skip to content

Commit

Permalink
refactor: Added OpenApiGenerator.Core library.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Apr 5, 2024
1 parent da562b5 commit 76c06e6
Show file tree
Hide file tree
Showing 23 changed files with 474 additions and 274 deletions.
7 changes: 7 additions & 0 deletions OpenApiGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.Integratio
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.Helpers", "src\libs\OpenApiGenerator.Helpers\OpenApiGenerator.Helpers.csproj", "{CF6126AF-6934-46EC-A0F2-B75C673EA8D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiGenerator.Core", "src\libs\OpenApiGenerator.Core\OpenApiGenerator.Core.csproj", "{3A3F5EE3-0076-4822-B3BA-2955EC802E25}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -50,6 +52,10 @@ Global
{CF6126AF-6934-46EC-A0F2-B75C673EA8D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF6126AF-6934-46EC-A0F2-B75C673EA8D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF6126AF-6934-46EC-A0F2-B75C673EA8D8}.Release|Any CPU.Build.0 = Release|Any CPU
{3A3F5EE3-0076-4822-B3BA-2955EC802E25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A3F5EE3-0076-4822-B3BA-2955EC802E25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A3F5EE3-0076-4822-B3BA-2955EC802E25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A3F5EE3-0076-4822-B3BA-2955EC802E25}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -59,6 +65,7 @@ Global
{440B9C75-CA33-47F3-9EDF-A7CD6886155C} = {9CAA231D-7BE1-46C9-ACD6-EB2E569CEBEA}
{0EC09367-E7C5-4847-A9F3-DBC18A1C8395} = {9CAA231D-7BE1-46C9-ACD6-EB2E569CEBEA}
{CF6126AF-6934-46EC-A0F2-B75C673EA8D8} = {6E5BF389-3D3F-4D74-9DD0-3B199CB529C5}
{3A3F5EE3-0076-4822-B3BA-2955EC802E25} = {6E5BF389-3D3F-4D74-9DD0-3B199CB529C5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1493AEE4-9211-46E9-BFE6-8F629EAC5693}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System.Globalization;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using OpenApiGenerator.Models;
namespace OpenApiGenerator;
using OpenApiGenerator.Core.Models;

internal static class Extensions
namespace OpenApiGenerator.Core.Extensions;

public static class OpenApiExtensions
{
public static OpenApiDocument GetOpenApiDocument(
this AdditionalText text,
this string yaml,
CancellationToken cancellationToken = default)
{
var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty;
var openApiDocument = new OpenApiStringReader().Read(yaml, out _);

openApiDocument.Components ??= new OpenApiComponents();
Expand All @@ -25,13 +23,13 @@ public static OpenApiDocument GetOpenApiDocument(
public static string GetCSharpType(
this KeyValuePair<string, OpenApiSchema> schema,
Settings settings,
params Model[] parents)
params ModelData[] parents)
{
var model = Model.FromSchema(schema, settings, parents);
var model = ModelData.FromSchema(schema, settings, parents);
var (type, reference) = (schema.Value.Type, schema.Value.Format) switch
{
("object", _) or (null, _) when schema.Value.Reference != null =>
($"{Model.FromKey(schema.Value.Reference.Id, settings).ClassName}", true),
($"{ModelData.FromKey(schema.Value.Reference.Id, settings).ClassName}", true),
("object", _) when schema.Value.Reference == null =>
($"{model.ExternalClassName}", true),

Expand Down Expand Up @@ -68,30 +66,17 @@ public static string GetCSharpType(
: type;
}

public static string AsArray(this string type)
{
return $"global::System.Collections.Generic.IList<{type}>";
}

public static string? WithPostfix(this string? type, string postfix)
{
if (type == null)
{
return null;
}

return type + postfix;
}

public static string? GetDefaultValue(this OpenApiSchema schema)
{
schema = schema ?? throw new ArgumentNullException(nameof(schema));

return schema.Default?.GetString();
}

private readonly static string[] NewLine = { "\n" };

public static string GetSummary(this OpenApiSchema schema)
{
schema = schema ?? throw new ArgumentNullException(nameof(schema));

var summary = schema.Description ?? string.Empty;
if (schema.Default != null)
{
Expand Down Expand Up @@ -122,56 +107,6 @@ public static string GetSummary(this OpenApiSchema schema)
};
}

public static string ToXmlDocumentationSummary(
this string text,
int level = 4)
{
var lines = text.Split(NewLine, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length == 0)
{
lines = new[] { string.Empty };
}

var spaces = new string(' ', level);

return $@"/// <summary>
{string.Join("\n", lines
.Select(line => $"{spaces}/// {line}"))}
{spaces}/// </summary>";
}

public static string UseWordSeparator(
this string propertyName,
params char[] separator)
{
if (!separator.Any(propertyName.Contains))
{
return propertyName;
}

return string.Join(
string.Empty,
propertyName
.Split(separator)
.Select(word => word.ToPropertyName()));
}

public static string AddIndent(
this string text,
int level)
{
if (level < 1)
{
return text;
}

var lines = text.Split(NewLine, StringSplitOptions.None);
var spaces = new string(' ', level * 4);

return string.Join("\n", lines
.Select(line => string.IsNullOrEmpty(line) ? line : $"{spaces}{line}"));
}

public static bool IsObjectWithoutReference(
this OpenApiSchema schema)
{
Expand All @@ -181,6 +116,8 @@ public static bool IsObjectWithoutReference(
public static bool IsEnum(
this OpenApiSchema schema)
{
schema = schema ?? throw new ArgumentNullException(nameof(schema));

return schema.Type == "string" && schema.Enum.Any();
}

Expand All @@ -199,7 +136,7 @@ public static KeyValuePair<string, OpenApiSchema> WithKey(
}


public static Property ToEnumValue(
public static PropertyData ToEnumValue(
this IOpenApiAny any)
{
var id = any.GetString() ?? string.Empty;
Expand All @@ -208,29 +145,22 @@ public static Property ToEnumValue(
.UseWordSeparator('_', '-')
.Replace(".", string.Empty);

return Property.Default with
return PropertyData.Default with
{
Id = id,
Name = name,
};
}

public static string FixPropertyName(
this string propertyName,
string className)
{
return propertyName == className
? $"{propertyName}1"
: propertyName;
}

public static Property ToProperty(
public static PropertyData ToProperty(
this KeyValuePair<string, OpenApiSchema> schema,
HashSet<string> requiredProperties,
Settings settings,
params Model[] parents)
params ModelData[] parents)
{
return new Property(
requiredProperties = requiredProperties ?? throw new ArgumentNullException(nameof(requiredProperties));

return new PropertyData(
Id: schema.Key,
Name: schema.Key.ToPropertyName()
.FixPropertyName(parents.Last().ClassName)
Expand All @@ -241,12 +171,12 @@ public static Property ToProperty(
Summary: schema.Value.GetSummary());
}

public static IEnumerable<Model> WithAdditionalModels(
this Model model)
public static IEnumerable<ModelData> WithAdditionalModels(
this ModelData modelData)
{
return new []{ model }
.Concat(model.AdditionalModels.SelectMany(WithAdditionalModels))
.Concat(model.Enumerations.SelectMany(WithAdditionalModels));
return new []{ modelData }
.Concat(modelData.AdditionalModels.SelectMany(WithAdditionalModels))
.Concat(modelData.Enumerations.SelectMany(WithAdditionalModels));
}

public static IEnumerable<OpenApiReference> GetReferences(
Expand Down
Loading

0 comments on commit 76c06e6

Please sign in to comment.