From 29d40a65cc2af478c3c9bb875c809df853ab37b5 Mon Sep 17 00:00:00 2001 From: HavenDV Date: Wed, 6 Dec 2023 07:35:58 +0400 Subject: [PATCH] feat: Added ModelGenerator template. --- .../{OptionsExtensions.cs => Extensions.cs} | 17 ++++- .../Generators/ModelGenerator.cs | 63 +++++++++++++------ .../Generators/NSwagGenerator.cs | 8 ++- src/libs/OpenApiGenerator/Models/Model.cs | 2 + .../Sources/Sources.Models.cs | 37 +---------- .../OpenApiGenerator/Sources/Sources.NSwag.cs | 9 +-- .../_#G.Models.AbuseResponse.g.verified.cs | 2 + .../_#G.Models.AsnResponse.g.verified.cs | 2 + .../_#G.Models.Carrier.g.verified.cs | 2 + .../_#G.Models.Company.g.verified.cs | 2 + .../_#G.Models.DomainsResponse.g.verified.cs | 2 + .../_#G.Models.Error.g.verified.cs | 2 + .../_#G.Models.FullResponse.g.verified.cs | 2 + .../_#G.Models.Prefix.g.verified.cs | 2 + .../_#G.Models.Prefix6.g.verified.cs | 2 + .../_#G.Models.PrivacyResponse.g.verified.cs | 2 + .../_#G.Models.RangesResponse.g.verified.cs | 2 + ...LocalFileUseNSwag_Diagnostics.verified.txt | 1 + .../_#G.Models.AbuseResponse.verified.cs} | 4 +- .../_#G.Models.AsnResponse.verified.cs} | 4 +- .../_#G.Models.Carrier.verified.cs} | 4 +- .../_#G.Models.Company.verified.cs} | 4 +- .../_#G.Models.DomainsResponse.verified.cs} | 4 +- .../_#G.Models.Error.verified.cs} | 4 +- .../_#G.Models.FullResponse.verified.cs} | 4 +- .../_#G.Models.Prefix.verified.cs} | 4 +- .../_#G.Models.Prefix6.verified.cs} | 4 +- .../_#G.Models.PrivacyResponse.verified.cs} | 4 +- .../_#G.Models.RangesResponse.verified.cs} | 4 +- .../Tests.Helpers.cs | 11 +++- .../OpenApiGenerator.SnapshotTests/Tests.cs | 21 +++++-- 31 files changed, 145 insertions(+), 90 deletions(-) rename src/libs/OpenApiGenerator/{OptionsExtensions.cs => Extensions.cs} (72%) create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AbuseResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AsnResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Carrier.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Company.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.DomainsResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Error.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.FullResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix6.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.PrivacyResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.RangesResponse.g.verified.cs create mode 100644 src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/Tests.YamlWithLocalFileUseNSwag_Diagnostics.verified.txt rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AbuseResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.AbuseResponse.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AsnResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.AsnResponse.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Carrier.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.Carrier.verified.cs} (97%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Company.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.Company.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.DomainsResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.DomainsResponse.verified.cs} (97%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Error.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.Error.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.FullResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.FullResponse.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.Prefix.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix6.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.Prefix6.verified.cs} (98%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.PrivacyResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.PrivacyResponse.verified.cs} (97%) rename src/tests/OpenApiGenerator.SnapshotTests/Snapshots/{YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.RangesResponse.verified.cs => YamlWithLocalFileUseNSwag/_#G.Models.RangesResponse.verified.cs} (97%) diff --git a/src/libs/OpenApiGenerator/OptionsExtensions.cs b/src/libs/OpenApiGenerator/Extensions.cs similarity index 72% rename from src/libs/OpenApiGenerator/OptionsExtensions.cs rename to src/libs/OpenApiGenerator/Extensions.cs index f10c1d241b..2accf797ba 100644 --- a/src/libs/OpenApiGenerator/OptionsExtensions.cs +++ b/src/libs/OpenApiGenerator/Extensions.cs @@ -1,10 +1,12 @@ using H.Generators.Extensions; using Microsoft.CodeAnalysis; +using Microsoft.OpenApi.Models; +using Microsoft.OpenApi.Readers; using OpenApiGenerator.Models; namespace OpenApiGenerator; -internal static class OptionsExtensions +internal static class Extensions { public static IncrementalValueProvider DetectSettings( this IncrementalGeneratorInitializationContext context) @@ -35,4 +37,17 @@ public static IncrementalValueProvider DetectSettings( )); } + + public static OpenApiDocument GetOpenApiDocument( + this AdditionalText text, + CancellationToken cancellationToken = default) + { + var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty; + var openApiDocument = new OpenApiStringReader().Read(yaml, out _); + + openApiDocument.Components ??= new OpenApiComponents(); + openApiDocument.Components.Schemas ??= new Dictionary(); + + return openApiDocument; + } } \ No newline at end of file diff --git a/src/libs/OpenApiGenerator/Generators/ModelGenerator.cs b/src/libs/OpenApiGenerator/Generators/ModelGenerator.cs index ba1be71c94..3d0140d6b6 100644 --- a/src/libs/OpenApiGenerator/Generators/ModelGenerator.cs +++ b/src/libs/OpenApiGenerator/Generators/ModelGenerator.cs @@ -1,13 +1,16 @@ -using Microsoft.CodeAnalysis; +using System.Collections.Immutable; +using H.Generators.Extensions; +using Microsoft.CodeAnalysis; +using OpenApiGenerator.Models; -namespace H.Generators; +namespace OpenApiGenerator; [Generator] public class ModelGenerator : IIncrementalGenerator { #region Constants - //private const string Id = "MG"; + private const string Id = "OAMG"; #endregion @@ -15,24 +18,46 @@ public class ModelGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { - // var settings = context.DetectSettings(); - // - // context.AdditionalTextsProvider - // .Where(static text => text.Path.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase)) - // .Combine(settings) - // .SelectAndReportExceptions(GetSourceCode, context, Id) - // .AddSource(context); + var settings = context.DetectSettings(); + + context.AdditionalTextsProvider + .Where(static text => text.Path.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase)) + .Combine(settings) + .SelectAndReportExceptions(PrepareData, context, Id) + .SelectMany(static (x, _) => x) + .SelectAndReportExceptions(GetSourceCode, context, Id) + .AddSource(context); } - // private static EquatableArray GetSourceCode( - // (AdditionalText text, Settings settings) tuple, - // CancellationToken cancellationToken = default) - // { - // var (text, settings) = tuple; - // - // return Sources.GenerateUsingNSwag(text, settings, cancellationToken); - // } - + private static EquatableArray PrepareData( + (AdditionalText text, Settings settings) tuple, + CancellationToken cancellationToken = default) + { + var (text, settings) = tuple; + if (settings.UseNSwag) + { + return ImmutableArray.Empty; + } + + var openApiDocument = text.GetOpenApiDocument(cancellationToken); + + return openApiDocument.Components.Schemas + .Select(schema => new Model( + Name: schema.Key.ToPropertyName(), + Namespace: settings.Namespace, + Style: settings.ModelStyle, + Properties: Array.Empty().ToImmutableArray())) + .ToImmutableArray(); + } + + private static FileWithName GetSourceCode( + Model model, + CancellationToken cancellationToken = default) + { + return new FileWithName( + Name: $"{model.Namespace}.Models.{model.Name}.g.cs", + Text: Sources.GenerateModel(model, cancellationToken: cancellationToken)); + } #endregion } diff --git a/src/libs/OpenApiGenerator/Generators/NSwagGenerator.cs b/src/libs/OpenApiGenerator/Generators/NSwagGenerator.cs index 15bb792bdf..3bd504d060 100644 --- a/src/libs/OpenApiGenerator/Generators/NSwagGenerator.cs +++ b/src/libs/OpenApiGenerator/Generators/NSwagGenerator.cs @@ -1,4 +1,5 @@ -using H.Generators.Extensions; +using System.Collections.Immutable; +using H.Generators.Extensions; using Microsoft.CodeAnalysis; using OpenApiGenerator.Models; @@ -31,10 +32,13 @@ private static EquatableArray GetSourceCode( CancellationToken cancellationToken = default) { var (text, settings) = tuple; + if (!settings.UseNSwag) + { + return ImmutableArray.Empty; + } return Sources.GenerateUsingNSwag(text, settings, cancellationToken); } - #endregion } diff --git a/src/libs/OpenApiGenerator/Models/Model.cs b/src/libs/OpenApiGenerator/Models/Model.cs index 634c242f71..ce23a292f8 100644 --- a/src/libs/OpenApiGenerator/Models/Model.cs +++ b/src/libs/OpenApiGenerator/Models/Model.cs @@ -3,5 +3,7 @@ namespace OpenApiGenerator.Models; internal readonly record struct Model( + string Name, + string Namespace, ModelStyle Style, EquatableArray Properties); \ No newline at end of file diff --git a/src/libs/OpenApiGenerator/Sources/Sources.Models.cs b/src/libs/OpenApiGenerator/Sources/Sources.Models.cs index 6081a6fafd..99b49f9cc7 100644 --- a/src/libs/OpenApiGenerator/Sources/Sources.Models.cs +++ b/src/libs/OpenApiGenerator/Sources/Sources.Models.cs @@ -1,44 +1,13 @@ -using System.Collections.Immutable; -using H.Generators.Extensions; -using Microsoft.CodeAnalysis; -using Microsoft.OpenApi.Readers; using OpenApiGenerator.Models; -using OpenApiSchema = Microsoft.OpenApi.Models.OpenApiSchema; -using NSwag; namespace OpenApiGenerator; internal static partial class Sources { - public static EquatableArray GenerateModel( - AdditionalText text, - Settings settings, + public static string GenerateModel( + Model model, CancellationToken cancellationToken = default) { - var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty; - var openApi = Task.Run(() => - OpenApiYamlDocument.FromYamlAsync(yaml, cancellationToken), cancellationToken).Result; - var openApiDocument = new OpenApiStringReader().Read(yaml, out _); - var schemas = openApiDocument.Components?.Schemas ?? new Dictionary(); - var prefix = Path.GetFileName(text.Path); - var allAdditionalExcludedTypeNames = schemas.Keys - .Select(schemaKey => GetAdditionalExcludedTypeNames(schemaKey, schemas)) - .SelectMany(x => x) - .ToArray(); - - var files = new List(); - files.AddRange(schemas.Keys.Select(schemaKey => - { - var excludedTypeNames = schemas.Keys - .Where(x => x != schemaKey) - .Concat(allAdditionalExcludedTypeNames.Except(GetAdditionalExcludedTypeNames(schemaKey, schemas))) - .ToArray(); - - return new FileWithName( - Name: $"{prefix}.Models.{schemaKey}.cs", - Text: Generate(openApi, settings, excludedTypeNames)); - })); - - return files.ToImmutableArray(); + return "//"; } } \ No newline at end of file diff --git a/src/libs/OpenApiGenerator/Sources/Sources.NSwag.cs b/src/libs/OpenApiGenerator/Sources/Sources.NSwag.cs index ae5415f66d..096077460b 100644 --- a/src/libs/OpenApiGenerator/Sources/Sources.NSwag.cs +++ b/src/libs/OpenApiGenerator/Sources/Sources.NSwag.cs @@ -1,7 +1,6 @@ using System.Collections.Immutable; using H.Generators.Extensions; using Microsoft.CodeAnalysis; -using Microsoft.OpenApi.Readers; using NJsonSchema.CodeGeneration.CSharp; using NSwag.CodeGeneration.CSharp; using NSwag.CodeGeneration.OperationNameGenerators; @@ -19,11 +18,10 @@ public static EquatableArray GenerateUsingNSwag( CancellationToken cancellationToken = default) { var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty; + var openApiDocument = text.GetOpenApiDocument(cancellationToken); var openApi = Task.Run(() => OpenApiYamlDocument.FromYamlAsync(yaml, cancellationToken), cancellationToken).Result; - var openApiDocument = new OpenApiStringReader().Read(yaml, out _); - var schemas = openApiDocument.Components?.Schemas ?? new Dictionary(); - var prefix = Path.GetFileName(text.Path); + var schemas = openApiDocument.Components.Schemas; var allAdditionalExcludedTypeNames = schemas.Keys .Select(schemaKey => GetAdditionalExcludedTypeNames(schemaKey, schemas)) .SelectMany(x => x) @@ -38,14 +36,13 @@ public static EquatableArray GenerateUsingNSwag( .ToArray(); return new FileWithName( - Name: $"{prefix}.Models.{schemaKey}.cs", + Name: $"{settings.Namespace}.Models.{schemaKey}.cs", Text: Generate(openApi, settings, excludedTypeNames)); })); return files.ToImmutableArray(); } - private static string[] GetAdditionalExcludedTypeNames( string key, IDictionary schemas) diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AbuseResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AbuseResponse.g.verified.cs new file mode 100644 index 0000000000..791e8cb4ed --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AbuseResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.AbuseResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AsnResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AsnResponse.g.verified.cs new file mode 100644 index 0000000000..ec9ceb461e --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AsnResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.AsnResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Carrier.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Carrier.g.verified.cs new file mode 100644 index 0000000000..f98c5dcaa2 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Carrier.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.Carrier.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Company.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Company.g.verified.cs new file mode 100644 index 0000000000..03ffb03256 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Company.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.Company.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.DomainsResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.DomainsResponse.g.verified.cs new file mode 100644 index 0000000000..29804496ac --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.DomainsResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.DomainsResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Error.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Error.g.verified.cs new file mode 100644 index 0000000000..de4d7ec518 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Error.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.Error.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.FullResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.FullResponse.g.verified.cs new file mode 100644 index 0000000000..3bfe3d1edf --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.FullResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.FullResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix.g.verified.cs new file mode 100644 index 0000000000..4a6377d163 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.Prefix.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix6.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix6.g.verified.cs new file mode 100644 index 0000000000..376fd669ed --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix6.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.Prefix6.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.PrivacyResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.PrivacyResponse.g.verified.cs new file mode 100644 index 0000000000..29d607c452 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.PrivacyResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.PrivacyResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.RangesResponse.g.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.RangesResponse.g.verified.cs new file mode 100644 index 0000000000..54c423c1d7 --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.RangesResponse.g.verified.cs @@ -0,0 +1,2 @@ +//HintName: G.Models.RangesResponse.g.cs +// \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/Tests.YamlWithLocalFileUseNSwag_Diagnostics.verified.txt b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/Tests.YamlWithLocalFileUseNSwag_Diagnostics.verified.txt new file mode 100644 index 0000000000..ad47dbb93f --- /dev/null +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/Tests.YamlWithLocalFileUseNSwag_Diagnostics.verified.txt @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AbuseResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AbuseResponse.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AbuseResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AbuseResponse.verified.cs index 177888f233..7bd553cfd8 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AbuseResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AbuseResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.AbuseResponse.cs +//HintName: G.Models.AbuseResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AsnResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AsnResponse.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AsnResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AsnResponse.verified.cs index debd83fe9e..d1e9c52547 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.AsnResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.AsnResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.AsnResponse.cs +//HintName: G.Models.AsnResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Carrier.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Carrier.verified.cs similarity index 97% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Carrier.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Carrier.verified.cs index f60b606436..3b37158879 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Carrier.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Carrier.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.Carrier.cs +//HintName: G.Models.Carrier.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Company.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Company.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Company.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Company.verified.cs index 3ff0485f8f..de25b7d067 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Company.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Company.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.Company.cs +//HintName: G.Models.Company.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.DomainsResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.DomainsResponse.verified.cs similarity index 97% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.DomainsResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.DomainsResponse.verified.cs index 30eba3c80b..1802399cc0 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.DomainsResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.DomainsResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.DomainsResponse.cs +//HintName: G.Models.DomainsResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Error.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Error.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Error.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Error.verified.cs index a88e376d1c..a81e5bf53f 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Error.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Error.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.Error.cs +//HintName: G.Models.Error.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.FullResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.FullResponse.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.FullResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.FullResponse.verified.cs index 920f42cb41..8ef8051c63 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.FullResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.FullResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.FullResponse.cs +//HintName: G.Models.FullResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix.verified.cs index 0c1b734da5..fb944f104a 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.Prefix.cs +//HintName: G.Models.Prefix.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix6.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix6.verified.cs similarity index 98% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix6.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix6.verified.cs index c9a6a47157..48a95fa547 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.Prefix6.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.Prefix6.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.Prefix6.cs +//HintName: G.Models.Prefix6.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.PrivacyResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.PrivacyResponse.verified.cs similarity index 97% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.PrivacyResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.PrivacyResponse.verified.cs index f3418b9f58..f3df0c43d6 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.PrivacyResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.PrivacyResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.PrivacyResponse.cs +//HintName: G.Models.PrivacyResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.RangesResponse.verified.cs b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.RangesResponse.verified.cs similarity index 97% rename from src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.RangesResponse.verified.cs rename to src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.RangesResponse.verified.cs index ade547eebe..c2b2d14819 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/Tests.YamlWithLocalFile#openapi.yaml.Models.RangesResponse.verified.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFileUseNSwag/_#G.Models.RangesResponse.verified.cs @@ -1,4 +1,4 @@ -//HintName: openapi.yaml.Models.RangesResponse.cs +//HintName: G.Models.RangesResponse.cs //---------------------- // // Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org) @@ -18,7 +18,7 @@ #pragma warning disable 8603 // Disable "CS8603 Possible null reference return" #pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter" -namespace OpenApiGenerator +namespace G { using System = global::System; diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Tests.Helpers.cs b/src/tests/OpenApiGenerator.SnapshotTests/Tests.Helpers.cs index 5af09cd12f..ab3190ef1a 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Tests.Helpers.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Tests.Helpers.cs @@ -11,11 +11,16 @@ namespace H.Generators.IntegrationTests; [TestClass] public partial class Tests : VerifyBase { - private async Task CheckSourceAsync( + private async Task CheckSourceAsync( AdditionalText[] additionalTexts, + Dictionary? globalOptions = null, [CallerMemberName] string? callerName = null, CancellationToken cancellationToken = default) + where T : IIncrementalGenerator, new() { + globalOptions ??= new Dictionary(); + globalOptions.Add("build_property.OpenApiGenerator_Namespace", "G"); + var referenceAssemblies = ReferenceAssemblies.Net.Net60 .WithPackages(ImmutableArray.Create(new PackageIdentity("Newtonsoft.Json", "13.0.1"))); var references = await referenceAssemblies.ResolveAsync(null, cancellationToken); @@ -23,10 +28,11 @@ private async Task CheckSourceAsync( assemblyName: "Tests", references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - var generator = new NSwagGenerator(); + var generator = new T(); var driver = CSharpGeneratorDriver .Create(generator) .AddAdditionalTexts(ImmutableArray.Create(additionalTexts)) + .WithUpdatedAnalyzerConfigOptions(new DictionaryAnalyzerConfigOptionsProvider(globalOptions)) .RunGeneratorsAndUpdateCompilation(compilation, out compilation, out _, cancellationToken); var diagnostics = compilation.GetDiagnostics(cancellationToken); @@ -37,6 +43,7 @@ await Task.WhenAll( .UseTextForParameters("Diagnostics"), Verify(driver) .UseDirectory($"Snapshots/{callerName}") + .UseFileName("_") //.AutoVerify() ); } diff --git a/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs b/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs index 169b139320..efe593a3ae 100644 --- a/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs +++ b/src/tests/OpenApiGenerator.SnapshotTests/Tests.cs @@ -2,6 +2,7 @@ using Microsoft.OpenApi; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; +using OpenApiGenerator; namespace H.Generators.IntegrationTests; @@ -43,14 +44,26 @@ public partial class Tests [TestMethod] public Task Empty() { - return CheckSourceAsync( + return CheckSourceAsync( Array.Empty()); } + [TestMethod] + public Task YamlWithLocalFileUseNSwag() + { + return CheckSourceAsync(new AdditionalText[] + { + new CustomAdditionalText("openapi.yaml", Resources.ipinfo_yaml.AsString()), + }, new Dictionary + { + ["build_property.OpenApiGenerator_UseNSwag"] = "true", + }); + } + [TestMethod] public Task YamlWithLocalFile() { - return CheckSourceAsync(new AdditionalText[] + return CheckSourceAsync(new AdditionalText[] { new CustomAdditionalText("openapi.yaml", Resources.ipinfo_yaml.AsString()), }); @@ -61,7 +74,7 @@ public Task OpenApi3_0_Yaml() { var yaml = PetStore.Serialize(OpenApiSpecVersion.OpenApi3_0, OpenApiFormat.Yaml); - return CheckSourceAsync(new AdditionalText[] + return CheckSourceAsync(new AdditionalText[] { new CustomAdditionalText("openapi.yaml", yaml), }); @@ -70,7 +83,7 @@ public Task OpenApi3_0_Yaml() [TestMethod] public Task YamlWithUrl() { - return CheckSourceAsync(new AdditionalText[] + return CheckSourceAsync(new AdditionalText[] { new CustomAdditionalText("https://dedoose-rest-api.onrender.com/swagger/v1/swagger.json", string.Empty), });