Skip to content

Commit

Permalink
fix: Fixed .json/url generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 21, 2024
1 parent 754bd82 commit 76c4bcb
Show file tree
Hide file tree
Showing 16 changed files with 5,473 additions and 172 deletions.
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<PropertyGroup Label="Versioning">
<Version>0.6.6</Version>
<Version>0.6.7</Version>
<MinVerMinimumMajorMinor>0.1</MinVerMinimumMajorMinor>
<MinVerTagPrefix>v</MinVerTagPrefix>
<MinVerDefaultPreReleaseIdentifiers>dev</MinVerDefaultPreReleaseIdentifiers>
Expand Down
16 changes: 8 additions & 8 deletions src/libs/OpenApiGenerator.Core/Generation/Sources.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public static string GenerateMethod(
/// {endPoint.Summary}
/// </summary>
{endPoint.Properties.Where(x => x.ParameterLocation != null).Select(x => $@"
/// <param name=""{x.Name.ToParameterName()}""></param>").Inject()}
/// <param name=""{x.ParameterName}""></param>").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType) ? " " : @"
/// <param name=""request""></param>")}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
public async {taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(x => x.ParameterLocation != null).Select(x => $@"
{x.Type.CSharpType} {x.Name.ToParameterName()},").Inject()}
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{(string.IsNullOrWhiteSpace(endPoint.RequestType) ? " " : @$"
{endPoint.RequestType} request,")}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default)
Expand All @@ -109,7 +109,7 @@ public static string GenerateMethod(
{(endPoint.JsonSerializerType == JsonSerializerType.NewtonsoftJson ? endPoint.Properties
.Where(x => x is { ParameterLocation: not null, Type.EnumValues.Length: > 0 })
.Select(x => $@"
var {x.ArgumentName} = {x.Name.ToParameterName()} switch
var {x.ArgumentName} = {x.ParameterName} switch
{{
{x.Type.Properties.Zip(x.Type.EnumValues, (property, value) => (Property: property, Value: value))
.Select(y => $@"
Expand Down Expand Up @@ -184,25 +184,25 @@ public static string GenerateExtensionMethod(
/// {endPoint.Summary}
/// </summary>
{endPoint.Properties.Select(x => $@"
/// <param name=""{x.Name.ToParameterName()}""></param>").Inject()}
/// <param name=""{x.ParameterName}""></param>").Inject()}
/// <param name=""cancellationToken"">The token to cancel the operation with</param>
/// <exception cref=""global::System.InvalidOperationException""></exception>
public async {taskType} {endPoint.MethodName}(
{endPoint.Properties.Where(static x => x.IsRequired).Select(x => $@"
{x.Type.CSharpType} {x.Name.ToParameterName()},").Inject()}
{x.Type.CSharpType} {x.ParameterName},").Inject()}
{endPoint.Properties.Where(static x => !x.IsRequired).Select(x => $@"
{x.Type.CSharpType} {x.Name.ToParameterName()} = default,").Inject()}
{x.Type.CSharpType} {x.ParameterName} = default,").Inject()}
{cancellationTokenAttribute}global::System.Threading.CancellationToken cancellationToken = default)
{{
var request = new {endPoint.RequestType}
{{
{endPoint.Properties.Where(x => x.ParameterLocation == null).Select(x => $@"
{x.Name} = {x.Name.ToParameterName()},").Inject()}
{x.Name} = {x.ParameterName},").Inject()}
}};
{response}{endPoint.MethodName}(
{endPoint.Properties.Where(x => x.ParameterLocation != null).Select(x => $@"
{x.Name.ToParameterName()}: {x.Name.ToParameterName()},").Inject()}
{x.ParameterName}: {x.ParameterName},").Inject()}
request: request,
cancellationToken: cancellationToken){configureAwaitResponse};
{(endPoint.Stream ? @"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class ModelGeneratorMethods
{
#region Methods

public static ImmutableArray<ModelData> PrepareData(
public static EquatableArray<ModelData> PrepareData(
(string text, Settings settings) tuple,
CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -75,11 +75,11 @@ public static ImmutableArray<ModelData> PrepareData(
})
.ToImmutableArray();

return [
return ImmutableArray.Create([
..components,
..objectParameters,
..enumParameters
];
]);
}

public static FileWithName GetSourceCode(
Expand Down
6 changes: 4 additions & 2 deletions src/libs/OpenApiGenerator.Core/Models/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ public static PropertyData FromSchema(
Summary: schema.Value.GetSummary());
}

public string ParameterName => Name.Replace(".", string.Empty).ToParameterName();

public string ArgumentName
{
get
{
if (Type.EnumValues.Length != 0 && JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return Name.ToParameterName() + "Value";
return ParameterName + "Value";
}

return Name.ToParameterName();
return ParameterName;
}
}
}
55 changes: 0 additions & 55 deletions src/libs/OpenApiGenerator/Generators/ClientGenerator.cs

This file was deleted.

78 changes: 0 additions & 78 deletions src/libs/OpenApiGenerator/Generators/ModelGenerator.cs

This file was deleted.

95 changes: 95 additions & 0 deletions src/libs/OpenApiGenerator/SdkGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using OpenApiGenerator.Core.Generators;
using OpenApiGenerator.Core.Models;
using FileWithName = H.Generators.Extensions.FileWithName;

namespace OpenApiGenerator;

[Generator]
public class SdkGenerator : IIncrementalGenerator
{
#region Constants

private const string Id = "OAG";

#endregion

#region Methods

public void Initialize(IncrementalGeneratorInitializationContext context)
{
var settings = context.DetectSettings();

var filesAndSettings = context.AdditionalTextsProvider
.Where(static text => text.Path.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase) ||
text.Path.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase))
.Select(static (text, cancellationToken) => GetContent(text, cancellationToken))
.Combine(settings);

filesAndSettings
.SelectAndReportExceptions(ClientGeneratorMethods.PrepareData, context, Id)
.SelectMany(static (x, _) => x)
.SelectAndReportExceptions(GetMethodSourceCode, context, Id)
.AddSource(context);

var models = filesAndSettings
.SelectAndReportExceptions(ModelGeneratorMethods.PrepareData, context, Id);

models
.SelectMany(static (x, _) => x)
.SelectAndReportExceptions(GetModelSourceCode, context, Id)
.AddSource(context);
models
.Select(static (x, _) => x)
.SelectAndReportExceptions(GetSuperTypeSourceCode, context, Id)
.AddSource(context);
}

private static string GetContent(AdditionalText additionalText, CancellationToken cancellationToken = default)
{
return additionalText.Path.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? Task.Run(() => new HttpClient().GetStringAsync(new Uri(additionalText.Path)), cancellationToken).Result
: additionalText.GetText(cancellationToken)?.ToString() ?? string.Empty;;
}

private static FileWithName GetMethodSourceCode(
EndPoint endPoint,
CancellationToken cancellationToken = default)
{
var fileWithName = ClientGeneratorMethods.GetSourceCode(endPoint, cancellationToken);

return new FileWithName(
Name: fileWithName.Name,
Text: fileWithName.Text);
}

private static FileWithName GetModelSourceCode(
ModelData model,
CancellationToken cancellationToken = default)
{
var fileWithName = ModelGeneratorMethods.GetSourceCode(model, cancellationToken);

return new FileWithName(
Name: fileWithName.Name,
Text: fileWithName.Text);
}

private static FileWithName GetSuperTypeSourceCode(
Core.Models.EquatableArray<ModelData> models,
CancellationToken cancellationToken = default)
{
if (models.IsEmpty)
{
return FileWithName.Empty;
}

var fileWithName = ModelGeneratorMethods.GetSourceCodeForSuperClass(models, cancellationToken);

return new FileWithName(
Name: fileWithName.Name,
Text: fileWithName.Text);
}

#endregion
}
Loading

0 comments on commit 76c4bcb

Please sign in to comment.